MediaWiki:Gadget-OrangeLinks.js
Jump to navigation
Jump to search
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- The following documentation is located at MediaWiki:Gadget-OrangeLinks.js/documentation. [edit]
- This script is a part of the
OrangeLinks
gadget (edit definitions)- Description (edit): OrangeLinks: colour links orange if the target language is missing on an existing page
- Useful links: subpage list • links • redirects
Colors links with a language name as anchor if there is no entry for that language on the page linked to.
See also: Special:Gadgets.
// <nowiki>
let actionAPI = new mw.Api({ ajax: { headers: { "Api-User-Agent": "Gadget developed by [[User:Ioaxxere]]" } } });
let HTML_unescape = text => new DOMParser().parseFromString(text, "text/html").body.textContent; // Why isn't this a builtin...?
mw.util.addCSS(`
.orange-link {
color: #B88D00;
}
.orange-link:visited {
color: #826F34;
}
`);
// Maps each page to a list of IDs.
let pageIDsOf = new Map();
function makeOrangeLinks(element) {
// Get a list of pages and links to process.
let pagesToProcess = [];
let linksToProcess = [];
for (let link of element.querySelectorAll("a")) {
// Check whether the link needs to be processed.
if (!link.matches([`[href^="/wiki/"]`])) continue;
if (link.matches(".orange-link, .not-orange-link, .new, .external")) continue;
let linkTitle = decodeURIComponent(link.href.split("/wiki/").pop().split(/[#?]/)[0]);
let linkAnchor = decodeURIComponent(link.href.split("#")[1] || "");
if (!linkAnchor || /^[a-z]/.test(linkAnchor) || (new mw.Title(linkTitle)).namespace !== 0) continue;
pagesToProcess.push(linkTitle);
linksToProcess.push(link);
}
// Filter out duplicates.
pagesToProcess = Array.from(new Set(pagesToProcess));
// Process the array in chunks.
let queries = [];
for (let i = 0; i < pagesToProcess.length; i += 100) {
let chunk = pagesToProcess.slice(i, i + 100);
// Query the IDs for all the pages using [[Module:get IDs]].
let wikitext = "{{#invoke:get IDs|show|" + chunk.join("|") + "}}";
let params = {
action: "expandtemplates",
format: "json",
prop: "wikitext",
text: wikitext
};
queries.push(actionAPI.post(params).then(response => {
// Integrate the results into `pageIDsOf`.
let pageIDs = HTML_unescape(response.expandtemplates.wikitext).split("\n\n");
for (let j = 0; j < chunk.length; j++) {
pageIDsOf.set(chunk[j], pageIDs[j].split(" "));
}
}));
}
// After all the queries have returned, determine whether each link needs to be orange.
Promise.all(queries).then(() => {
for (let link of linksToProcess) {
let linkTitle = decodeURIComponent(link.href.split("/wiki/").pop().split(/[#?]/)[0]);
let linkAnchor = decodeURIComponent(link.href.split("#")[1] || "");
if (pageIDsOf.get(linkTitle).includes(linkAnchor)) {
link.classList.add("not-orange-link");
} else {
link.classList.add("orange-link");
}
}
});
}
// Activate the gadget.
let pageContent = document.querySelector(".mw-parser-output");
if (pageContent)
makeOrangeLinks(pageContent);
// Create a global hook in case any other gadget or script would like to activate it.
window.makeOrangeLinks = makeOrangeLinks;
// </nowiki>