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", updateMedals);		parent.id = "MedalCotainer";		parent.appendChild(newButton);	}	function areThereChanges (compare1, compare2) {		return compare1 !== compare2;	}	function updateMedals  {		var OriginalMedalSettings = "";		var MedalSettings = {};		var OriginalMedalsList = "";		var MedalsList = null;		// medals		const medal = {	    	posts: {	    		list: ["Forum User", "Forum Poster", "Forum Machine"],	    		"Forum User": 100, "Forum Poster": 1000, "Forum Machine": 5000, },	   	edits: { list: ["Starting 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, },	   	eachMedalsUsers: {} };	   // add to eachMedalsUsers for (var i = 0, allMedals = medal.posts.list.concat(medal.edits.list), l = allMedals.length; i < l; i++) { medal.eachMedalsUsers[allMedals[i]] = []; }		// get medals list $.ajax({	       url: '/index.php',	        type: 'GET',	        data: {	            title: 'Taming.io_Wiki:Medals_List/template',	            action: 'raw',	            cb: Math.ceil(new Date.getTime / 1000),	            dataType:'text'	        },	        success: function(data) {	            MedalsList = data;	        },	    }); $.ajax({	       url: '/index.php',	        type: 'GET',	        data: {	            title: 'Taming.io_Wiki:Medals_List',	            action: 'raw',	            cb: Math.ceil(new Date.getTime / 1000),	            dataType:'text'	        },	        success: function(data) {	            OriginalMedalsList = data;	        },	    }); // 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	            OriginalMedalSettings = data;	            MedalSettings = JSON.parse(data);	        },	    }); window.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;	            window.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) { window.users[username].posts = data.userData.posts; window.users[username].tags = data.userData.tags; if (current === l-1) {onFinish;} },	           });	        }	    });	    function onFinish  { /***********************/	   	/* MedalSettings start */ /***********************/	   	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++) { const medalName = medals[i]; medal.eachMedalsUsers[medalName].push(+username+); // check if already has if (MedalSettings.dataUser[username].includes(medalName) === false) { MedalSettings.dataUser[username].push(medalName); }		   	}		    }	    	// for every user for (var i = 0, l = usersList.length; i < l; i++) { const user = usersList[i]; const grantedMedals = []; // user posts medals if (window.users[user].posts > 0) { const posts = window.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 (window.users[user].edits > 0) { const edits = window.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); /**********************/	   	/* MedalSettings done */ /**********************/	   	/*  MedalsList start  */ /**********************/	   	function replaceWithUsers (medalName) { const allUsersWMedal = (medal.eachMedalsUsers[medalName].join('|')); try { MedalsList.replace('['+medalName+' Title]', MedalSettings.dataMedal[medalName].title); } catch (error) { console.log(MedalSettings.dataMedal, MedalSettings.dataMedal[medalName], medalName) } if (allUsersWMedal === "") { MedalsList = MedalsList.replace('['+medalName+']', 'N/A'); } else { MedalsList = MedalsList.replace('['+medalName+']', ''); }	   	}	    	for (var i = 0, allMedals = medal.posts.list.concat(medal.edits.list), l = allMedals.length; i < l; i++) { replaceWithUsers(allMedals[i]); }	   	/*******************/	    	/* MedalsList done */ /*******************/	   	function editMedals  { new mw.Api.get({action:"query", meta:"tokens"}).done(function(e){					new mw.Api.post({ action: 'edit', title: 'Taming.io Wiki:Medals', summary: 'Automated edit.', text: stringifyMedalSettings, bot: true, minor: true, token: e.query.tokens.csrftoken });				});	   	}	    	function editMedalsList  { new mw.Api.get({action:"query", meta:"tokens"}).done(function(e){					new mw.Api.post({ action: 'edit', title:'Taming.io Wiki:Medals_List', summary: 'Automated edit.', text: MedalsList, bot: true, minor: true, token: e.query.tokens.csrftoken });				});	   	}	    	if ( areThereChanges(OriginalMedalSettings, stringifyMedalSettings) ) { editMedals } if ( areThereChanges(OriginalMedalsList, MedalsList) ) { editMedalsList } console.log(medal); }	}	function init { waitForButtonLoad } init; });