User:Aidan9382/scripts/IPBlockNotice.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:Aidan9382/scripts/IPBlockNotice. |
//Lets you know if your current IP has no blocks, is softblocked (only affects IP editors), or hardblocked (affects all except IPBE editors)
//This is very lazily and poorly coded, it'll probably break under any environment but mine, so be warned
$.when( $.ready, mw.loader.using( 'mediawiki.util' ) ).then( function() { //this is how its always done cause idk
var NOBLOCK = 0; //its like a worse enum
var SOFTBLOCK = 1;
var HARDBLOCK = 2;
function GetIPBlockStatus(ip, callback) {
//I hate how I do a callback thing here but $.get isnt pausing or whatever so I just have to now cause I'm not looking into it
var resp = $.get( //logic taken from [[User:Suffusion of Yellow/markblocked.js]]
mw.util.wikiScript( 'api' ) + '?format=json&action=query',
{list: 'blocks', bklimit: 10, bkip: ip, bkprop: 'flags'},
function(data) {
if (data.batchcomplete === undefined) {
console.log("Response error", data);
return;
}
var softblocked = false;
for (var blockID in data.query.blocks) {
var block = data.query.blocks[blockID];
if (block.anononly === undefined) {
callback(HARDBLOCK);
return;
}
softblocked = true;
}
if (softblocked)
callback(SOFTBLOCK);
else
callback(NOBLOCK);
}
);
}
//so lazy but eh
var notice = document.createElement("li");
notice.class = "mw-list-item";
notice.style = "color: gray";
notice.innerHTML = "IP Blocked: Unknown";
var up = document.getElementById("pt-userpage");
if (up === null) {
console.log("Not even gonna bother trying to do IPBlock notice visually");
return;
} else {
up.parentElement.insertBefore(notice, up);
}
function UpdateNotice(state, IP) {
var BlockStatus, BlockColor;
if (state === HARDBLOCK) {
BlockStatus = "Yes";
BlockColor = "red";
} else if (state === SOFTBLOCK) {
BlockStatus = "Anon. Only";
BlockColor = "orange";
} else {
BlockStatus = "No";
BlockColor = "green";
}
notice.innerHTML = "IP Blocked: <a style='color:inherit' href='/wiki/Special:Contributions/" + IP + "'>" + BlockStatus + "</a>";
notice.style = "color: " + BlockColor;
}
var lastTime = mw.user.options.get("userjs-aidan9382-ipbn-time"), lastState = mw.user.options.get("userjs-aidan9382-ipbn-state");
if (lastTime === undefined || Date.now() - lastTime > 60000) { //60s
$.get("https://api.ipify.org/", function(IP) {
if (IP == "<nil>") {
notice.innerHTML = "Unable to get IP";
} else {
GetIPBlockStatus(IP, function(state) {
UpdateNotice(state, IP);
var api = new mw.Api();
api.saveOptions({"userjs-aidan9382-ipbn-time": Date.now(), "userjs-aidan9382-ipbn-state": state}).done(console.log);
});
}
});
} else {
$.get("https://api.ipify.org/", function(IP) {
UpdateNotice(parseInt(lastState), IP);
});
}
});