User:JBW/common.js
Appearance
< User:JBW
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. |
The accompanying .css page for this skin can be added at User:JBW/common.css. |
//Load Special:Massblock to use the mass block form.
importScript('User:Timotheus Canens/massblock.js')
//Mass move
mw.loader.load( '//en.wikipedia.org/w/index.php?title=User:Plastikspork/massmove.js&action=raw&ctype=text/javascript' ); //[[User:Plastikspork/massmove.js]]
mw.util.addPortletLink("p-tb", "//en.wikipedia.org/wiki/Special:Massmove", "Mass move", "p-massmove", "Mass move");
// =====≈==================
// Mark globally locked accounts
// Script from User:GeneralNotability/mark-locked.js
//
// <nowiki>
// @ts-check
// Companion to markblocked - asynchronously marks locked users
// 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 lockedUsers_getUsers($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/ (for userscripts)
const userTitleRX = new RegExp('^(' + userNS.join('|') + '|Special:Contrib(?:ution)?s\\/|Special:CentralAuth\\/)+([^\\/#]+)$', 'i');
const articleRX = new RegExp(mw.config.get('wgArticlePath').replace('$1', '') + '([^#]+)');
const redlinkRX = new RegExp(mw.config.get('wgScript') + '\\?title=([^#&]+)');
$('a', $content).each(function () {
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;
}
}
let pgTitle;
try {
pgTitle = decodeURIComponent(articleTitleReMatch[1]).replace(/_/g, ' ');
} catch (error) {
// Happens sometimes on non-username paths, like if there's a slash in the path
return;
}
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 a user is locked and if they are, return details about it
*
* @param {string} user Username to check
*
* @return {Promise<object>} Whether the user in question is locked and a formatted tooltip about the lock
*/
async function lockedUsers_getLock(user) {
let locked = false;
let tooltip = '';
user = user.charAt(0).toUpperCase() + user.slice(1);
const api = new mw.ForeignApi('https://meta.wikimedia.org/w/api.php');
try {
const response = await api.get({
action: 'query',
list: 'logevents',
leprop: 'user|timestamp|comment|details',
leaction: 'globalauth/setstatus',
letitle: `User:${user}@global`
});
// If the length is 0, then we couldn't find the log event
for (let logEvent of response.query.logevents) {
if (logEvent.params.added.includes('locked') || logEvent.params.includes('locked')) {
const timestamp = new Date(logEvent.timestamp);
const prettyTimestamp = lockedUsers_formatTimeSince(timestamp);
tooltip = `Locked by ${logEvent.user}: ${logEvent.comment} (${prettyTimestamp} ago)`;
locked = true;
break;
}
}
} catch (error) {}
return { locked, tooltip };
}
/**
* Formats time since a date. Taken from mark-blocked.js
*
* @param {targetDate} Date to check the time since for
*
* @return {string} A prettified string regarding time since the lock occured
*/
function lockedUsers_formatTimeSince(targetDate) {
const lockedUsers_padNumber = (number) => number <= 9 ? '0' + number : number;
const msSince = new Date() - targetDate;
let minutes = Math.floor(msSince / 60000);
if (!minutes) {
return Math.floor(msSince / 1000) + 's';
}
let hours = Math.floor(minutes / 60);
minutes %= 60;
let days = Math.floor(hours / 24);
hours %= 24;
if (days) {
return `${days}${(days < 10 ? '.' + lockedUsers_padNumber(hours) : '' )}d`;
}
return `${hours}:${lockedUsers_padNumber(minutes)}`;
}
// 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 = lockedUsers_getUsers($content);
usersOnPage.forEach(async (val, key, _) => {
const { locked, tooltip } = await lockedUsers_getLock(key);
if (locked) {
val.forEach(($link) => {
$link.css({ opacity: 0.4, 'border-bottom-size': 'thick', 'border-bottom-style': 'dashed', 'border-bottom-color': 'red' });
$link.attr('title', tooltip);
});
}
});
});
});
// </nowiki>