2023年政策修订增补工作正在进行中,欢迎参与!
User:AnnAngela/js/watchScripts.js
< User:AnnAngela | js
注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox/Safari:按住“Shift”的同时单击“刷新”,或按“Ctrl-F5”或“Ctrl-R”(Mac为“⌘-R”)
- Google Chrome:按“Ctrl-Shift-R”(Mac为“⌘-Shift-R”)
- Internet Explorer:按住“Ctrl”的同时单击“刷新”,或按“Ctrl-F5”
- Opera:在“工具→首选项”中清除缓存
- 如果您已登录但该页面出现未登录状态,请尝试在地址栏的地址最后添加代码
?_=1
来访问最新页面。 - 添加代码后的本页地址如下:
-{R|https://moegirl.icu/User:AnnAngela/js/watchScripts.js?_=1}-
// <pre>
"use strict";
$(() => (async () => {
await mw.loader.using(["mediawiki.api", "ext.gadget.LocalObjectStorage", "mediawiki.user"]);
const localObjectStorage = new LocalObjectStorage("watchScripts");
const lastTime = localObjectStorage.getItem("lastUpdate", -1);
const now = Date.now();
if (typeof lastTime === "number" && now - lastTime < 86400000) {
return;
}
localObjectStorage.setItem("lastUpdate", now);
const api = new mw.Api();
const existWatchlist = await (async () => {
const result = [];
let wrcontinue;
while (Number.MAX_SAFE_INTEGER > Number.MIN_SAFE_INTEGER) {
const r = await api.post({
action: "query",
list: "watchlistraw",
wrnamespace: "8|274",
wrlimit: "max",
wrcontinue,
});
for (const { ns, title } of r.watchlistraw) {
if (ns === 274 || /\.(?:j|cs)s$/.test(title)) {
result.push(title);
}
}
if (r.continue) {
wrcontinue = r.continue.wrcontinue;
} else {
break;
}
}
return result;
})();
const scriptPages = await (async () => {
const result = [];
let apcontinue;
while (Number.MAX_SAFE_INTEGER > Number.MIN_SAFE_INTEGER) {
const r = await api.post({
action: "query",
list: "allpages",
apnamespace: "8",
aplimit: "max",
apcontinue,
});
for (const { title } of r.query.allpages) {
if (/\.(?:j|cs)s$/.test(title)) {
result.push(title);
}
}
if (r.continue) {
apcontinue = r.continue.apcontinue;
} else {
break;
}
}
apcontinue = undefined;
while (Number.MAX_SAFE_INTEGER > Number.MIN_SAFE_INTEGER) {
const r = await api.post({
action: "query",
list: "allpages",
apnamespace: "274",
aplimit: "max",
apcontinue,
});
for (const { title } of r.query.allpages) {
result.push(title);
}
if (r.continue) {
apcontinue = r.continue.apcontinue;
} else {
break;
}
}
return result;
})();
const needToBeWatched = scriptPages.filter((page) => !existWatchlist.includes(page));
console.info("existWatchlist", existWatchlist);
console.info("scriptPages", scriptPages);
console.info("needToBeWatched", needToBeWatched);
if (needToBeWatched.length === 0) {
return;
}
const userRights = await mw.user.getRights();
const titlesPerRequest = userRights.includes("apihighlimits") ? 500 : 50;
console.info("titlesPerRequest", titlesPerRequest);
for (let i = 0; i * titlesPerRequest < needToBeWatched.length; i++) {
const start = i * titlesPerRequest;
const end = (i + 1) * titlesPerRequest;
const titles = needToBeWatched.slice(start, end);
console.info("start", start, "end", end, "titles", titles);
console.info(await api.postWithToken("watch", {
action: "watch",
titles: titles.join("|"),
redirects: true,
}));
}
})());