User:Begoon/unWatch.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 script adds an "unwatch" link to each entry in your watchlist, using the new
* ajax watchlist thing.
*/

var wlUnwatch = {
	supported: true,
	onLoad: function () {
		var i, j, m, a, span, d, links, link;
		
		// history links
		links = $( '.mw-changeslist > ul > li .mw-changeslist-links' );
		for ( i = links.length-1; i >= 0; i-- ) {
			link = $( links[ i ] ).find( 'a' )[0];
			if ( !link ) {
				continue;
			}
			m = link.href.match( /title=([^&]*)/ );
			if ( !m ) {
				continue;
			}

			a = document.createElement( 'A' );
			a.href = mw.util.wikiScript() + '?action=unwatch&title=' + m[ 1 ];
			a.title = 'Unwatch ' + link.title;
			a.unwatchInProgress = false;
			a.onclick = wlUnwatch.onClick;
			a.appendChild( document.createTextNode( 'unw' ) );

			span = document.createElement( 'SPAN' );
			span.appendChild( a );
			links[ i ].appendChild( span );
		}
		
		// log entries
		links = $( '.mw-changeslist > ul > li > .mw-changeslist-line-inner > a' );
		for ( i = links.length-1; i >= 0; i-- ) {
			if ( /\/Special:Log\/newusers$/.test( links[ i ].href ) ) {
				j = i + 1;
			} else if ( /\/Special:Log\//.test( links[ i ].href ) ) {
				j = i + 2;
			} else {
				continue;
			}
			m = links[ j ].href.match( /title=([^&]*)/ );
			if ( !m ) {
				m = links[ j ].href.match( /\/wiki\/([^?]*)/ );
			}
			if ( !m ) {
				continue;
			}
			a = document.createElement( 'A' );
			a.href = mw.util.wikiScript() + '?action=unwatch&title=' + m[ 1 ];
			a.title = 'Unwatch ' + links[ j ].title;
			a.unwatchInProgress = false;
			a.onclick = wlUnwatch.onClick;
			a.appendChild( document.createTextNode( 'unw' ) );
			links[ i ].parentNode.insertBefore( a, links[ i ].nextSibling );

			// insert a delimiter between the two links
			d = document.createTextNode( '\xa0| ' );
			links[ i ].parentNode.insertBefore( d, a );
		}
	},

	onClick: function () {
		if ( !wlUnwatch.supported || false ) {
			wlUnwatch.supported = false;
			return true;
		}

		/* START - CONFIRMATION PROMPT ADDED */
		/* ================================= */

		if (!confirm('Remove this page from your watchlist?')) {
				wlUnwatch.supported = true;
				return false;
		}

		/*  END - CONFIRMATION PROMPT ADDED  */
		/* ================================= */

		var link = this;
		if ( link.unwatchInProgress ) {
			return false;
		}
		link.unwatchInProgress = true;
		link.style.color = 'red';
		var timeout = setTimeout( function() {
			timeout = null;
			link.unwatchInProgress = false;
			link.style.color = '';
		}, 10000 );
		var api = new mw.Api();
		api.postWithToken( 'watch', {
			action: 'watch',
			unwatch: 1,
			titles: decodeURIComponent( link.href.match( /&title=(.+)/ )[ 1 ].replace( /_/g,' ' ) )
		} ).done( function( data ) {
			if ( timeout ) {
				clearTimeout( timeout );
			}
			timeout = null;
			link.unwatchInProgress = false;
			link.style.color = '';
			if ( data.watch[ 0 ].unwatched !== undefined ) {
				var li;
				for ( li = link; li && li.nodeName !== 'LI'; li = li.parentNode ); // Empty loop
				if ( li ) {
					var classes = li.className.split(' ');
					var i, len, cl, matches, ns, assocCl;
					for ( i = 0, len = classes.length; i < len; i++ ) {
						cl = classes[i];
						matches = cl.match( /^watchlist-(\d+)-(.*)/ );
						if ( matches ) {
							break;
						}
					}
					if ( matches ) {
						ns = Number( matches[ 1 ] );
						ns ^= 1; // use bitwise XOR to get the associated namespace number
						assocCl = 'watchlist-' + ns + '-' + matches[ 2 ];
						$( '.' + cl ).remove();
						$( '.' + assocCl ).remove();
					} else {
						li.parentNode.removeChild( li );
					}
				}
			}
		} ).fail( function ( error ) {
			if ( timeout ) {
				clearTimeout( timeout );
			}
			timeout = null;
			link.unwatchInProgress = false;
			link.style.color = '';
		} );
		return false;
	}
};

if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Watchlist' && mw.config.get( 'wgAction' ) === 'view' ) {
	mw.loader.using( [ 'mediawiki.util','mediawiki.api' ], function() {
		$( document ).ready( wlUnwatch.onLoad );
	} );
}