User:Irukaza/js/queryWhatlinkshere.js
跳到导航
跳到搜索
注意:这类代码页面在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- 按住
CTRL+SHIFT+DEL 或 ⌘-Shift-R来清除缓存! - 或尝试在地址栏的地址最后添加代码
?_=1来访问最新页面。 - 你还可以在设置中勾选小工具在页面右上角添加清除缓存按钮!
// 开头的判断条件如有更新需要同步到本体里
// <pre>
"use strict";
$(() => (async () => {
const wgRelevantPageName = mw.config.get("wgRelevantPageName");
await mw.loader.using("mediawiki.api");
const upperFirstCase = (s) => /^[a-z]/.test(s) ? s.substring(0, 1).toUpperCase() + s.substring(1) : s;
const api = new mw.Api();
const nsids = {
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 wgNamespaceIds = {};
for (const [ns, nsid] of Object.entries(mw.config.get("wgNamespaceIds"))) {
if (!Array.isArray(wgNamespaceIds[nsid])) {
wgNamespaceIds[nsid] = [ns];
} else {
wgNamespaceIds[nsid].push(ns);
}
}
const pageinfo = Object.values((await api.post({
action: "query",
format: "json",
prop: "info",
titles: wgRelevantPageName,
})).query.pages)[0];
const target = pageinfo.title;
const ns = pageinfo.ns;
const title = target.replace(ns === 0 ? "" : RegExp(`^(?:${wgNamespaceIds[ns].join("|")}):`, "i"), "");
$("#mw-content-text > form").after(`<fieldset><legend>链入情况</legend><p id="queryWhatlinkshere">……</p></fieldset>`);
const queryWhatlinkshere = $("#queryWhatlinkshere");
const queryWhatembeddedin = $("#queryWhatembeddedin");
const whatlinkshere = $("#whatlinkshere");
const whatembeddedin = $("#whatembeddedin");
queryWhatlinkshere.on("click", async () => {
queryWhatlinkshere.hide();
whatlinkshere.show();
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;
whatlinkshere.append("…");
} else {
lhcontinue = eol;
}
result.push(...Object.values(_result.query.pages)[0].linkshere || []);
}
return result;
})();
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 };
whatlinkshere.text(`共有${list.length}个页面含有到本页面的站内链接(不考虑嵌入),其中有${list.filter((item) => "redirect" in item).length}个重定向页面。`);
if (list.length > 0) { whatlinkshere.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>${upperFirstCase(nsids[nsnumber])}:${count}个页面(其中有${redirect}个重定向页面)`));
whatlinkshere.after(ul);
});
queryWhatembeddedin.on("click", async () => {
queryWhatembeddedin.hide();
whatembeddedin.show();
const list = await (async () => {
const result = {
redirects: [],
all: [],
};
const eol = Symbol();
let eicontinue = undefined;
while (eicontinue !== eol) {
const _result = await api.post({
action: "query",
format: "json",
list: "embeddedin",
eititle: target,
einamespace: "*",
eifilterredir: "redirects",
eicontinue,
eilimit: "max",
});
if (_result.continue) {
eicontinue = _result.continue.eicontinue;
whatembeddedin.append("…");
} else {
eicontinue = eol;
}
result.redirects.push(..._result.query.embeddedin);
result.all.push(..._result.query.embeddedin);
}
eicontinue = undefined;
while (eicontinue !== eol) {
const _result = await api.post({
action: "query",
format: "json",
list: "embeddedin",
eititle: target,
einamespace: "*",
eifilterredir: "nonredirects",
eicontinue,
eilimit: "max",
});
if (_result.continue) {
eicontinue = _result.continue.eicontinue;
whatembeddedin.append("…");
} else {
eicontinue = eol;
}
result.all.push(..._result.query.embeddedin);
}
return result;
})();
const nslist = {
0: { count: 0, redirect: [] },
1: { count: 0, redirect: [] },
2: { count: 0, redirect: [] },
3: { count: 0, redirect: [] },
4: { count: 0, redirect: [] },
5: { count: 0, redirect: [] },
6: { count: 0, redirect: [] },
7: { count: 0, redirect: [] },
8: { count: 0, redirect: [] },
9: { count: 0, redirect: [] },
10: { count: 0, redirect: [] },
11: { count: 0, redirect: [] },
12: { count: 0, redirect: [] },
13: { count: 0, redirect: [] },
14: { count: 0, redirect: [] },
15: { count: 0, redirect: [] },
274: { count: 0, redirect: [] },
275: { count: 0, redirect: [] },
710: { count: 0, redirect: [] },
711: { count: 0, redirect: [] },
828: { count: 0, redirect: [] },
829: { count: 0, redirect: [] },
2300: { count: 0, redirect: [] },
2301: { count: 0, redirect: [] },
2302: { count: 0, redirect: [] },
2303: { count: 0, redirect: [] },
};
const global = { redirect: 0 };
whatembeddedin.text(`共有${list.all.length}个页面嵌入了本页面,其中有${list.redirects.length}个重定向页面。`);
if (list.all.length > 0) { whatembeddedin.append("按名字空间划分如下:"); }
const ul = $("<ul/>");
list.all.forEach(({ ns }) => {
nslist[ns].count++;
});
list.redirects.forEach(({ ns, title }) => {
nslist[ns].redirect.push(title);
global.redirect++;
});
Object.entries(nslist).filter(([, { count }]) => count > 0).sort(([a], [b]) => a - b).forEach(([nsnumber, { count, redirect }]) => ul.append(`<li class="mw-parser-output">${upperFirstCase(nsids[nsnumber])}:${count}个页面${redirect.length > 0 ? `(其中有${redirect.length}个重定向页面:${redirect.map((title) => `<a target="_blank" rel="nofollow noreferrer noopener" class="external text" href="https://zh.moegirl.org.cn/index.php?title=${encodeURIComponent(title)}&redirect=no">${title}</a>`).join("、")})` : ""}`));
whatembeddedin.after(ul);
});
})());
// </pre>