User:SuperHamster/vector.js

From Nookipedia, the Animal Crossing wiki

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
// Script by User:Enterprisey on the English Wikipedia
// Licensed under CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0/)
// Documentation: https://en.wikipedia.org/wiki/User:Enterprisey/talk-tab-count
// Source: https://en.wikipedia.org/wiki/User:Enterprisey/talk-tab-count.js

$( function() {
    if( !document.getElementById( "ca-talk" ) ) return;

    // Insert count early
    var talkTabLink = document.getElementById( "ca-talk" ).childNodes[0].childNodes[0];
    talkTabLink.textContent += " (?)";

    // Determine the name of this page's associated talk page.
    // (If we're already on one, then talkPageName is set to wgPageName.)
    // The " | 1" part is to set the 1's place to a 1, which guarantees a talk namespace.
    var talkPageNamespace = mw.config.get( "wgFormattedNamespaces" )[ mw.config.get( "wgNamespaceNumber" ) | 1 ];
    var talkPageName = talkPageNamespace + ":" + mw.config.get( "wgTitle" );


    mw.loader.using( [ "mediawiki.api" ] ).then( function () {

        // Get the text of the talk page and count the level-2 headers
        ( new mw.Api() ).get( {
            prop: 'revisions',
            rvprop: 'content',
            rvlimit: 1,
            titles: talkPageName
        } ).done( function ( data ) {
            if ( !data.query || !data.query.pages ) return;
            var pageid = Object.getOwnPropertyNames( data.query.pages )[0],
                revisions = data.query.pages[pageid].revisions;

            if( revisions === undefined ) {

                // The talk page does not exist
                talkTabLink.textContent = talkTabLink.textContent.replace( "(?)", "(0)" );
            } else {
                var text = revisions[0]["*"];
                var HEADER_RE = /^\s*==\s*[^=]+?\s*==\s*$/gm;
                var headerMatchList = text.match( HEADER_RE );
                var headerCount = headerMatchList ? headerMatchList.length : 0;
                talkTabLink.textContent = talkTabLink.textContent.replace( "(?)", "(" + headerCount + ")" );
            }
        } );
    } );
} );

/*
* 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
* {{Projet:JavaScript/Script|Smart patrol}}
*/

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 );
                        }
                    } );
            }

        } );
    } );
}