User:אוהב חכמה/y.js

const lang = "yi", sites = "enwiki"; function processDiffs { const elements = document.querySelectorAll('.diff-addedline'); const regex = /\[\[([^|\]]+)(?:\|[^|\]]+)?\]\]/g;

elements.forEach(element => {       const text = element.textContent;        const matches = text.match(regex);

if (matches) { matches.forEach(match => {               const a = match.slice(2, -2).split('|')[0];                const redirResolve = `https://en.wikipedia.org/w/api.php?action=query&titles=${encodeURIComponent(a)}&redirects=&format=json`;                fetch(redirResolve)                    .then((response) => response.json)                    .then((res) => { for (var p in res.query.pages) { if (p == -1) { break; }                           const title = res.query.pages[p].title; console.log(title); const wikiDataPropId = `https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&sites=${sites}&titles=${encodeURIComponent(                               title                            )}&props=sitelinks&languages=${lang}&sitefilter=yiwiki&utf8=1&origin=*`; fetch(wikiDataPropId) .then((response) => response.json) .then((result) => {                                   for (var e in result.entities) {                                        if (e == -1) {                                            break;                                        }                                        var labels = result.entities[e].sitelinks;                                        if (!labels.yiwiki) break;                                        var yiName = labels.yiwiki.title;                                        console.log("וויקיפעדיע: " + yiName);                                        const abbr = document.createElement("abbr");                                        abbr.textContent = ' Y';                                        abbr.title = yiName;                                        const sup = document.createElement("sup");                                        sup.appendChild(abbr); element.insertAdjacentElement('afterend', abbr); }                               });                        }                    });            });        }    }); }

function process { const divElement = document.querySelector("#mw-content-text"); const aElements = divElement.querySelectorAll("a[href^='/']");

aElements.forEach((a) => {       const redirResolve = `https://en.wikipedia.org/w/api.php?action=query&titles=${encodeURIComponent(a.title)}&redirects=&format=json`;        fetch(redirResolve)            .then((response) => response.json)            .then((res) => { for (var p in res.query.pages) { if (p == -1) { break; }                   const title = res.query.pages[p].title; console.log(title); const wikiDataPropId = `https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&sites=${sites}&titles=${encodeURIComponent(                       title                    )}&props=sitelinks&languages=${lang}&sitefilter=yiwiki&utf8=1&origin=*`; fetch(wikiDataPropId) .then((response) => response.json) .then((result) => {                           for (var e in result.entities) {                                if (e == -1) {                                    break;                                }                                var labels = result.entities[e].sitelinks;                                if (!labels.yiwiki) break;                                var yiName = labels.yiwiki.title;                                console.log("וויקיפעדיע: " + yiName);                                const abbr = document.createElement("abbr");                                abbr.textContent = ' Y';                                abbr.title = yiName;                                const sup = document.createElement("sup");                                sup.appendChild(abbr);                                a.appendChild(sup);                            }                        }); }           });    }); }

$(function {    if (mw.util.getParamValue('diff')) {        processDiffs;    } else if (mw.config.get('wgAction') == 'view') {        process;    } }); // Call