Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
(function () {

$.when(mw.loader.using(["mediawiki.util"]), $.ready).done(function () {
    mw.util.addPortletLink(
        "p-personal",
        mw.util.getUrl("User:Tollens/Dashboard"),
        "Dashboard",
        "pt-dashboard",
        "View the dashboard",
        null,
        "#pt-preferences"
    );
    mw.util.addPortletLink(
        "p-personal",
        mw.util.getUrl("Special:ShortPages"),
        "Short Pages",
        "pt-shortpages",
        "View the list of short pages",
        null,
        "#pt-preferences"
    );
    mw.util.addPortletLink(
        "p-personal",
        mw.util.getUrl("Special:PendingChanges"),
        "Pending Changes",
        "pt-pendingchanges",
        "View the list of edits awaiting review",
        null,
        "#pt-preferences"
    );
});

const currentPage = mw.config.get("wgPageName");
const currentNamespace = mw.config.get("wgCanonicalNamespace");

if (currentPage === "User:Tollens/common.js") {
    const toggleLink = document.createElement("a");
    toggleLink.href = "#";
    toggleLink.textContent = "Toggle dev mode";
    toggleLink.onclick = () => {
        localStorage.setItem("TollensDevMode", localStorage.getItem("TollensDevMode") === "true" ? "false" : "true");
        location.reload();
        return false;
    };

    const separator = document.createElement("span");
    separator.textContent = " – ";

    document.getElementById("firstHeading").appendChild(separator);
    document.getElementById("firstHeading").appendChild(toggleLink);
}

if (!currentPage.includes("/")) {
    if (currentNamespace === "User" || currentNamespace === "User_talk") {
        const fullAddition = document.createElement("span");
        const contribsLink = document.createElement("a");
        contribsLink.href =
            "https://en.wikipedia.org/wiki/Special:Contributions/" +
            currentPage.substring(currentPage.indexOf(":") + 1);
        contribsLink.textContent = "contribs";
        fullAddition.appendChild(document.createTextNode(" ("));
        fullAddition.appendChild(contribsLink);
        fullAddition.appendChild(document.createTextNode(")"));

        document.getElementById("firstHeading").appendChild(fullAddition);
    }
}

if (localStorage.getItem("TollensDevMode") === "true") {
    const devModeWarningSpan = document.createElement("span");
    devModeWarningSpan.style.setProperty("color", "#ff0000", "important");
    devModeWarningSpan.textContent = "DEV MODE: ";
    document.getElementById("firstHeading").prepend(devModeWarningSpan);

    // Disable user stylesheets (global.css/common.css/vector.css, all of them)
    // Needs to be done repeatedly because we have no way of knowing how many are loaded or if they've been loaded yet
    let disableAttempts = 0;
    let disableSSInterval = setInterval(() => {
        for (let sheet of document.styleSheets) {
            if (sheet.href && sheet.href.includes("user.styles") && sheet.href.includes("Tollens"))
                sheet.disabled = true;
        }
        disableAttempts++;

        if (disableAttempts >= 10) clearInterval(disableSSInterval);
    }, 500);

    mw.loader.load("http://localhost:5500/Code/localFileToRead.js"); // Import the local dev file

    console.warn("%cDEV MODE ENABLED", "font-size: 3em; font-weight: bold;");
} else {
    importScript("User:Terasail/Edit Request Tool.js"); // Backlink: [[User:Terasail/Edit Request Tool.js]]
    importScript("User:Enterprisey/cv-revdel.js"); // Backlink: [[User:Enterprisey/cv-revdel.js]]
    importScript("User:Tollens/subnetContribs.js"); // Backlink: [[User:Tollens/subnetContribs.js]]
    importScript("User:Fred_Gandt/confirmLogout.js"); // Backlink: [[User:Fred_Gandt/confirmLogout.js]]
    importScript("User:Awesome Aasim/xfdvote.js"); // Backlink: [[User:Awesome Aasim/xfdvote.js]]
    importScript("User:Tollens/watchUser.js"); // Backlink: [[User:Tollens/watchUser.js]]
    importScript("User:Tollens/longCommenter.js"); // Backlink: [[User:Tollens/longCommenter.js]]
}

})();