User:NKohli (WMF)/megawatch.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

// Page title
var ns = mw.config.get( 'wgCanonicalNamespace' );
var catpg = mw.config.get( 'wgPageName' );
var tok = 0;

function execute() {
	if ( ns == 'Category' ) {
	$( '#p-cactions' ).after(
   		'<div id="megawatch" role="navigation" class="vectorMenu"><h3><span class="" title="Watch all subpages for this category">Megawatch</span></h3></div>'
   		);
	}
	$( '#megawatch' ).click( function() {
		var n = 500;
		if ( !$( '#megawatch' ).hasClass( 'watched' ) ) {
			confirm( 'Are you sure you want to watch all pages in this category? (Restricted to top 500 pages only)' );
			watchPages( n, 'watch' );
		} else {
			confirm( 'Are you sure you want to unwatch all pages in this category? (Restricted to top 500 pages only)' );
			watchPages( n, 'unwatch' );
		}
	} );
}

// Watch/unwatch pages
// @param n Number of pages to watch/unwatch
// @param act Action: watch or unwatch
function watchPages( n, act ) {
	// Make the ajax request to fetch subpages
	$.ajax( {
			url: mw.util.wikiScript( 'api' ),
			data: {
				action: 'query',
				list: 	'categorymembers',
				format: 'json',
				cmtitle: catpg,
				formatversion:	2,
				cmprop: 'title',
				cmtype: 'page',
				cmlimit: n
			},
			success: function( data ) {
				subpgs = data.query.categorymembers;
				// Check if we got anything
				if ( !subpgs ) {
					console.log('none found');
				} else {
					result = [];
			 		for( var i = 0; i < subpgs.length; i++) {
			 			result.push( subpgs[i].title );
			 		}

			 		// Post the watch request
			 		if ( act == 'watch' ) {
				 		new mw.Api().postWithToken( 'watch', {
							action: 'watch',
							titles: $.isArray( result ) ? result.join( '|' ) : String( result ),
							formatversion: 2,
							format: 'json'
						} )
						.fail( function ( code, errResponse ) { console.log( errResponse ); } )
						.done( function () { 
							if ( act == 'watch' ) {
								$( '#megawatch' ).addClass( 'watched' );	
							} else {
								$( '#megawatch' ).removeClass('watched' );
							}
						} );
					// Or the unwatch request
			 		} else {
			 			new mw.Api().postWithToken( 'watch', {
							action: 'watch',
							titles: $.isArray( result ) ? result.join( '|' ) : String( result ),
							formatversion: 2,
							format: 'json',
							unwatch: 'true'
						} )
						.fail( function ( code, errResponse ) { console.log( errResponse ); } )
						.done( function () { 
							if ( act == 'watch' ) {
								$( '#megawatch' ).addClass( 'watched' );
							} else {
								$( '#megawatch' ).removeClass( 'watched' );
							}
						} );
			 		}
				}
			}
		} );
}

$( document ).ready( execute() );