User:Opencooper/collapseBots.js: Difference between revisions
Appearance
Content deleted Content added
Opencooper (talk | contribs) maybe this will work |
Opencooper (talk | contribs) m forgot to update one |
||
(32 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
// Automatically collapse talk page sections from bots |
|||
// License: CC0 |
|||
function setup() { |
function setup() { |
||
// If we're not reading a talk page, do nothing |
// If we're not reading a talk page, do nothing |
||
Line 8: | Line 11: | ||
} |
} |
||
mw.loader. |
mw.loader.using("jquery.makeCollapsible", function() { |
||
var userMappings = {}; |
|||
$("#bodyContent h2").each(tryCollapse); |
|||
$(".mw-heading2").each(function() { |
|||
extractUsers(this, userMappings); |
|||
⚫ | |||
checkBotStatus(userMappings); |
|||
}); |
|||
} |
} |
||
function |
function extractUsers(header, userMappings) { |
||
var talkNodes = $( |
var talkNodes = $(header).nextUntil(".mw-heading2"); |
||
var lastSignature = $(talkNodes).find("a[href^='/wiki/User:']").last(); |
var lastSignature = $(talkNodes).find("a[href^='/wiki/User:']").last(); |
||
if (lastSignature.length) { |
if (lastSignature.length) { |
||
var username = lastSignature.text(); |
|||
checkBotStatus($(this), lastSignature.text()); |
|||
// Map each user with their sections |
|||
if (!(username in userMappings)) { |
|||
userMappings[username] = [header]; |
|||
} else { |
|||
userMappings[username].push(header); |
|||
} |
|||
} |
} |
||
} |
} |
||
function checkBotStatus( |
function checkBotStatus(userMappings) { |
||
var userList = Object.keys(userMappings); |
|||
// API docs: https://www.mediawiki.org/wiki/API:Users |
// API docs: https://www.mediawiki.org/wiki/API:Users |
||
$.ajax({ |
$.ajax({ |
||
Line 30: | Line 47: | ||
list: "users", |
list: "users", |
||
format: "json", |
format: "json", |
||
ususers: |
ususers: userList.join("|"), |
||
usprop: "groups" |
usprop: "groups" |
||
}, |
}, |
||
success: function(response) { |
success: function(response) { |
||
var |
var botList = []; |
||
var responseList = response.query.users; |
|||
⚫ | |||
for (var r of responseList) { |
|||
if (r.groups && r.groups.includes("bot")) { |
|||
console.log("User:Opencooper/collapseBots.js: " + username + " is a bot."); |
|||
botList.push(r.name); |
|||
} |
|||
} |
} |
||
filterUsers(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 |
|||
function filterUsers(userMappings, botList) { |
|||
var headers = []; |
|||
for (var bot of botList) { |
|||
for (var user in userMappings) { |
|||
if (bot.toLowerCase() == user.toLowerCase()) { |
|||
headers = headers.concat(userMappings[user]); |
|||
} |
|||
} |
|||
} |
|||
for (var h of headers) { |
|||
collapseSection(h); |
|||
} |
|||
} |
} |
||
// In-house method: https://www.mediawiki.org/wiki/Manual:Collapsible_elements |
// In-house method: https://www.mediawiki.org/wiki/Manual:Collapsible_elements |
||
function collapseSection(header) { |
function collapseSection(header) { |
||
var siblings = $(header).nextUntil(" |
var siblings = $(header).nextUntil(".mw-heading2, .collapseBots"); |
||
$(siblings).wrapAll("<div class='mw-collapsible-content'></div>"); |
$(siblings).wrapAll("<div class='mw-collapsible-content'></div>"); |
||
// Recompute since we just wrapped all the siblings |
|||
siblings = $(header).nextUntil("h2"); // recompute b/c of collapsible |
|||
siblings = $(header).next(".mw-collapsible-content"); |
|||
var section = $().add(header).add(siblings); |
var section = $().add(header).add(siblings); |
||
$(section).wrapAll("<div class='collapseBots mw-collapsed'></div>"); |
$(section).wrapAll("<div class='collapseBots mw-collapsed'></div>"); |
||
$( |
$(section).parent().makeCollapsible(); |
||
} |
} |
||
Latest revision as of 11:43, 14 March 2023
// Automatically collapse talk page sections from bots
// License: CC0
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 = {};
$(".mw-heading2").each(function() {
extractUsers(this, userMappings);
});
checkBotStatus(userMappings);
});
}
function extractUsers(header, userMappings) {
var talkNodes = $(header).nextUntil(".mw-heading2");
var lastSignature = $(talkNodes).find("a[href^='/wiki/User:']").last();
if (lastSignature.length) {
var username = lastSignature.text();
// Map each user with their sections
if (!(username in userMappings)) {
userMappings[username] = [header];
} else {
userMappings[username].push(header);
}
}
}
function checkBotStatus(userMappings) {
var userList = Object.keys(userMappings);
// 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 botList = [];
var responseList = response.query.users;
for (var r of responseList) {
if (r.groups && r.groups.includes("bot")) {
botList.push(r.name);
}
}
filterUsers(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
function filterUsers(userMappings, botList) {
var headers = [];
for (var bot of botList) {
for (var user in userMappings) {
if (bot.toLowerCase() == user.toLowerCase()) {
headers = headers.concat(userMappings[user]);
}
}
}
for (var h of headers) {
collapseSection(h);
}
}
// In-house method: https://www.mediawiki.org/wiki/Manual:Collapsible_elements
function collapseSection(header) {
var siblings = $(header).nextUntil(".mw-heading2, .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();