Snippets/Unwatch from watchlist

From MediaWiki.org
Jump to navigation Jump to search
How to use Snippets
List of Snippets
Crystal Clear action run.png
Unwatch from watchlist
Language(s): JavaScript
Compatible with: MediaWiki 1.30+ (Vector)

Description[edit]

Unwatch a page right from the watchlist itself. Adds " | unwatch" for each entry.

Code[edit]

/**
 * 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.watch'] ).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( ' | ' );
		} );
	} );
} );

See also[edit]