User:Aidan9382/scripts/IPBlockNotice.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//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);
		});
	}
});