User:BoxOfNotes/sugarcube.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:BoxOfNotes/sugarcube. |
// 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"> `)
}