The Mudcat Café TM
Thread #120999   Message #4223375
Posted By: GUEST,Grishka
28-May-25 - 12:23 PM
Thread Name: Tech: how do i? (blue clicky)
Subject: RE: Tech: how do i? (blue clicky)
Here is my bold attempt at a linkifyer to single Mudcat posts. Unfortunately, you first have to save the page to your computer, then copy the "Date:" line of the post you want to link to. The usage will be self-explanatory.

Note: courtesy of ChatGPT; my own part being reduced to fault-finding. Feel free to improve.


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Extract Post Link</title>
</head>
<body>
    <label for="dateInput">Enter the "Date:" line of the post:</label>
    <input type="text" id="dateInput"><br><br>

    <p>On the thread page in your browser, right click and invoke the "Save page as ..." function. Remember where you stored the file, and press the following button to select it.</p>
    <input type="file" id="fileInput"><br><br>

    <p id="output"></p>

    <script>
       document.getElementById("fileInput").addEventListener("change", function() {
            let dateInput = document.getElementById("dateInput").value.trim();
            if (!dateInput.startsWith("Date:")) {
                document.getElementById("output").textContent = "Error: Please enter the entire 'Date:' line.";
                return;
            }

            const file = this.files[0];
            if (!file) {
                document.getElementById("output").textContent = "Error: Please select a file.";
                return;
            }

            const reader = new FileReader();
            reader.onload = function(event) {
                const text = event.target.result;

                // Modify search string: "Date: mydate" -> "Date:</b> mydate"
                dateInput = dateInput.replace("Date:", "Date:</b>");

                // Search for modified date string
                const dateIndex = text.indexOf(dateInput);
                if (dateIndex === -1) {
                   document.getElementById("output").textContent = "Error: Date text not found in file.";
                   return;
                }

                // Extract canonical URL
                const canonicalMatch = text.match(/<link rel="canonical" href="([^"]+)">/);
                const url = canonicalMatch ? canonicalMatch[1] : null;

                // Find the **last anchor before the date**
                const beforeText = text.substring(0, dateIndex);
                const anchorMatch = [...beforeText.matchAll(/<a name="?([^">]+)"?>/gi)].pop();
                const anchor = anchorMatch ? anchorMatch[1] : null;

                if (!url || !anchor) {
                   document.getElementById("output").textContent = "Error: Could not extract required data.";
                   return;
                }

                // Construct final link
                const resultLink       = `<a href="${url}#${anchor}">this postt</a>`;
                const resultLinkEsc = `&lt;a href="${url}#${anchor}"&gt;this post&lt;/a&gt;`;
                document.getElementById("output").innerHTML = "The following link has been copied to your clipboard:<br>"
                      + resultLinkEsc
                      + "<br>Just press Ctrl.-v in your post to Mudcat, or anywhere where links are allowed. Edit the \"this post\" part to your wishes."

                // Copy to clipboard
                navigator.clipboard.writeText(resultLink).catch(err => console.error("Clipboard error:", err));
            };

            reader.readAsText(file);
       });
    </script>
</body>
</html>