Jump to content

User:Insertcleverphrasehere/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.
/**
 * Unwatch from watchlist
 *
 * Add an "unwatch" link near each entry on the watchlist view ([[bugzilla:424]]).
 *
 * @source https://www.mediawiki.org/wiki/Snippets/Unwatch_from_watchlist
 * @author Krinkle
 * @revision 2016-09-01
 */
mw.hook( 'wikipage.content' ).add( function ( $content ) {
	// Only on Watchlist and not in the /edit or /raw mode
	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Watchlist' || location.href.indexOf( '/edit' ) > 0 || location.href.indexOf( '/raw' ) > 0 ) {
		return;
	}
	mw.loader.using( ['mediawiki.util', 'mediawiki.api'] ).then( function () {
		// Get the links
		var $wlHistLinks = $content.find( 'ul.special .mw-changeslist-line .mw-changeslist-history');

		$wlHistLinks.each( function () {
			var $unwatch,
				$el = $( this ),
				title = mw.util.getParamValue( 'title', this.href );

			$unwatch = $el.clone()
				.text( 'unwatch' )
				.attr( 'href', function ( i, val ) {
					return val.replace( 'action=history', 'action=unwatch' );
				} )
				.on( 'click', function ( e ) {
					new mw.Api().unwatch( title )
						.done( function () {
							$unwatch.css( { pointerEvents: 'none', opacity: 0.5 } );
						} )
						.fail( function () {
							$unwatch.off( 'click' ).append( ' (try again?)' );
						} );
					e.preventDefault();
				} );
			$el.after( $unwatch ).after( ' | ' );
		} );
	} );
} );