Jump to content

User:Fred Gandt/unwatchList.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.
if ( ( /^Special:Watchlist/ ).test( mw.config.get( "wgPageName" ) ) ) {
	const CHNGLST = document.querySelector( "#mw-content-text div.mw-changeslist" );
	mw.loader.load( "/w/index.php?title=User:Fred_Gandt/unwatchList.css&action=raw&ctype=text/css", "text/css" );
	$( document ).ready( () => {
		CHNGLST.addEventListener( "click", evt => {
			const TRG = evt.target,
				PAR = TRG.parentElement;
			if ( TRG === PAR.querySelector( "span.mw-changeslist-separator" ) ) {
				const CSSQ = 'li span.mw-changeslist-line-inner[data-target-page="',
					UNWTCHNG = !PAR.parentElement.classList.contains( "fg-changeslist-unwatched" ),
					TPP = /^(?:([a-z]+)( talk)?\:)?(.+)$/i.exec( PAR.dataset.targetPage ),
					NTNS = TPP[ 1 ] && TPP[ 1 ] !== "Talk",
					PGNM = ( NTNS ? TPP[ 1 ] + ":" : "" ) + TPP[ 3 ],
					TPGNM = ( NTNS ? TPP[ 1 ] + " talk:" : "Talk:" ) + TPP[ 3 ];
					QS = {
						action: "watch",
						format: "json",
						token: mw.user.tokens.values.watchToken,
						titles: PGNM
					};
				if ( UNWTCHNG ) QS.unwatch = true;
				$.ajax( {
					type: "POST",
					url: "/w/api.php",
					dataType: QS.format,
					data: QS,
					success: data => CHNGLST.querySelectorAll( CSSQ + PGNM + '"],' + CSSQ + TPGNM + '"]' ).forEach( s => s.parentElement.classList.toggle( "fg-changeslist-unwatched", UNWTCHNG ) ),
					error: data => console.error( data )
				} );
			}
		} );
	} );
}