User:Decomposer/common.js

// element with ID currentDate will receive the current time in ISO 8601 format var localTime = document.getElementById('localtime'); Date.prototype.toLocalISO = function { var tzo = -this.getTimezoneOffset, dif = tzo >= 0 ? '+' : '-',       pad = function(num) { var norm = Math.floor(Math.abs(num)); return (norm < 10 ? '0' : '') + norm; };   return this.getFullYear + '-' + pad(this.getMonth + 1) + '-' + pad(this.getDate) + 'T' + pad(this.getHours) + ':' + pad(this.getMinutes) + ':' + pad(this.getSeconds); };

var dt = new Date; localTime.innerHTML = dt.toLocalISO;

/////////////////////////////// /// HotCat shortcuts (function{function setShortCuts{if (!window.HotCat.addShortcuts) return; window.HotCat.addShortcuts({ // Do not modify above. Add your shortcuts below // Shortcut : Replacement, both inside single quotes. If the replacement shall contain a single quote, write it as \'

// ADD YOUR SHORTCUTS HERE. DO NOT FORGET THE COMMA AT THE END OF EACH LINE.

'ww':'Wild World', 'cf':'City Folk', 'nl':'New Leaf', 'wa':'Welcome amiibo', 'hhd':'Happy Home Designer', 'amf':'Amiibo Festival', 'nh':'New Horizons',

// Do not modify below ' ':""});} if (window.HotCat && window.HotCat.runWhenReady) window.HotCat.runWhenReady(setShortCuts); else $('body').on('hotcatSetupCompleted',setShortCuts);});

/////////////////////////////// // Smart Patrol /*
 * This script adds a tab which allows a mass patrolling on the current page. Because it was pointless to mark as patrolled 10 intermediate versions when we can read the difference between the first and the last

if ( mw.config.get('wgNamespaceNumber') >= 0 ) {

mw.loader.using( 'mediawiki.util', function {        $( function ( $ ) {

var link = mw.util.addPortletLink( 'p-cactions', '#', 'Smart patrol', 'Smart patrol' ); $( link ).click( function ( e ) {               e.preventDefault;                mw.loader.using( [ 'mediawiki.api', 'mediawiki.notify', 'oojs-ui' ], function  { askToLaunch; } );           } );

function askToLaunch { new OO.ui.confirm( 'Are you sure you want to patrol all the page revisions?' ).then( function ( response ) {                   if ( response === true ) {                        getAndProcessRevisions;                    }                } ); }

function getAndProcessRevisions( apicontinue ) { var params = { 'action': 'query', 'format': 'json', 'prop': 'revisions', 'titles': mw.config.get( 'wgPageName' ), 'formatversion': 2, 'rvprop': 'ids', 'rvlimit': 50 };               if ( apicontinue ) { $.extend( params, apicontinue ); }               new mw.Api .get( params ) .then( function ( data ) {                       var revisions = data.query.pages[ 0 ].revisions;                        markRevisionsAsPatrolled( revisions )                            .then( function ( shouldContinue ) { if ( shouldContinue && data[ 'continue' ] ) { getAndProcessRevisions( data[ 'continue' ] ); } else { window.location.reload; }                           } )                            .fail( function ( error ) { mw.notify( 'Something went wrong: ' + error, { title: 'Smart Patrol', type: 'error' } ); } );                   } )                    .fail( function ( error ) {                        mw.notify( 'Something went wrong: ' + error, { title: 'Smart Patrol', type: 'error' } );                    } ); }

function markRevisionsAsPatrolled( revisions ) { var deferred = $.Deferred; markOneAsPatrolled( revisions, 0, deferred ); return deferred.promise; }

function markOneAsPatrolled( revisions, index, deferred ) { var revid = revisions[ index ].revid; new mw.Api .postWithToken( 'patrol', {                       'action': 'patrol',                        'revid': revid                    } ) .then( function ( info ) {                       console.log( 'Successfully patrolled: ' + revid );                        if ( revisions[ index + 1 ] ) {                            markOneAsPatrolled( revisions, index + 1, deferred );                        } else {                            deferred.resolve( true );                        }                    } ) .fail( function ( error ) {                       if ( error === 'notpatrollable' ) {                            console.log( "Can't be patrolled as it's too old: " + revid );                            deferred.resolve( false );                        } else {                            deferred.reject( error );                        }                    } ); }

} );   } ); }

/////////////////////////////////////// // Purge tab function purge {//Here, we declare the function and its parameters var hist; var url;//Here, we declare two variables if (!(hist = document.getElementById('ca-history') )) return;	//Try to set hist as the document's element //with id 'ca-history' - tha's the history tab if (!(url = hist.getElementsByTagName('a')[0] )) return;	//Try to set url as the history tab's first //element with the tag 'a'	if (!(url = url.href )) return;	//Try to set url as the href of that element from line 3 mw.util.addPortletLink('p-cactions',	//add a PortletLink to the p-cactions area (the tabs at the top of the page)			url.replace(/([?&]action=)history([&#]|$)/, '$1purge$2'),	//replace the ?action=history											//part of the href we got from											//the history tab with ?action=purge,											//and set it as the destination			'purge',				//set the text that will appear on the tab			'ca-purge',				//set the internal id for the tab - this is useful for								//referring to it, and positioning tabs relative to one another			'Purge server cache for this page',	//set the mouseover text			'0');					//the access key } if ( wgCanonicalNamespace != "Special" ) addOnloadHook(purge);	//if it's not a special page then hook onto the page loading //and run the function purge