User:Awesome Aasim/usergroups.js
//
function isIPV4Address(ip) {
let digits = ip.split(".");
if (digits.length != 4) {
return false;
}
// for last digit see if it is a range
let r0 = digits[3].split("/");
let range;
if (r0.length > 2) {
return false;
} else {
digits[3] = r0[0];
if (r0.length == 2) range = r0[1];
else range = 0
}
// if any digits are empty string return false
if (digits[0] === || digits[1] === || digits[2] === || digits[3] === || range === '') {
return false;
}
return digits[0] < 256 && digits[0] >= 0
&& digits[1] < 256 && digits[1] >= 0
&& digits[2] < 256 && digits[2] >= 0
&& digits[3] < 256 && digits[3] >= 0
&& range <= 32 && range >= 0;
}
function isIPV6Address(ip) {
let digits = ip.split(":");
if (digits.length > 8) {
return false;
}
// for last digit see if it is a range
let r0 = digits[digits.length - 1].split("/");
let range;
if (r0.length > 2) {
return false;
} else {
digits[digits.length - 1] = r0[0];
if (r0.length == 2) range = r0[1];
else range = 0;
}
for (let i = 0; i < digits.length; i++) {
if (digits[i] === "") {
let differenceInCIDRlength = 8 - digits.length;
for (let j = 0; j < differenceInCIDRlength; j++) {
digits.push("");
}
for (let j = digits.length - 1; j >= i; j--) {
digits[j] = digits[j - differenceInCIDRlength];
digits[j - differenceInCIDRlength] = "0";
}
digits[i] = "0";
break;
}
}
return "0x" + digits[0] < 65536 && "0x" + digits[0] >= 0
&& "0x" + digits[1] < 65536 && "0x" + digits[1] >= 0
&& "0x" + digits[2] < 65536 && "0x" + digits[2] >= 0
&& "0x" + digits[3] < 65536 && "0x" + digits[3] >= 0
&& "0x" + digits[4] < 65536 && "0x" + digits[4] >= 0
&& "0x" + digits[5] < 65536 && "0x" + digits[5] >= 0
&& "0x" + digits[6] < 65536 && "0x" + digits[6] >= 0
&& "0x" + digits[7] < 65536 && "0x" + digits[7] >= 0
&& range <= 128 && range >= 0;
}
function isIP(ip) {
return isIPV6Address(ip) || isIPV4Address(ip)
}
mw.loader.using([ 'mediawiki.api', 'mediawiki.jqueryMsg' ], function() {
mw.loader.load(`${window.location.origin + mw.config.get("wgScriptPath")}/index.php?title=User:Awesome_Aasim/usergroups.css&action=raw&ctype=text/css`, "text/css");
mw.loader.load(`${window.location.origin + mw.config.get("wgScriptPath")}/index.php?title=MediaWiki:Usergroups.css&action=raw&ctype=text/css`, "text/css");
if (!window.userGroupsLoaded) {
window.userGroupsLoaded = true;
window.userGroupsCache = {};
$(document).ready(async function() {
let user;
if (mw.config.get("wgNamespaceNumber") == 2 || mw.config.get("wgNamespaceNumber") == 3) {
user = mw.config.get("wgTitle").split("/")[0];
} else if (mw.config.get("wgNamespaceNumber") == -1) {
user = mw.config.get("wgTitle").split("/")[1] ? mw.config.get("wgTitle").split("/")[1] : null;
}
if (user) {
let res = await $.get(mw.config.get('wgScriptPath') + '/api.php', {
action: "query",
list: "users|blocks",
ususers: user,
usprop: "groups|blockinfo",
bkusers: isIP(user) ? undefined : user,
bkip: isIP(user) ? user : undefined,
format: "json"
});
if (!res.error) {
var userdata = res.query.users[0];
var blockdata = res.query.blocks.length > 0 ? res.query.blocks[0] : null;
var usergroups = "";
var messagesArray = [];
var linksArray = [];
if (userdata.invalid === null || userdata.invalid === undefined) {
for (let group of userdata.groups) {
messagesArray.push(`group-${group}-member`);
linksArray.push(`grouppage-${group}`);
}
}
messagesArray.push('blockedtitle');
await new mw.Api().loadMessagesIfMissing(messagesArray);
await new mw.Api().loadMessagesIfMissing(linksArray, {amlang: mw.config.get("wgContentLanguage")});
$("#siteSub").text("");
$("#siteSub").css("display", "block");
$("#siteSub").append($('
'));if (userdata.blockid || blockdata) {
// readd block ID
$("#firstHeading").append(` (${mw.message(`blockedtitle`)})`);
}
if (userdata.invalid === null || userdata.invalid === undefined) {
for (let group of userdata.groups) {
if (group == "*" || group == "user") continue;
$("#usergroups").append(`${mw.message(`group-${group}-member`).parse()} `);
}
}
} else {
console.error(res.error);
}
}
$(".mw-userlink").each(async function() {
let that = this;
let user = $(this).text();
if (window.userGroupsCache[user]) {
return;
} else {
window.userGroupsCache[user] = true;
}
let res = typeof window.userGroupsCache[user] == "object" ? window.userGroupsCache[user] : await $.get(mw.config.get('wgScriptPath') + '/api.php', {
action: "query",
list: "users|blocks",
ususers: user,
usprop: "groups|blockinfo",
bkusers: isIP(user) ? undefined : user,
bkip: isIP(user) ? user : undefined,
format: "json"
});
if (res.error) {
console.error(res.error);
delete window.userGroupsCache[user];
} else {
window.userGroupsCache[user] = res;
var userdata = res.query.users.length > 0 ? res.query.users[0] : {};
var blockdata = res.query.blocks.length > 0 ? res.query.blocks[0] : null;
var classString = "";
classString += 'usergroup ';
if (userdata.blockid) {
classString += "usergroup-blocked ";
if (userdata.blockpartial !== null && userdata.blockpartial !== undefined) {
classString += "usergroup-blockedpartial ";
}
} else if (blockdata) {
classString += "usergroup-blocked "
if (blockdata.partial !== null && blockdata.partial !== undefined) {
classString += "usergroup-blockedpartial ";
}
}
if (userdata.invalid === null || userdata.invalid === undefined) {
for (let group of userdata.groups) {
if (group == "*" || group == "user") continue;
classString += `usergroup-${group} `;
}
}
$(`.mw-userlink:contains(${user})`).each(function() {
if ($(this).text() != user) return;
let $enclosure = $('');
$(this).before($enclosure);
$enclosure.append($(this));
$enclosure.addClass(classString);
})
}
});
});
}
});
//