User:Former User aDB0haVymg/Gadgets/editclassic.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
* editclassic.js
*
* ----
*
* A MediaWiki Gadget to enable editing via the classic wikitext editor,
* even when VisualEditor & 2017 Wikitext Editor is fully enabled.
*
* ----
*
* @author User:Classy Melissa on wikicn.playgoteam.workers.dev
* @version 8
* @license Public Domain
*
*/
/* global mw */
// wrap in anonymous function to prevent scope leak
(function() {
/**
* The starting point.
* @param {string} key Pre-defined ignition key
*/
function ignite(key) {
if (key != "851a869c-3811-4d49-9c8d-0e567317579c") {
throw new Error("Invalid ignition key");
}
// find the node to copy
const oldLiNode = document.querySelector("li#ca-edit");
// cannot proceed if this doesn't exist, or doesn't have a link inside
if (checkPageEligibility(oldLiNode) === false) {
// maybe it's not an existing article page. Just ignore.
console.log("editclassic.js: Non-eligible page ignored.");
return;
}
// clone the node & find the link within
const newLiNode = oldLiNode.cloneNode(true);
const newLiLink = newLiNode.querySelector("a");
// hotpatch v#3: if the old node is selected, de-select it.
if (oldLiNode.classList.contains("selected")) {
oldLiNode.classList.remove("selected");
}
// change or clear its relevant attributes
newLiNode.id = "ca-edit-classic";
applyNewLinkNodeStyles(newLiLink);
// generate and insert the little 2010 badge
const newLiBadge = generateBadge();
newLiLink.insertAdjacentElement("beforeend", newLiBadge);
// update link href
newLiLink.href = getUpdatedHref(newLiLink.href);
// insert newLiNode
oldLiNode.insertAdjacentElement("afterend", newLiNode);
// done.
}
/**
* Generates the 2010 little blue badge
*/
function generateBadge() {
const newLiBadge = document.createElement("span");
// various styles
/* it's my intentional design to not use innerHTML
* Because that's gross.
*/
newLiBadge.innerText = "2010";
newLiBadge.style.fontSize = "80%";
newLiBadge.style.display = "inline";
newLiBadge.style.border = "2px solid #A7BED3";
newLiBadge.style.borderRadius = "3px";
newLiBadge.style.padding = "0.1em 0.5em";
newLiBadge.style.marginLeft = "0.5em";
newLiBadge.style.verticalAlign = "top";
newLiBadge.style.backgroundColor = "#CDF7F6";
return newLiBadge;
}
function applyNewLinkNodeStyles(newLiLink) {
newLiLink.accessKey = null;
newLiLink.id = "ca-edit-classic-link";
newLiLink.title = "Edit this page in classic wikitext editor";
newLiLink.innerText = "Edit";
}
/**
* Checks if the page is eligible for this little gadget.
* @returns true or false
*/
function checkPageEligibility(oldLiNode) {
if (!(oldLiNode && oldLiNode.querySelector("a"))) {
return false;
} else {
return true;
}
}
/**
* Gets an updated href target from an old target, for use with the new link.
* replace "veaction" with "action" (if applicable) and
* "editsource" with "submit"
*/
function getUpdatedHref(oldHref) {
var newHrefTarget = oldHref;
newHrefTarget = newHrefTarget.replace(/(ve)?action/, "action");
newHrefTarget = newHrefTarget.replace(/edit(source)?/, "submit");
return newHrefTarget;
}
// launch the operation asynchronously
return new Promise(
function(resolve, reject) {
try {
ignite("851a869c-3811-4d49-9c8d-0e567317579c");
resolve();
} catch(e) {
mw.notify("editclassic.js error: " + e.toString());
reject();
}
}
)
})();