User:Irukaza/js/RecentchangesTagFilter.js
跳到导航
跳到搜索
注意:这类代码页面在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- 按住
CTRL+SHIFT+DEL 或 ⌘-Shift-R
来清除缓存! - 或尝试在地址栏的地址最后添加代码
?_=1
来访问最新页面。 - 你还可以在设置中勾选小工具在页面右上角添加清除缓存按钮!
// <pre>
"use strict";
$(() => (async () => {
if (mw.config.get("wgCanonicalSpecialPageName") !== "Recentchanges") {
return;
}
await mw.loader.using("mw.Api");
const uuidv4 = () => {
let result = "";
while (result === "" || $(`#${result}, [name="${result}"]`).length > 0) {
result = ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16),
);
}
return result;
};
const api = new mw.Api();
const container = $("<table/>");
container.html("<td>加载中……</td>").css("border-collapse", "collapse");
$(".tagfilterForm .mw-input").empty().append(container);
const tags = await (async () => {
const result = [];
try {
const cache = JSON.parse(localStorage.getItem("AnnTools-tags"));
if (!$.isPlainObject(cache)
|| typeof cache.timestamp !== "number" || cache.timestamp < new Date().getTime() - 24 * 60 * 60 * 1000
|| !Array.isArray(cache.tags)) {
throw new Error();
} else {
for (const tag of cache.tags) {
if (!("name" in tag && "displayname" in tag && "description" in tag)) {
throw new Error();
}
}
}
result.push(...cache.tags);
} catch {
const eol = Symbol();
let tgcontinue = undefined;
while (tgcontinue !== eol) {
const _result = await api.post({
action: "query",
list: "tags",
tglimit: "max",
tgprop: "displayname|description|active",
tgcontinue,
});
if (_result.continue) {
tgcontinue = _result.continue.tgcontinue;
} else {
tgcontinue = eol;
}
result.push(..._result.query.tags.filter((tag) => "active" in tag).map((tag) => {
delete tag.active;
return tag;
}));
}
localStorage.setItem("AnnTools-tags", JSON.stringify({
timestamp: new Date().getTime(),
tags: result,
}));
}
return result;
})();
const select = $("<select/>");
let defaultValue = uuidv4();
while (tags.includes(defaultValue)) {
defaultValue = uuidv4();
}
select.html(`<option selected value="${defaultValue}">未选择(选中的标签将被过滤)</option>`).attr({
autocomplete: "none",
multiple: "multiple",
});
tags.forEach(({ displayname, description, name }) => {
const option = $("<option/>");
option.text(displayname).attr({
value: name,
title: `${description} (${name})`,
});
select.append(option);
});
const selectTd = $("<td/>");
selectTd.css("vertical-align", "middle");
selectTd.append(select);
container.empty().append(selectTd);
const typeCheckbox = $("<input/>");
const typeId = uuidv4();
typeCheckbox.attr({
type: "checkbox",
id: typeId,
title: "勾选以隐藏未指定标签",
});
const typeLabel = $("<label/>");
typeLabel.text("只显示指定标签的编辑记录").attr({
"for": typeId,
title: "勾选以隐藏未指定标签",
});
const methodCheckbox = $("<input/>");
const methodId = uuidv4();
methodCheckbox.attr({
type: "checkbox",
id: methodId,
});
const methodLabel = $("<label/>");
const checkboxTd = $("<td/>");
checkboxTd.css("vertical-align", "bottom");
checkboxTd.append(methodCheckbox);
checkboxTd.append(methodLabel);
checkboxTd.append('<div style="padding-left: .25em">按住 Ctrl 键可多选,按住 Shift 键可批量选择所有项</div>');
checkboxTd.append(typeCheckbox);
checkboxTd.append(typeLabel);
container.append(checkboxTd);
$(".mw-changeslist-line").each((_, ele) => {
ele.dataset.tags = Array.from(ele.classList).filter((cls) => cls.startsWith("mw-tag-")).map((cls) => cls.replace("mw-tag-", "")).join("|");
});
mw.loader.addStyleTag(".hidden { display: none !important; }");
select.add(typeCheckbox).add(methodCheckbox).on("change", () => {
const type = typeCheckbox.prop("checked");
const method = methodCheckbox.prop("checked");
const tags = select.val().map((tag) => tag.replace(/ /g, "_"));
Array.from(document.getElementsByClassName("mw-changeslist-line")).forEach((ele) => {
const eleTags = ele.dataset.tags.split("|");
if (method) {
if (eleTags.filter((tag) => !tags.includes(tag)).length > 0) {
ele.classList[type ? "add" : "remove"]("hidden");
} else {
ele.classList[type ? "remove" : "add"]("hidden");
}
} else {
if (eleTags.filter((tag) => tags.includes(tag)).length > 0) {
ele.classList[type ? "remove" : "add"]("hidden");
} else {
ele.classList[type ? "add" : "remove"]("hidden");
}
}
});
});
})());
// </pre>