SFF Enhancer

Replaces ID on SFF with copy link, and adds extra features

// ==UserScript==
// @name         SFF Enhancer
// @namespace    https://sff.i5.lu/
// @version      1.2
// @license      GPLv3
// @description  Replaces ID on SFF with copy link, and adds extra features
// @author       ceodoe
// @match        https://sff.i5.lu/
// @match        https://sff.i5.lu/index.php
// @icon         https://www.google.com/s2/favicons?sz=64&domain=getsession.org
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

function setupOptionsBoxes() {
    let bumpLink = document.querySelector("a[href$='bump.php'");

    let html = `
        | <input type="checkbox" name="sffe-shorten-ids-checkbox" id="sffe-shorten-ids-checkbox" ${shorten ? `checked` : ``}><label for="sffe-shorten-ids-checkbox">Shorten IDs</label>
        <input type="checkbox" name="sffe-favicon-checkbox" id="sffe-favicon-checkbox" ${setFavicon ? `checked` : ``}><label for="sffe-favicon-checkbox">Add site favicon</label>
        <input type="checkbox" name="sffe-autorefresh-checkbox" id="sffe-autorefresh-checkbox" ${autoRefresh ? `checked` : ``}><label for="sffe-autorefresh-checkbox">Auto refresh</label>
        <input type="checkbox" name="sffe-remember-copied-checkbox" id="sffe-remember-copied-checkbox" ${rememberCopiedIDs ? `checked` : ``}><label for="sffe-remember-copied-checkbox">Remember copied IDs</label>
        <input type="checkbox" name="sffe-hide-add-date" id="sffe-hide-add-date" ${hideAddDate ? `checked` : ``}><label for="sffe-hide-add-date">Hide "Add Date" column</label>
        <input type="checkbox" name="sffe-hide-bump-date" id="sffe-hide-bump-date" ${hideBumpDate ? `checked` : ``}><label for="sffe-hide-bump-date">Hide "Bump Date" column</label>
    `;

    bumpLink.insertAdjacentHTML("afterend", html);

    document.getElementById("sffe-shorten-ids-checkbox").addEventListener("change", function() {
        GM_setValue("sffeShortenIDs", this.checked);
        location.reload();
    });

    document.getElementById("sffe-favicon-checkbox").addEventListener("change", function() {
        GM_setValue("sffeFavicon", this.checked);
        location.reload();
    });

    document.getElementById("sffe-autorefresh-checkbox").addEventListener("change", function() {
        GM_setValue("sffeAutoRefresh", this.checked);
        location.reload();
    });

    document.getElementById("sffe-remember-copied-checkbox").addEventListener("change", function() {
        GM_setValue("sffeRememberCopiedIDs", this.checked);

        if(!this.checked) {
            GM_setValue("sffeRememberedIDs", []); // clear saved IDs if user disables memory
        }

        location.reload();
    });

    document.getElementById("sffe-hide-add-date").addEventListener("change", function() {
        GM_setValue("sffeHideAddDate", this.checked);
        location.reload();
    });

    document.getElementById("sffe-hide-bump-date").addEventListener("change", function() {
        GM_setValue("sffeHideBumpDate", this.checked);
        location.reload();
    });
}

function setupCopyableIDs() {
    let idElements = document.querySelectorAll("tr > td:first-child");
    for(let i = 1; i < idElements.length; i++) { // Skip first row as it is a header row not marked as such
        let id = idElements[i].innerText.trim();
        let displayID = shorten ? id.substring(0,9) + "…" : id;

        let html = `
            <span class="copy-btn" id="copy-btn-${i}" data-session-id="${id}" style="cursor: pointer; border-bottom: 1px dashed;" title="Click to copy">
                <span id="copy-icon-${i}">${rememberCopiedIDs && rememberedIDs.includes(id) ? `✅` : `📄`}</span> ${displayID}
            </span>
        `;

        idElements[i].innerHTML = html;

        document.getElementById(`copy-btn-${i}`).addEventListener("click", async function() {
            let tempInput = document.createElement("input");
            let sessionID = this.getAttribute("data-session-id").trim();
            tempInput.value = sessionID;
            tempInput.select();
            tempInput.setSelectionRange(0,66);

            try {
                await navigator.clipboard.writeText(tempInput.value);
                document.getElementById(`copy-icon-${i}`).innerText = "✅";

                if(rememberCopiedIDs) {
                    if(!rememberedIDs.includes(sessionID)) {
                        rememberedIDs.push(sessionID);
                        GM_setValue("sffeRememberedIDs", rememberedIDs);
                    }
                }
            } catch (err) {
                alert("Sorry, failed to copy to clipboard. Your browser said:\n\n" + err);
                document.getElementById(`copy-icon-${i}`).innerText = "❌";
            }
        });
    }
}

function setupFavicon() {
    document.querySelector("head").insertAdjacentHTML("afterbegin", `
        <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAMAMDAAAAEAIACoJQAANgAAACAgAAABACAA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26zY1OJsyMTT+MTA0/zlDMP9Yjh3/YaYX/2GlF/9hpRf/YaUX/2GlF/9hpRf/YKIY/0VgKP8yMDT/MjE0/jY1OJs6OTshNDM2ujIxNP8xMDT/MjA0/zU4Mv82OjL/Njoy/zY6Mv82OjL/Njoy/zM0M/8xLzT/MjE0/zQzNro6OTshAAAAADk4OyE2NTibNDM27DIxNP8yMTT/MjE0/zIxNP8yMTT/MjE0/zIxNP8yMTX/NDM27DY1OJs5ODshAAAAAMADAACAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAEAAMADAAA=" />
    `);
}

function main() {
    GM_addStyle(`
        td:first-child {
            white-space: nowrap;
        }

        ${hideAddDate ? `
        td:nth-child(3) {
            display: none;
        }
        ` : ``}

        ${hideBumpDate ? `
        td:nth-child(4) {
            display: none;
        }
        ` : ``}
    `);

    if(setFavicon) {
        setupFavicon();
    }

    if(autoRefresh) {
        window.setTimeout(function() {
            location.reload();
        }, 300000);
    }

    setupOptionsBoxes();
    setupCopyableIDs();
}

let shorten = GM_getValue("sffeShortenIDs", true);
let setFavicon = GM_getValue("sffeFavicon", true);
let autoRefresh = GM_getValue("sffeAutoRefresh", true);
let rememberCopiedIDs = GM_getValue("sffeRememberCopiedIDs", true);
let rememberedIDs = GM_getValue("sffeRememberedIDs", []);
let hideAddDate = GM_getValue("sffeHideAddDate", true);
let hideBumpDate = GM_getValue("sffeHideBumpDate", false);
main();