User:Williek11/global.js

importArticles({   type: 'script',    articles: [        'u:dev:MediaWiki:Rollback/code.js',        'u:dev:MediaWiki:AnchoredRollback/code.js',        'u:dev:MediaWiki:UsefulDropdown/code.js',		'u:dev:MediaWiki:RemoveTracking.js',		'u:dev:MediaWiki:MassProtect/code.js',		'u:dev:MediaWiki:AjaxBatchDelete.js',		'u:dev:MediaWiki:MassRollback.js',    ] });

if (location.search.includes("action=edit")) { window.onbeforeunload = function{ return '' }; }

// navbar adjustments

(function{ const doc = document;

const upperNav = doc.createElement("div"); const ul = doc.createElement("ul"); upperNav.className = "upper-container";

$(".main-container")[0].appendChild(upperNav);

upperNav.appendChild(ul);

function addElement (node, id) { const li = doc.createElement("li"); li.id = id; li.appendChild(node); ul.appendChild(li); }

addElement($(".fandom-community-header__top-container")[0].children[2]); addElement($(".global-navigation__bottom")[0].children[1], "user-profile"); addElement($(".notifications")[0], "notifications");

});

// medals adjustments

(function{	if (location.pathname !== "/wiki/Taming.io_Wiki:Medals") return;	function waitForButtonLoad {		console.log("Global.js: Waiting");		if ($("#MedalSettings")[0] === undefined) {setTimeout(waitForButtonLoad, 60)}		else {onMedalsLoad}	}	function onMedalsLoad  {		const button = $("#MedalSettings")[0];		const parent = button.parentElement;		const newButton = document.createElement("button");		newButton.textContent = "Update Settings";		newButton.addEventListener("click", function  { updateMedalsList; updateMedals; });		parent.id = "MedalCotainer";		parent.appendChild(newButton);	}	function updateMedalsList  {}	function updateMedals  {		// medals		const medal = {	    	posts: {	    		list: ["Forum User", "Forum Poster", "Forum Machine"],	    		"Forum User": 100,	    		"Forum Poster": 1000,	    		"Forum Machine": 5000,	    	},	    	edits: {	    		list: ["Starter Badge", "Rock Badge", "Water Badge", "Electric Badge", "Plant Badge", "Poison Badge", "Combat Badge", "Fire Badge", "Ice Badge", "Spectrum Badge", "Dragon Badge"], "Starting Badge": 1, "Rock Badge": 50, "Water Badge": 100, "Electric Badge": 250, "Plant Badge": 500, "Poison Badge": 750, "Combat Badge": 1000, "Fire Badge": 1500, "Ice Badge": 2000, "Spectrum Badge": 3000, "Dragon Badge": 5000, }	   };		var MedalSettings = {}; // get medal settings $.ajax({	       url: '/index.php',	        type: 'GET',	        data: {	            title: 'Project:Medals',	            action: 'raw',	            cb: Math.ceil(new Date.getTime / 1000),	            dataType:'text'	        },	        success: function(data) {	            // Parsing result	            MedalSettings = JSON.parse(data);	        },	    }); const users = {}; const usersList = []; // get all users const allUsers = new mw.Api.get({	       action: 'listuserssearchuser',	        format: 'json',	        groups: 'all,bureaucrat,content-moderator,rollback,sysop,threadmoderator',	        contributed: 0,	        limit: 10000,	        order: 'edits',	        sort: 'desc',	        offset: 0,	    }).done(function(e){	    	const data = e.listuserssearchuser;	    	// for every user in data	        for (var i = 0, l = data.result_count; i < l; i++) {	        	const current = i;	            const username = data[i].username;	            users[username] = { edits: parseInt(data[i].edit_count) };	            usersList[i] = username;	            $.ajax({ url: '/wikia.php', type: 'GET', data: { controller: 'UserProfile', method: 'getUserData', format: 'json', userId: data[i].user_id },	               success: function(data) { users[username].posts = data.userData.posts; users[username].tags = data.userData.tags; if (current === l-1) {onFinish;} },	           });	        }	    });	    function addMedals (username, medals) { // if the person isnt yet in the settings if (MedalSettings.dataUser[username] === undefined) { MedalSettings.dataUser[username] = []; }	   	// for every medal awarded for (var i = 0; i < medals.length; i++) { // check if already has if (MedalSettings.dataUser[username].includes(medals[i]) === false) { MedalSettings.dataUser[username].push(medals[i]); }	   	}	    }	    function onFinish  { // for every user for (var i = 0, l = usersList.length; i < l; i++) { const user = usersList[i]; const grantedMedals = []; // user posts medals if (users[user].posts > 0) { const posts = users[user].posts; for (var m = 0, n = medal.posts.list.length; m < n; m++) { const currentMedal = medal.posts.list[m]; if (posts >= medal.posts[currentMedal]) {grantedMedals.push(currentMedal)} }	   		}	    		// user posts medals if (users[user].edits > 0) { const edits = users[user].edits; for (var m = 0, n = medal.edits.list.length; m < n; m++) { const currentMedal = medal.edits.list[m]; if (edits >= medal.edits[currentMedal]) {grantedMedals.push(currentMedal)} }	   		}	    		// if there are medals to be awarded if (grantedMedals[0] !== undefined) { addMedals(user, grantedMedals) } }	   	const stringifyMedalSettings = JSON.stringify(MedalSettings); function areThereChanges (title, text, callback) { return new mw.Api.get({					action: "compare",					fromtitle: title,					toslots: "main",					"totext-main": text,				}).done(function(e){callback(e.compare['*'] !== "")}); }	   	function editMedals  { new mw.Api.get({action:"query",meta:"tokens"}).done(function(e){					new mw.Api.post({ action: 'edit', title:'Taming.io Wiki:Medals', text: stringifyMedalSettings, bot: true, minor: true, token: e.query.tokens.csrftoken });				});	   	}	    	areThereChanges("Taming.io Wiki:Medals", stringifyMedalSettings, function (are) { if (are) { editMedals } }); }	}	function init { waitForButtonLoad } init; });