User:ZeScript/userTitle.js
来自滚动的天空Wiki
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
(function Title() {
var api = new mw.Api();
var data = {};
function group(name) {
if (!(name in group)) {
return undefined;
}
return group[name];
}
$.extend(group, {
sysop: `<svg width="20" height="20" xmlns="http://www.w3.org/2000/svg">
<g id="Layer_1">
<title>Layer 1</title>
<ellipse ry="5" rx="5" id="svg_3" cy="4.8625" cx="5" stroke="#000" fill="#B1FFFF"/>
<ellipse ry="5" rx="5" id="svg_4" cy="15" cx="15" stroke="#000" fill="#FFAC8D"/>
<rect stroke="#000" id="svg_1" height="10" width="14.9375" y="0" x="5" stroke-width="0" fill="#B1FFFF"/>
<rect stroke="#000" id="svg_2" height="10" width="14.9375" y="10" x="0.0625" stroke-width="0" fill="#FFAC8D"/>
</g>
</svg>`,
bureaucrat: `<svg width="20" height="20" xmlns="http://www.w3.org/2000/svg">
<g id="Layer_1">
<title>Layer 1</title>
<rect fill="none" stroke="#000" stroke-width="0" x="0" y="0" width="15" height="10" id="svg_1"/>
<rect stroke-width="0" id="svg_2" height="10" width="15" y="0" x="0" stroke="#000" fill="#CE7975"/>
<rect stroke-width="0" id="svg_3" height="10" width="15" y="10" x="0" stroke="#000" fill="#8CD79F"/>
<ellipse ry="5" rx="5" id="svg_4" cy="5" cx="15" stroke-width="0" stroke="#000" fill="#CE7975"/>
<ellipse ry="5" rx="5" id="svg_5" cy="15" cx="15" stroke-width="0" stroke="#000" fill="#8CD79F"/>
</g>
</svg>`,
"interface-admin": `<svg width="20" height="20" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Layer 1</title>
<rect transform="rotate(0.0521587 387.5 301.5)" stroke="#000" id="svg_1" height="319" width="304.99998" y="142" x="235" fill="#fff"/>
<line stroke-width="4" id="svg_8" y2="20" x2="10" y1="0" x1="10" stroke="#000" fill="none"/>
<line stroke-width="4" id="svg_9" y2="2" x2="20" y1="2" x1="0" stroke="#000" fill="none"/>
<line id="svg_10" y2="198" x2="200" y1="198" x1="0" stroke-width="4" stroke="#000" fill="none"/>
<line id="svg_11" y2="18" x2="20" y1="18" x1="0" stroke-width="4" stroke="#000" fill="none"/>
</g>
</svg>`
});console.log("breakpoint 0");
function fetchGroups() {
api.get({
"action": "query",
"format": "json",
"list": "allusers",
"formatversion": "2",
"auprop": "groups",
"aulimit": "500"
}, {"async": false}).done(function (result) {
$.each(result.query.allusers, function (_, obj) {
data[obj.name.replace(/ /g, "_")] = obj.groups.reverse();
});
localStorage.userGroups = JSON.stringify(data);
});
}
function run() {// console.log("breakpoint 5")
$("a").each(function (_, link) {
var href = link.href;
if (!href || !href.startsWith("https:")) {
return;
}
var url = new URL(href);
var user, match;
var path = url.pathname;
if (path === "/w/index.php") {
page = decodeURI(url.searchParams.get("title"))
match = page.match(/^User\:([^\/]+)$/) || page.match(/^用户\:([^\/]+)$/);
} else {
page = decodeURI(path)
match = page.match(/^\/wiki\/User\:([^\/]+)$/) || page.match(/^\/wiki\/用户\:([^\/]+)$/);
}
if (match) {
user = match[1];
} // console.log("breakpoint 2", link);
if (!user || user !== link.innerText.replace(/ /g, "_")) {
return;
} // console.log("breakpoint 3", link);
if (!(user in data)) {
fetchGroups();
if(!(user in data)) {
return;
}
}
var groups = data[user]; // console.log("breakpoint 1");
var titles = $("<span/>").insertBefore(link);
$.each(groups, function (_, g) {
var t = group(g);
if (t) {
titles.append(t);
titles.append(" ");
}
});
});
}
function main() {
if (localStorage.userGroups) {
data = JSON.parse(localStorage.userGroups);
run();
} else {
fetchGroups();
run();
} scriptLoader.send("userTitle", 1);
}
$(main);
})();