Jump to content

User:BoxOfNotes/sugarcube.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BoxOfNotes (talk | contribs) at 04:56, 21 August 2022 (v0.2). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// Cookie functions
function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
function eraseCookie(name) {   
    document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

// Setup
sugarcubeEnabled = JSON.parse(getCookie("sugarcube-enabled"))
titleIcon = getCookie("sugarcube-titleicon")

// Special:Sugarcube page
function sugarcubeChecked(toCheck) {
    if(sugarcubeEnabled[toCheck] == true) {
        return "checked"
    }
    return ""
}
function sugarcubeSave() {
    sugarcubeEnabled = {
        "tools":$("#sugarcube-features-tools").is(":checked"),
        "noSiteSub":$("#sugarcube-features-noSiteSub").is(":checked"),
        "titleIcon":$("#sugarcube-features-titleIcon").is(":checked")
    }
    titleIcon = $("#sugarcube-titleIcon").val()
    setCookie("sugarcube-enabled",JSON.stringify(sugarcubeEnabled),14)
    setCookie("sugarcube-titleicon",titleIcon,14)
    location.reload()
}

if(mw.config.get("wgPageName") == "Special:Sugarcube") {
    $("#firstHeading").text("Sugarcube Preferences")
    $("#mw-content-text").html(`
<b>Note:</b> Preferences will be saved as cookies that last 14 days. That means, if you haven't changed your preferences in 14 days, they will get reset.
<h2>Features</h2>
<input type="checkbox" id="sugarcube-features-tools" name="" ${sugarcubeChecked("tools")}>
<label for="sugarcube-features-tools">Extra tools (<code>tools</code>) - Adds in 3 dropdown buttons on the top left with more functionality.</label>
<br />
<input type="checkbox" id="sugarcube-features-noSiteSub" name="" ${sugarcubeChecked("noSiteSub")}>
<label for="sugarcube-features-noSiteSub">No site subtitle (<code>noSiteSub</code>) - Removes the "From Wikipedia, the free encyclopedia" below page titles.</label>
<br />
<input type="checkbox" id="sugarcube-features-titleIcon" name="" ${sugarcubeChecked("titleIcon")}>
<label for="sugarcube-features-titleIcon">Title icon (<code>titleIcon</code>) - Add a small image before every page title.</label>
<br />
<label for="sugarcube-titleIcon" style="padding-left:8px">The URL of the image:</label>
<input type="text" id="sugarcube-titleIcon" name="" value="${titleIcon}">
<br />
<a onclick="sugarcubeSave()"><b>Save preferences</b></a>
`)
}

// Special:Sugarcube button in personal menu
$(`
<li id="sugarcube-button" class="mw-list-item">
    <a href="https://en.wikipedia.org/wiki/Special:Sugarcube" onclick="sugarcubeSpecial()" title="Your sugarcube preferences">
        <span>sugarcube</span>
    </a>
</li>
`).insertBefore("#pt-betafeatures")

// FEATURES

// tools
if(sugarcubeEnabled["tools"] == true) {
    $(`
<div id="sugarcube-tools" style="position:absolute;left:180px;top:12px;font-size:14px">
    Tools:
    <a onclick="sugarcubeToolsWiki">Wiki</a>,
    <a onclick="sugarcubeToolsPage">Page</a>
</div>
`).insertBefore("#p-personal");
}

// noSiteSub
if(sugarcubeEnabled["noSiteSub"] == true) {
    $("#siteSub").remove()
}

// titleIcon
if(sugarcubeEnabled["titleIcon"] == true) {
    $("#firstHeading").prepend(`<img src="${mw.html.escape(titleIcon)}" width="40px" height="40px" style="padding-bottom:8px">&nbsp;`)
}