User:Enterprisey/set-js-prefs.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Enterprisey/set-js-prefs. |
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 );
}
} );
} );
}
} );
} );
}