User:Opencooper/collapseBots.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Opencooper/collapseBots. |
// TODO: Need to cache the lookups, possibly long-term
function setup() {
// If we're not reading a talk page, do nothing
if (!(mw.config.get('wgAction') === 'view'
&& mw.config.get('wgIsArticle')
&& !location.search.split('oldid=')[1] // Old revision
&& mw.config.get('wgNamespaceNumber') % 2 == 1)) { // talk namespaces have odd numbers
return;
}
mw.loader.using("jquery.makeCollapsible", function() {
var userMappings = {};
$("#bodyContent h2").each(function() {
extractSignatures(this, userMappings);
});
var botList = [];
getBotStatus(userMappings, botList);
// Look for the bots in our mappings
// More convoluted than necessary because API returns usernames in its
// own case, while we use them as keys
var headers = [];
for (var bot of botList) {
for (var user in userMappings) {
if (bot.toLowerCase() == user.toLowerCase()) {
headers.push(userMappings[user]);
}
}
}
for (var i = 0; i < headers.length; i++) {
collapseSection(headers[i]);
}
});
}
function extractSignatures(header, userMappings) {
var talkNodes = $(header).nextUntil("h2");
var lastSignature = $(talkNodes).find("a[href^='/wiki/User:']").last();
if (lastSignature.length) {
var username = lastSignature.text();
// We map each user with their sections so we don't have to do
// duplicate lookups
if (!(username in userMappings)) {
userMappings[username] = [header];
} else {
userMappings[username].push(header);
}
}
}
var counter = 0;
function getBotStatus(userMappings, botList) {
var userList = [];
for (var user in userMappings) {
userList.push(user);
}
// API docs: https://www.mediawiki.org/wiki/API:Users
$.ajax({
url: "https://en.wikipedia.org/w/api.php",
data: {
action: "query",
list: "users",
format: "json",
ususers: userList.join("|"),
usprop: "groups"
},
success: function(response) {
var responseList = response.query.users;
for (var r of responseList) {
if (r.groups && r.groups.includes("bot")) {
// console.log("User:Opencooper/collapseBots.js: " + r.name + " is a bot.");
botList.push(r.name);
}
}
}
});
}
// In-house method: https://www.mediawiki.org/wiki/Manual:Collapsible_elements
function collapseSection(header) {
var siblings = $(header).nextUntil("h2, .collapseBots");
$(siblings).wrapAll("<div class='mw-collapsible-content'></div>");
// Recompute since we just wrapped all the siblings
siblings = $(header).next(".mw-collapsible-content");
var section = $().add(header).add(siblings);
$(section).wrapAll("<div class='collapseBots mw-collapsed'></div>");
$(section).parent().makeCollapsible();
}
setup();