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

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
const lang = "yi", sites = "hewiki";

function processElements(elmnt, position) {
    const elements = document.querySelectorAll(elmnt);
    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://he.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(position, abbr);
                                    }
                                });
                        }
                    });
            });
        }
    });
}

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

    aElements.forEach((a) => {
        const redirResolve = `https://he.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')) {
        processElements('.diff-addedline', 'beforeend');
    } else if (mw.config.get('wgAction') == 'view') {
        process();
        if (mw.config.get('wgCanonicalSpecialPageName') == 'Search') {
            processElements('div.searchresult', 'afterend');
        } else if (mw.config.get('wgNamespaceNumber') == 1 && document.body.textContent.includes("מיזם תאריכים עבריים")) {
        	processElements('li', 'beforeend');
        }
    }
}); // Call