User:Irukaza/js/queryWhatlinkshere.js

H萌娘,万物皆可H的百科全书!
< User:Irukaza
imported>Irukaza2022年2月21日 (一) 19:05的版本
跳到导航 跳到搜索

注意:这类代码页面在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

// <pre>
"use strict";
$(() => (async () => {
    if (mw.config.get("wgCanonicalSpecialPageName") !== "Whatlinkshere") {
        return;
    }
    const target = (document.querySelector("#mw-whatlinkshere-target") || {}).value;
    if (typeof target !== "string" || target.length === 0) {
        return;
    }
    await mw.loader.using("mw.Api");
    const upperFirstCase = (s) => /^[a-z]/.test(s) ? s.substring(0, 1).toUpperCase() + s.substring(1) : s;
    const api = new mw.Api();
    const p = $('<fieldset><legend>链入情况 | <a href="/Special:Search/${encodeURIComponent(`linksto:"${target}" insource:"${target}"`)}" target="_blank" class="external text">通过搜索查找链入</a></legend><p id="queryWhatlinkshere">……</p></fieldset>').insertAfter("#mw-content-text > form").find("#queryWhatlinkshere");
    const list = await (async () => {
        const result = [];
        const eol = Symbol();
        let lhcontinue = undefined;
        while (lhcontinue !== eol) {
            const _result = await api.post({
                action: "query",
                format: "json",
                prop: "linkshere",
                titles: target,
                lhprop: "redirect|title|pageid",
                lhcontinue,
                lhlimit: "max",
            });
            if (_result.continue) {
                lhcontinue = _result.continue.lhcontinue;
            } else {
                lhcontinue = eol;
            }
      result.push(...(Object.values(_result.query.pages)[0].linkshere || []));
        }
        return result;
    })();
    const ns = {
        0: "条目",
        1: "讨论",
        2: "用户",
        3: "用户讨论",
        4: "H萌娘",
        5: "H萌娘讨论",
        6: "文件",
        7: "文件讨论",
        8: "mediawiki",
        9: "mediawiki讨论",
        10: "模板",
        11: "模板讨论",
        12: "帮助",
        13: "帮助讨论",
        14: "分类",
        15: "分类讨论",
        274: "widget",
        275: "widget_talk",
        710: "timedtext",
        711: "timedtext_talk",
        828: "模块",
        829: "模块讨论",
        2300: "gadget",
        2301: "gadget_talk",
        2302: "gadget_definition",
        2303: "gadget_definition_talk",
    };
    const nslist = {
        0: { count: 0, redirect: 0 },
        1:  { count: 0, redirect: 0 },
        2:  { count: 0, redirect: 0 },
        3:  { count: 0, redirect: 0 },
        4:  { count: 0, redirect: 0 },
        5:  { count: 0, redirect: 0 },
        6:  { count: 0, redirect: 0 },
        7:  { count: 0, redirect: 0 },
        8:  { count: 0, redirect: 0 },
        9:  { count: 0, redirect: 0 },
        10:  { count: 0, redirect: 0 },
        11:  { count: 0, redirect: 0 },
        12:  { count: 0, redirect: 0 },
        13:  { count: 0, redirect: 0 },
        14:  { count: 0, redirect: 0 },
        15:  { count: 0, redirect: 0 },
        274:  { count: 0, redirect: 0 },
        275:  { count: 0, redirect: 0 },
        710:  { count: 0, redirect: 0 },
        711:  { count: 0, redirect: 0 },
        828:  { count: 0, redirect: 0 },
        829:  { count: 0, redirect: 0 },
        2300:  { count: 0, redirect: 0 },
        2301:  { count: 0, redirect: 0 },
        2302:  { count: 0, redirect: 0 },
        2303:  { count: 0, redirect: 0 },
    };
    const global = { redirect: 0 };
    p.text(`共有${list.length}个页面含有到本页面的站内链接,其中有${list.filter((item) => "redirect" in item).length}个重定向页面。`);
   if (list.length > 0) { p.append("按名字空间划分如下:"); }
    const ul = $("<ul/>");
    list.forEach((item) => {
        nslist[item.ns].count++;
        if ("redirect" in item) {
            nslist[item.ns].redirect++;
            global.redirect++;
        }
    });
    Object.entries(nslist).filter(([, { count }]) => count > 0).sort(([a], [b]) => a - b).forEach(([nsnumber, { count, redirect }]) => ul.append(`<li>${nsnumber === 0 ? "(主名字空间)" : upperFirstCase(ns[nsnumber])}${count}个页面(其中有${redirect}个重定向页面)`));
    p.after(ul);
})());
// </pre>