Jump to content

User:Galobtter/scripts/WatchlistLinksNewTab.js

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by Galobtter (talk | contribs) at 05:35, 9 August 2019 (Galobtter moved page User:Galobtter/WatchlistLinksNewTab.js to User:Galobtter/scripts/WatchlistLinksNewTab.js). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
/* Make links in watchlist open in a new tab. */
( function main() {
	var prevPostRequest;
	var fixLinks = function () {
		// target = "_blank" means open in new context (usually new tab)
		$( '.mw-changeslist a' ).prop( 'target', '_blank' );
	};

	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Watchlist' ) {
		$.ready.then( fixLinks() );
		/* Rerun every time update watchlist */
		$( document ).ajaxSend( function ( event, request, settings ) {
			/**
			 * Should not update on regular pings by the watchlist checking for an update
			 * Wait for the next ping after a post request
			 * (which is involved in updating watchlist)
			 */
			if ( settings.type === 'POST' ) {
				prevPostRequest = true;
				return;
			}
			if (
				prevPostRequest &&
				settings.url.indexOf( 'Special:Watchlist' ) === -1 &&
				settings.type === 'GET'
			) {
				fixLinks();
				prevPostRequest = false;
			}
		} );
	}
}() );