Jump to content

User:Czar/delsort+findvgsources.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>
importScript( 'User:Enterprisey/delsort.js' ); // Backlink: [[User:APerson/delsort.js]] // Deletion sorting script, used to use [[WP:FWDS]], see [[Wikipedia:WikiProject Deletion sorting#Scripts and tools]]

// hook to change {{find sources}} to {{find VG sources}} at relevant video game AfDs, by Enterprisey, see [[User talk:Enterprisey#Find video game sources with Delsort script]]

window.delsortDoneHook = function () {
    var SUMMARY = "Swapping the regular find sources template for the video game-specific one";
    var VG_DELSORT_PAGE = "Wikipedia:WikiProject Deletion sorting/Video games";

    $.getJSON(
        mw.util.wikiScript('api'),
        {
            format: 'json',
            action: 'query',
            prop: 'revisions',
            rvprop: 'content',
            rvlimit: 1,
            titles: mw.config.get( 'wgPageName' )
        }
    ).done( function (data) {
        try {
            var pageId = Object.keys(data.query.pages)[0];
            wikitext = data.query.pages[pageId].revisions[0]['*'];

            // Check if page has been delsorted into the video games list
            if( wikitext.indexOf( VG_DELSORT_PAGE ) >= 0 ) {

                // Swap find sources template with the VG-specific one
                wikitext = wikitext.replace( "{{Find sources AFD|", "{{Find video game sources|" );

                // Save page
                $.ajax( {
                    url: mw.util.wikiScript( 'api' ),
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        format: 'json',
                        action: 'edit',
                        title: mw.config.get( 'wgPageName' ),
                        summary: SUMMARY,
                        token: mw.user.tokens.get( 'csrfToken' ),
                        text: wikitext
                    }
                } ).done ( function ( data ) {
                    if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
                        mw.notify( "Find sources template swapped!" );
                    } else {
                        mw.notify( "Error swapping find sources template!" );
                    }
                } )
            }
        } catch ( e ) {
            mw.notify( "Error swapping find sources template!" );
            console.log( "post-DELSORT content request error: " + e.message );
            console.log( "post-DELSORT content request response: " + JSON.stringify( data ) );
        }
    } );
};
// </nowiki>