Jump to content

User:Enterprisey/set-js-prefs.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.
if( mw.config.get("wgNamespaceNumber") === 2 &&
        mw.config.get( "wgTitle" ).endsWith( "/common.js" ) &&
        document.getElementById( "ca-edit" ).textContent.startsWith( "Edit" ) ) {
    mw.loader.using( [ "mediawiki.util", "mediawiki.api" ] ).then( function () {
        var api = new mw.Api();
        api.get( {
            prop: 'revisions',
            rvprop: 'content',
            rvlimit: 1,
            titles: mw.config.get( "wgPageName" )
        } ).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]["*"];

            var highlightEls = document.getElementsByClassName( "mw-highlight" );
            if( !highlightEls.length ) return;
            var codeContainer = highlightEls[0].parentNode;
            var newContainer = document.createElement( "div" );
            newContainer.style.border = "thin gray solid";
            newContainer.style.padding = "0.5em";
            newContainer.style.display = "inline-block";
            codeContainer.parentNode.insertBefore( newContainer, codeContainer );

            var wktxtLookup = {}; // id to wikitext line fragments

            function addPref( id, label, value ) {
                newContainer.innerHTML += "<div><label for='pref" + id +
                    "'>" + label + "</label><input type='text' id='pref" +
                    id + "' value='" + value + "' /></div>";
                wktxtLookup[id] = label + value;
            }

            // Find preferences in common.js, then make them into form elements
            var PREF_REGEX = /(.+?\s*=\s*)(.+?);/g;
            var prefMatch;
            var idCounter = 0;
            do {
                prefMatch = PREF_REGEX.exec( text );
                if( prefMatch && prefMatch[1] && prefMatch[1].length < 50 &&
                    prefMatch[2].length < 50) {
                    addPref( idCounter++, prefMatch[1], prefMatch[2] );
                }
            } while( prefMatch );

            if( newContainer.children.length > 0 ) {
                var saveButton = document.createElement( "button" );
                saveButton.className = "mw-ui-button mw-ui-progressive";
                saveButton.appendChild( document.createTextNode( "Save changes" ) );
                newContainer.appendChild( saveButton );
                saveButton.addEventListener( "click", function () {
                    var newWikitext = text;
                    for( var i = 0; i < newContainer.children.length - 1; i++ ) {
                        newWikitext = newWikitext.replace( wktxtLookup[i],
                            newContainer.children[i].children[0].textContent +
                            newContainer.children[i].children[1].value );
                    }

                    // Now, save it
                    api.postWithToken( "csrf", {
                        action: "edit",
                        title: mw.config.get( "wgPageName" ),
                        summary: "Setting preferences ([[User:Enterprisey/set-js-prefs|set-js-prefs]])",
                        text: newWikitext
                    } ).done ( function ( data ) {
                        if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
                            mw.notify( "Changed preferences!" );
                            document.location.reload( true );
                        }
                    } );
                } );
            }
        } );
    } );
}