MediaWiki:Gadget-OrangeLinks.js: Difference between revisions
Jump to navigation
Jump to search
Created page with "// <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...? function getTitleAndAnchor(link) { let linkTitle = decodeURIComponent(link.pathname.split("/wiki/")[1]); let linkAnchor = decodeURIComponent(link.hash.slice(1) || ""); return [linkTitle, linkAnchor]; } mw.util.addCS..." |
No edit summary |
||
| Line 1: | Line 1: | ||
// <nowiki> | // <nowiki> | ||
var actionAPI = new mw.Api({ ajax: { headers: { "Api-User-Agent": "Gadget developed by [[User:Ioaxxere]]" } } }); | |||
function HTML_unescape(text) { | |||
return new DOMParser().parseFromString(text, "text/html").body.textContent; | |||
} | |||
function getTitleAndAnchor(link) { | function getTitleAndAnchor(link) { | ||
var linkTitle = decodeURIComponent(link.pathname.split("/wiki/")[1]); | |||
var linkAnchor = decodeURIComponent(link.hash.slice(1) || ""); | |||
return [linkTitle, linkAnchor]; | return [linkTitle, linkAnchor]; | ||
} | } | ||
mw.util.addCSS( | mw.util.addCSS( | ||
.orange-link { | ".orange-link { color: var(--wikt-palette-gold, #b88d00); }" + | ||
".orange-link:visited { color: var(--wikt-palette-dullgold, #826f34); }" + | |||
".orange-link:hover, .orange-link:visited:hover { color: var(--wikt-palette-honey, #81540e); }" | |||
.orange-link:visited { | ); | ||
.orange-link:hover, .orange-link:visited:hover { | |||
// | // Use plain object instead of Map | ||
var pageIDsOf = {}; | |||
function makeOrangeLinks(element) { | function makeOrangeLinks(element) { | ||
// Get a list of pages and links to process. | // Get a list of pages and links to process. | ||
var pagesToProcess = []; | |||
var linksToProcess = []; | |||
var links = element.querySelectorAll("a"); | |||
for (var i = 0; i < links.length; i++) { | |||
var link = links[i]; | |||
// Check whether the link needs to be processed. | // Check whether the link needs to be processed. | ||
if (!link.href. | if (!link.href.match(/^https:\/\/(en|en\.m)\.wiktionary\.org\/wiki\//)) continue; | ||
if (link.matches(".orange-link, .not-orange-link, .new, .external")) continue; | if (link.matches(".orange-link, .not-orange-link, .new, .external")) continue; | ||
var linkParts = getTitleAndAnchor(link); | |||
var linkTitle = linkParts[0]; | |||
var linkAnchor = linkParts[1]; | |||
if (!linkAnchor || /^[a-z]/.test(linkAnchor) || (new mw.Title(linkTitle)).namespace !== 0) continue; | if (!linkAnchor || /^[a-z]/.test(linkAnchor) || (new mw.Title(linkTitle)).namespace !== 0) continue; | ||
| Line 42: | Line 45: | ||
// Filter out duplicates. | // Filter out duplicates. | ||
pagesToProcess = | var seen = {}; | ||
var uniquePages = []; | |||
for (var j = 0; j < pagesToProcess.length; j++) { | |||
if (!seen[pagesToProcess[j]]) { | |||
seen[pagesToProcess[j]] = true; | |||
uniquePages.push(pagesToProcess[j]); | |||
} | |||
} | |||
pagesToProcess = uniquePages; | |||
// Process the array in chunks. | // Process the array in chunks. | ||
var queries = []; | |||
for ( | for (var k = 0; k < pagesToProcess.length; k += 100) { | ||
(function(chunk) { | |||
var wikitext = "{{#invoke:get IDs|show|" + chunk.join("|") + "}}"; | |||
var params = { | |||
action: "expandtemplates", | |||
format: "json", | |||
prop: "wikitext", | |||
text: wikitext | |||
}; | |||
queries.push(actionAPI.post(params).then(function(response) { | |||
// Integrate the results into `pageIDsOf`. | |||
var pageIDs = HTML_unescape(response.expandtemplates.wikitext).split("\n\n"); | |||
for (var m = 0; m < chunk.length; m++) { | |||
pageIDsOf[chunk[m]] = pageIDs[m].split(" "); | |||
} | } | ||
})); | })); | ||
})(pagesToProcess.slice(k, k + 100)); | |||
} | } | ||
// After all the queries have returned, determine whether each link needs to be orange. | // After all the queries have returned, determine whether each link needs to be orange. | ||
Promise.all(queries).then(() | Promise.all(queries).then(function() { | ||
for ( | for (var n = 0; n < linksToProcess.length; n++) { | ||
var link = linksToProcess[n]; | |||
if (pageIDsOf | var linkParts = getTitleAndAnchor(link); | ||
var linkTitle = linkParts[0]; | |||
var linkAnchor = linkParts[1]; | |||
if (pageIDsOf[linkTitle] && pageIDsOf[linkTitle].indexOf(linkAnchor) !== -1) { | |||
link.classList.add("not-orange-link"); | link.classList.add("not-orange-link"); | ||
} else { | } else { | ||
| Line 81: | Line 94: | ||
// Activate the gadget. | // Activate the gadget. | ||
var pageContent = document.querySelector(".mw-parser-output"); | |||
if (pageContent) | if (pageContent) { | ||
makeOrangeLinks(pageContent); | makeOrangeLinks(pageContent); | ||
} | |||
// Create a global hook in case any other gadget or script would like to activate it. | // Create a global hook in case any other gadget or script would like to activate it. | ||
window.makeOrangeLinks = makeOrangeLinks; | window.makeOrangeLinks = makeOrangeLinks; | ||
// </nowiki> | // </nowiki> | ||
Revision as of 18:22, 16 September 2025
// <nowiki>
var actionAPI = new mw.Api({ ajax: { headers: { "Api-User-Agent": "Gadget developed by [[User:Ioaxxere]]" } } });
function HTML_unescape(text) {
return new DOMParser().parseFromString(text, "text/html").body.textContent;
}
function getTitleAndAnchor(link) {
var linkTitle = decodeURIComponent(link.pathname.split("/wiki/")[1]);
var linkAnchor = decodeURIComponent(link.hash.slice(1) || "");
return [linkTitle, linkAnchor];
}
mw.util.addCSS(
".orange-link { color: var(--wikt-palette-gold, #b88d00); }" +
".orange-link:visited { color: var(--wikt-palette-dullgold, #826f34); }" +
".orange-link:hover, .orange-link:visited:hover { color: var(--wikt-palette-honey, #81540e); }"
);
// Use plain object instead of Map
var pageIDsOf = {};
function makeOrangeLinks(element) {
// Get a list of pages and links to process.
var pagesToProcess = [];
var linksToProcess = [];
var links = element.querySelectorAll("a");
for (var i = 0; i < links.length; i++) {
var link = links[i];
// Check whether the link needs to be processed.
if (!link.href.match(/^https:\/\/(en|en\.m)\.wiktionary\.org\/wiki\//)) continue;
if (link.matches(".orange-link, .not-orange-link, .new, .external")) continue;
var linkParts = getTitleAndAnchor(link);
var linkTitle = linkParts[0];
var linkAnchor = linkParts[1];
if (!linkAnchor || /^[a-z]/.test(linkAnchor) || (new mw.Title(linkTitle)).namespace !== 0) continue;
pagesToProcess.push(linkTitle);
linksToProcess.push(link);
}
// Filter out duplicates.
var seen = {};
var uniquePages = [];
for (var j = 0; j < pagesToProcess.length; j++) {
if (!seen[pagesToProcess[j]]) {
seen[pagesToProcess[j]] = true;
uniquePages.push(pagesToProcess[j]);
}
}
pagesToProcess = uniquePages;
// Process the array in chunks.
var queries = [];
for (var k = 0; k < pagesToProcess.length; k += 100) {
(function(chunk) {
var wikitext = "{{#invoke:get IDs|show|" + chunk.join("|") + "}}";
var params = {
action: "expandtemplates",
format: "json",
prop: "wikitext",
text: wikitext
};
queries.push(actionAPI.post(params).then(function(response) {
// Integrate the results into `pageIDsOf`.
var pageIDs = HTML_unescape(response.expandtemplates.wikitext).split("\n\n");
for (var m = 0; m < chunk.length; m++) {
pageIDsOf[chunk[m]] = pageIDs[m].split(" ");
}
}));
})(pagesToProcess.slice(k, k + 100));
}
// After all the queries have returned, determine whether each link needs to be orange.
Promise.all(queries).then(function() {
for (var n = 0; n < linksToProcess.length; n++) {
var link = linksToProcess[n];
var linkParts = getTitleAndAnchor(link);
var linkTitle = linkParts[0];
var linkAnchor = linkParts[1];
if (pageIDsOf[linkTitle] && pageIDsOf[linkTitle].indexOf(linkAnchor) !== -1) {
link.classList.add("not-orange-link");
} else {
link.classList.add("orange-link");
}
}
});
}
// Activate the gadget.
var 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>