User:Mojtabakd/scripts/HistoryHighlight.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.
// Significantly Modified from [[User:Guywan/Scripts/HistoryHighlight.js]]
// I copied the original version from https://fa.wikipedia.org/wiki/کاربر:Jeeputer/historyHighlight.js
// This script is under test, it's not usable yet.

$(function()
{
	if(mw.config.get("wgCanonicalSpecialPageName") == "Contributions")
	{
		// Set the username in local storage.
		mw.cookie.set("hh-username", mw.config.get("wgRelevantUserName"));
	}
	else if(mw.config.get("wgAction") == "history")
	{
		var userlist = "";
		
		$("#pagehistory li").each(function()
		{
			if (userlist == "")
			{
				userlist = $(".history-user a bdi", this).html();
			}
			else
			{
				userlist += "|" + $(".history-user a bdi", this).html();
			}
		});
		
		var url = "https://en.wikipedia.org/w/api.php"; 

		var params =
		{
	    	action: "query",
	    	list: "users",
	    	ususers: userlist,
	    	usprop: "groups",
	    	format: "json"
		};

		url = url + "?origin=*";
		Object.keys(params).forEach(function(key){url += "&" + key + "=" + params[key];});

		fetch(url)
		    .then(function(response){return response.json();})
		    .then(function(response)
		    {
		        var users = response.query.users;
		        var myusername = mw.cookie.get("hh-username");

		        $("#pagehistory li").each(function()
				{
					var colour = "lightblue";
					for (u in users)
					{
						var captured_user = $(".history-user a bdi", this).html();
						if ((users[u].name.includes(captured_user)==true)&&(myusername!=captured_user))
						{
							if (users[u].hasOwnProperty("groups")==true)
							{
	        	    			if (users[u].groups.includes("sysop")==true)
	            				{
	            					colour = "#ffff4d";
	    	    	    		}
    	    		    		else if (users[u].groups.includes("bot")==true)
	        		    		{
    		        				colour = "#66ff66";
			            		}
			            		$(this).css("background-color", colour);
							}
							else
							{
								colour = "#ff80ff";
								$(this).css("background-color", colour);
							}
						}
						else if ((users[u].name.includes(captured_user)==true)&&(myusername==captured_user))
						{
							colour = "#ff9933";
							$(this).css("background-color", colour);
						}
					}
				});
		    })
   			.catch(function(error){console.log(error);});
	}
});