User:MonkeysHK/common.js

$(function {   var hypixelItems;    window.hypixelItems = function {        if (hypixelItems) return hypixelItems;        $.get("https://api.hypixel.net/resources/skyblock/items", function(data) { hypixelItems = data; console.log("Success. Run again for data."); });   }; });

mw.loader.using(["mediawiki.api"]).then(function {	var ALLOWED_PAGE = "User:MonkeysHK/sandbox5",		MAYOR_DATA_PAGE = "Module:Mayor/Data",		MAYOR_ELECTIONS_PAGE = "Module:Mayor/Elections",		ELECTED_BUTTON = "#mayordata-last",		CURRENT_BUTTON = "#mayordata-current";

if (		mw.config.get("wgPageName") !== ALLOWED_PAGE &&		($(ELECTED_BUTTON).length + $(CURRENT_BUTTON).length < 1)	) return; function getElectionData { return fetch("https://api.hypixel.net/resources/skyblock/election") .then(function (response) {				return response.json;			}); }	function getModuleData(page) { return (new mw.Api).post({               action: "scribunto-console",                title: mw.config.get("wgPageName"),                question: "=mw.text.jsonEncode(require('" + page + "'))"            }) .then(function (response) {               return response.return;            }) .then(function (data) {               return JSON.parse(data);            }); }	function getLocalData { return getModuleData(MAYOR_DATA_PAGE); }	function getLocalElections { return getModuleData(MAYOR_ELECTIONS_PAGE); }	function getPerks(candidateData, localData) { var a = [], mayorData = localData[candidateData.name], indexedMayorPerks = mayorData.perks_listed || mayorData.perks; candidateData.perks.forEach(function(perk) {			for (var j in indexedMayorPerks) {				if (indexedMayorPerks[j][0] === perk.name) {					a.push(Number(j) + 1);					break;				}			}		}); return a.join(""); }	function candidatesToArray(candidates, localData, existingData) { var res = []; candidates.forEach(function(cand, i) {			res.push({ name: cand.name, order: i + 1, perks: getPerks(cand, localData), votes: cand.votes, last: existingData && existingData[cand.name] && existingData[cand.name].last || undefined, });		});		return res; }

if ($(ELECTED_BUTTON).length > 0) $(ELECTED_BUTTON).show.click(function {			Promise.all([getElectionData, getLocalData, getLocalElections]).then(function (data) { var pastElectionData = data[0].mayor.election, localData = data[1].mayors, localElections = data[2].elections, year = Number(pastElectionData.year), existingData = localElections[year - 1] && localElections[year - 1].data || undefined; var candidates = candidatesToArray(pastElectionData.candidates, localData, existingData).map(function(v) {					return "\t\t" + v.name + " = { " + " votes = " + v.votes + ", perks = '" + v.perks + "', order = " + v.order + ", last = " + (v.last || "nil") + " },";				}); alert("\t[" + year + "] = { " + "date = nil, ui = true, data = {\n" + candidates.join("\n") + "\n\t}},"); });		});	if ($(CURRENT_BUTTON).length > 0) $(CURRENT_BUTTON).show.click(function {			Promise.all([getElectionData, getLocalData]).then(function (data) { var currentElectionData = data[0].current, localData = data[1].mayors, year = Number(currentElectionData.year); var candidates = candidatesToArray(currentElectionData.candidates, localData).map(function(v) {					return "\t\t" + v.name + " = { " + " votes = nil, perks = '" + v.perks + "', order = " + v.order + ", last = nil },";				}); alert("\t[" + year + "] = { control = 'in-progress', data = {\n" + candidates.join("\n") + "\n\t}},"); });		}); });