MediaWiki:Gadget-OrangeLinks.js: Difference between revisions

No edit summary
Tag: Reverted
m 1 revision imported
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* eslint-env es5, browser, jquery */
/* eslint semi: "error" */
/* jshint esversion: 5, eqeqeq: true */
/* globals $, mw */
/* requires mw.cookie, mw.storage */
// <nowiki>
// <nowiki>
window.$ = window.jQuery;
var actionAPI = new mw.Api({ ajax: { headers: { "Api-User-Agent": "Gadget developed by [[User:Ioaxxere]]" } } });
function HTML_unescape(text) {
function HTML_unescape(text) {
return new DOMParser().parseFromString(text, "text/html").body.textContent;
return new DOMParser().parseFromString(text, "text/html").body.textContent;
Line 14: Line 5:


function getTitleAndAnchor(link) {
function getTitleAndAnchor(link) {
var linkTitle = decodeURIComponent(link.pathname.split("/wiki/")[1]);
let linkTitle = decodeURIComponent(link.pathname.split("/wiki/")[1]);
var linkAnchor = decodeURIComponent(link.hash.slice(1) || "");
let linkAnchor = decodeURIComponent(link.hash.slice(1) || "");
return [linkTitle, linkAnchor];
return [linkTitle, linkAnchor];
}
}


mw.util.addCSS(
mw.util.addCSS(`
".orange-link { color: var(--wikt-palette-gold, #b88d00); }" +
.orange-link {
".orange-link:visited { color: var(--wikt-palette-dullgold, #826f34); }" +
color: var(--wikt-palette-gold);
".orange-link:hover, .orange-link:visited:hover { color: var(--wikt-palette-honey, #81540e); }"
}
);
.orange-link:visited {
color: var(--wikt-palette-dullgold);
}
.orange-link:hover, .orange-link:visited:hover {
color: var(--wikt-palette-honey);
}
`);


// Use plain object instead of Map
let actionAPI = new mw.Api({ajax: {headers: {"Api-User-Agent": "Gadget developed by [[User:Ioaxxere]]"}}});
var pageIDsOf = {};


function makeOrangeLinks(element) {
// Maps each page to a list of IDs.
let pageIDsOf = new Map();
 
async function makeOrangeLinks(element) {
// Get a list of pages and links to process.
// Get a list of pages and links to process.
var pagesToProcess = [];
let pagesToProcess = [];
var linksToProcess = [];
let linksToProcess = [];
var links = element.querySelectorAll("a");
for (let link of 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.match(/^https:\/\/(en|en\.m)\.wiktionary\.org\/wiki\//)) continue;
if (!link.href.startsWith("https://en.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);
let [linkTitle, linkAnchor] = 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)) continue;
 
if (![0, 100, 118].includes(new mw.Title(linkTitle).namespace)) continue; // Main, Appendix, Reconstruction


pagesToProcess.push(linkTitle);
pagesToProcess.push(linkTitle);
Line 52: Line 47:


// Filter out duplicates.
// Filter out duplicates.
var seen = {};
pagesToProcess = [...new Set(pagesToProcess)];
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 = [];
let queries = [];
for (var k = 0; k < pagesToProcess.length; k += 100) {
for (let i = 0; i < pagesToProcess.length; i += 100) {
(function(chunk) {
let chunk = pagesToProcess.slice(i, i + 100);
var wikitext = "{{#invoke:get IDs|show|" + chunk.join("|") + "}}";
 
var params = {
// Query the IDs for all the pages using [[Module:get IDs]].
action: "expandtemplates",
let params = {
format: "json",
action: "expandtemplates",
prop: "wikitext",
format: "json",
text: wikitext
prop: "wikitext",
};
text: `{{#invoke:get IDs|show|${chunk.join("|")}}}`
};


queries.push(actionAPI.post(params).then(function(response) {
queries.push(actionAPI.post(params).then(response => {
// Integrate the results into `pageIDsOf`.
// Integrate the results into `pageIDsOf`.
var pageIDs = HTML_unescape(response.expandtemplates.wikitext).split("\n\n");
let pageIDs = HTML_unescape(response.expandtemplates.wikitext).split("\n\n");
for (var m = 0; m < chunk.length; m++) {
for (let j = 0; j < chunk.length; j++)
pageIDsOf[chunk[m]] = pageIDs[m].split(" ");
pageIDsOf.set(chunk[j], pageIDs[j].split(" "));
}
}));
}));
})(pagesToProcess.slice(k, k + 100));
}
}
await Promise.all(queries);


// 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(function() {
for (let link of linksToProcess) {
for (var n = 0; n < linksToProcess.length; n++) {
let [linkTitle, linkAnchor] = getTitleAndAnchor(link);
var link = linksToProcess[n];
let anchorExists = pageIDsOf.get(linkTitle).includes(linkAnchor);
var linkParts = getTitleAndAnchor(link);
link.classList.add(anchorExists ? "not-orange-link" : "orange-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.
// Activate the gadget.
var pageContent = document.querySelector(".mw-parser-output");
let 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>