🇮🇷 Iran Proxy | https://www.wikipedia.org/wiki/User:Andrybak/Scripts/user-tabs-on-contribs.js
Jump to content

User:Andrybak/Scripts/user-tabs-on-contribs.js

From Wikipedia, the free encyclopedia
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.
/*
 * This is a fork of [[User:Enterprisey/user-tabs-on-contribs.js]],
 * as of https://en.wikipedia.org/w/index.php?oldid=916388347
 */

(() => {
	if ((mw.config.get('wgNamespaceNumber') !== -1) ||
			(mw.config.get('wgCanonicalSpecialPageName') !== 'Contributions'))
	{
		return;
	}

	/**
	 * Makes a tab linking to the given page name. `redlink`` is a boolean
	 * indicating whether the page should be linked as a red link.
	 */
	function utocMakeTab(label, pageName, redlink, accesskey, id) {
		setTimeout(() => {
			const previousVersion = document.querySelector(".emptyPortlet #" + id);
			if (previousVersion) {
				previousVersion.remove();
			}
		}, 0);
		const link = $("<a>")
			.text(label)
			.attr("accesskey", accesskey)
			.attr("title", pageName + " (access key " + accesskey + ")")
			.attr("href", mw.util.getUrl(pageName));
		var tab = $("<li>")
			.append(link)
			.attr("id", id);
		if (redlink) {
			link.addClass("new");
		}
		tab.addClass('vector-tab-noicon mw-list-item');
		return tab;
	}
	
	function getUserPageTabName() {
		const nsTabUser = mw.message('Nstab-user');
		if (!nsTabUser) {
			return "User page";
		}
		return nsTabUser.text();
	}
	
	function getUserTalkPageTabName() {
		const candidateKeys = ['Nstab-user_talk', 'Nstab-talk', 'Talk'];
		for (const k of candidateKeys) {
			const candidate = mw.message(k);
			if (candidate && candidate.exists() && candidate.text().length > 0) {
				return candidate.text();
			}
		}
		return "Talk";
	}

	$.when(
		mw.loader.using( [ "mediawiki.api", "mediawiki.util" ] ),
		$.ready
	).then( function () {
		/*
		 * Load localization messages:
		 * [[MediaWiki:Nstab-user talk]], [[MediaWiki:Nstab-user]], [[MediaWiki:Talk]]
		 */
		new mw.Api().loadMessagesIfMissing(['Nstab-user', 'Nstab-user_talk', 'Nstab-talk', 'Talk']).done(() => {
			const username = mw.config.get('wgRelevantUserName') || mw.util.getParamValue( "target" );
			let userPageTitle = "User:" + username;
			let userTalkPageTitle = "User talk:" + username;
			new mw.Api().get( {
				action: "query",
				titles: userPageTitle + "|" + userTalkPageTitle,
				formatversion: "2"
			} ).done( function ( data ) {
				if( data && data.query && data.query.pages ) {
					let userPageRedlink;
					let userTalkPageRedlink;
					if (data.query.normalized) {
						for (const n of data.query.normalized) {
							if (n.from === userPageTitle) {
								userPageTitle = n.to;
							}
							if (n.from === userTalkPageTitle) {
								userTalkPageTitle = n.to;
							}
						}
					}
					Object.values( data.query.pages ).forEach( function ( v ) {
						if (v.title === userPageTitle) {
							userPageRedlink = v.missing;
						}
						if (v.title === userTalkPageTitle) {
							userTalkPageRedlink = v.missing;
						}
					} );
					$('#p-associated-pages').removeClass('emptyPortlet');
					const userPageTabName = getUserPageTabName();
					const userTalkPageTabName = getUserTalkPageTabName();
					$( "#left-navigation nav ul" )
						.append(utocMakeTab(userPageTabName, userPageTitle, userPageRedlink, "c", "ca-nstab-user"))
						.append(utocMakeTab(userTalkPageTabName, userTalkPageTitle, userTalkPageRedlink, "t", "ca-nstab-talk"));
				}
			} );
		});
	} );

})();