Jump to content

User:Enterprisey/sync-template-sandbox.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Enterprisey (talk | contribs) at 17:56, 20 April 2016 (add some more log spamming). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
console.log( "HI" );
document.addEventListener( "DOMContentLoaded", function () {
    console.log( mw.config.get( "wgNamespaceNumber" ) );
    console.log( mw.config.get( "wgPageName" ) );
    console.log( mw.config.get( "wgPageName" ).includes( "/sandbox" ) );
    if ( mw.config.get( "wgNamespaceNumber" ) === 10 && mw.config.get( "wgPageName" ).includes( "/sandbox" ) ) {
        var link = mw.util.addPortletLink(
            "p-cactions",
            "",
            "Sync with main",
            "ca-sync",
            "Overwrite the sandbox code with the code from the main template"
        );
        mw.loader.using( "mediawiki.api", function () {
            link.addEventListener( "click", function () {
                var api = new mw.Api();
                api.get( {
                    prop: 'revisions',
                    rvprop: 'content',
                    rvlimit: 1,
                    titles: mw.config.get( "wgPageName" ).replace( "/sandbox", "" )
                } ).done( function ( data ) {
                    if ( !data.query || !data.query.pages ) return;
                    var pageid = Object.getOwnPropertyNames( data.query.pages )[0],
                        text = data.query.pages[pageid].revisions[0]["*"];
                    console.log( "GOT TEXT" );
                    api.post( {
                        action: "edit",
                        title: mw.config.get( "wgPageName" ),
                        text: text,
                        summary: "Syncing sandbox code with main template ([[User:APerson/sync-template-sandbox|sync-template-sandbox.js]])"
                    } ).done( function ( data ) {
                        console.log( "EDIT RETURNED" );
                        document.location.reload( true );
                    } );
                } );
            } );
        } );
    }
} );