Jump to content

User:DannyS712 test/sandbox.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.
// <nowiki>
// Quick script to mass patrol new pages
// @author DannyS712
$(() => {
const MassCuration = {};
window.MassCuration = MassCuration;

MassCuration.init = function () {
	mw.loader.using(
		[ 'mediawiki.api' ],
		MassCuration.run
	);
};

MassCuration.onErrHandler = function () {
	// Shared error handler
	alert( 'Something went wrong' );
	console.log.apply( null, arguments );
};

MassCuration.run = function () {
	console.log( 'running' );
	var $newPages = $('#mw-content-text > ul > li.not-patrolled');
	$newPages.each( function () {
		var $newPage = $( this );
		
		var pageTitle = $newPage.children( 'a:first' ).attr( 'title' );
		var $button = $( '<button>' )
			.text( 'curate' );
		$button.click(
			function() {
				MassCuration.curate( pageTitle ).then(
					function () {
						$button.remove();
					},
					MassCuration.onErrHandler
				);
			}
		);
		$newPage.prepend( $button );
	} );
};

MassCuration.curate = function ( pageName ) {
	return new Promise(	( resolve ) => {
		var api = new mw.Api();
		api.get( {
			action: 'query',
			prop: 'info',
			titles: pageName,
			formatversion: 2
		} ).then(
			function ( response ) {
				console.log( response );
				var pageId = response.query.pages[0].pageid;
				api.postWithEditToken( {
					action: 'pagetriageaction',
					pageid: pageId,
					reviewed: 1,
				} ).then(
					function ( response2 ) {
						console.log( response2.pagetriageaction );
						resolve();
					},
					MassCuration.onErrHandler
				);
			},
			MassCuration.onErrHandler
		);
	} );
};

});

$( document ).ready( () => {
	if ( mw.config.get( 'wgNamespaceNumber' ) === -1 ) {
		const page = mw.config.get( 'wgCanonicalSpecialPageName' );
		if ( page === 'Newpages' ) {
			window.MassCuration.init();
		}
	}
});

// </nowiki>