User:Mdaniels5757/mark-global-blocked.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. |
Documentation for this user script can be added at User:Mdaniels5757/mark-global-blocked. |
// <nowiki>
// @ts-check
// BETA!
// Companion to markblocked and marklocked - asynchronously marks gblocked IPs
// Derived from [[User:GeneralNotability/mark-locked.js]];
// Chunks borrowed from [[User:Krinkle/Scripts/CVNSimpleOverlay_wiki.js]],
// [[User:GeneralNotability/ip-ext-info.js]], and [[MediaWiki:Gadget-markblocked.js]]
/**
* Get all userlinks on the page
*
* @param {JQuery} $content page contents
* @return {Map} list of unique users on the page and their corresponding links
*/
function gblockedIPs_getIPs($content) {
const userLinks = new Map();
// Get all aliases for user: & user_talk: (taken from markblocked)
const userNS = [];
for ( const ns in mw.config.get( 'wgNamespaceIds' ) ) {
if ( mw.config.get( 'wgNamespaceIds' )[ns] === 2 || mw.config.get( 'wgNamespaceIds' )[ns] === 3 ) {
userNS.push( mw.util.escapeRegExp(ns.replace( /_/g, ' ' )) + ':' );
}
}
// RegExp for all titles that are User:| User_talk: | Special:Contributions/ | Special:Contribs/ | Special:CentralAuth/ | Special:GlobalBlockList/
const userTitleRX = new RegExp('^(' + userNS.join('|') + '|Special:Contrib(?:ution)?s\\/|Special:CentralAuth\\/|Special:GlobalBlockList\\/)+([^#]+)$', 'i'); $('a', $content).each(function () {
const articleRX = new RegExp(mw.config.get('wgArticlePath').replace('$1', '') + '([^#]+)');
const redlinkRX = new RegExp('/w/index.php?title=([^#&]+)');
if (!$(this).attr('href')) {
// Ignore if the <a> doesn't have a href
return;
}
let articleTitleReMatch = articleRX.exec($(this).attr('href').toString());
if (!articleTitleReMatch) {
// Try the redlink check
articleTitleReMatch = redlinkRX.exec($(this).attr('href').toString());
if (!articleTitleReMatch) {
return;
}
}
const pgTitle = decodeURIComponent(articleTitleReMatch[1]).replace(/_/g, ' ');
const userTitleReMatch = userTitleRX.exec(pgTitle);
if (!userTitleReMatch) {
return;
}
const username = userTitleReMatch[2];
if (mw.util.isIPAddress(username, true)) {
if (!userLinks.get(username)) {
userLinks.set(username, []);
}
userLinks.get(username).push($(this));
}
});
return userLinks;
}
/**
* Check whether an IP is globally blocked
*
* @param {string} IP to check
*
* @return {Promise<boolean>} Whether the IP in question is gblocked
*/
async function gblockedUsers_isGBlocked(input) {
const api = new mw.Api();
try {
const response = await api.get({
action: 'query',
list: 'globalblocks',
bglimit: '1',
bgip: input,
bgprop: 'address'
});
if (response.query.globalblocks.length === 0) {
// If the length is 0, then no block
return false;
}
// If there is a length, then the IP is gblocked
return true;
} catch (error) {
return false;
}
}
// On window load, get all the users on the page and check if they're blocked
$.when( $.ready, mw.loader.using( 'mediawiki.util' ) ).then( function () {
mw.hook('wikipage.content').add(function ($content) {
const usersOnPage = gblockedIPs_getIPs($content);
usersOnPage.forEach(async (val, key, _) => {
const userGBlocked = await gblockedUsers_isGBlocked(key);
if (userGBlocked) {
val.forEach(($link) => {
$link.css({ opacity: 0.4, 'border-bottom-size': 'thick', 'border-bottom-style': 'dashed', 'border-bottom-color': 'red' });
});
}
});
});
});
// </nowiki>