User:Oshwah/History-UserLinks.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.
//<nowiki>
mw.loader.using("mediawiki.util", function () {
	const action = mw.config.get("wgAction"); //Grabs the current action (e.g. 'view', 'edit', 'history').

	if (action === "history") { //Only proceed if this is an action page and if the current action page is 'history'.
		$("li:has(.mw-userlink):has(.mw-usertoollinks)").each(function () { //Loop through <li>s that have '.mw-userlink' AND '.mw-usertoollinks' inside them.

			const user_tool_links = $(this).find(".mw-usertoollinks").first().html(); //Grab the HTML of the user tool links for the current user.
			const relevant_user = $(this).find(".mw-userlink").first().text(); //Grab the username of the relevant user.
			const encoded_username = encodeURIComponent(relevant_user);

			//Create all the links we want to append.
			const usertalk_link = user_tool_links.match('^(.*)>talk</a>'); //Grab existing HTML of the user talk page link from the existing '.mw-usertoollinks' HTML. This also captures the opening <span> tag for the entire list of user links.
			const contribs_temp = user_tool_links.match('<a href="/wiki/Special:Contributions(.*)>contribs</a>');
			const contribs_link = ((contribs_temp == null)?('<a href="/wiki/Special:Contributions/' + relevant_user + '" class="mw-usertoollinks-contribs" title="Special:Contributions/' + relevant_user + '">contribs</a>'):(contribs_temp[0])); //If the current user's tool links don't contain a contribs link (IP address), we must create our own instead.

			const publiclog_link = '<a href="/wiki/Special:Log?user=' + encoded_username + '">logs</a>';
			const checkuser_link = '<a href="/w/index.php?title=Special:CheckUser&user=' + encoded_username + '">checkuser</a>';
			const checklog_link = '<a href="/w/index.php?title=Special:CheckUserLog&cuSearchType=target&cuSearch=' + encoded_username + '">checks</a>';
			const block_link = '<a href="/wiki/Special:Block/' + encoded_username + '" class="mw-usertoollinks-block" title="Special:Block/' + relevant_user + '">block</a>';

			let new_links = [usertalk_link[0]]; //Create array of new tool links, set first element to user talk page link.
			new_links.push(contribs_link, publiclog_link); //Add contribs and public log links to the array.

			if (mw.util.isIPAddress(relevant_user)) { //If the current user is an IP address, we have IP user tool links to add as well.
				const whois_link = '<a href="https://tools.wmflabs.org/whois-referral/gateway.py?lookup=true&ip=' + encoded_username + '">WHOIS</a>'; //Create IP user tool links 
				const geolocate_link = '<a href="https://whatismyipaddress.com/ip/' + relevant_user + '">geolocate</a>';
				const proxy_check_link = '<a href="https://spur.us/context/' + relevant_user + '">proxy</a>';
				const bullseye_check_link = '<a href="https://bullseye.toolforge.org/ip/' + relevant_user + '">bullseye</a>';

				new_links.push(whois_link, geolocate_link, proxy_check_link, bullseye_check_link); //Add IP user tool links to the array.
			}
			new_links.push(checkuser_link, checklog_link, block_link); //Add ending user tool links to the array.
			const new_links_str = new_links.join('</span> <span>') + '</span>'; //Join each element of the new user tool links together, separated by '</span> <span>' (which automatically adds the " | " separators), add a closing </span> tag, and place into to a singular string.

			$(this).find(".mw-usertoollinks").first().html(new_links_str); //Write the new HTML string back to the user '.mw-usertoollinks' span object and replace the old HTML.
		});
	}
});
//</nowiki>