User:Splarka/formfiller.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. |
Documentation for this user script can be added at User:Splarka/formfiller. |
/* ********************************************************************************************
Form Filler Inner 0.0.2,
- base dev page: http://wiki.riteme.site/wiki/User:Splarka/formfiller.js
Work in progress, text-example of a form-filler-inner, for those pesky forms that don't have
query-string parameter pre-loads. For example, action=protect (which currently has an annoying
"if( $wgRequest->wasPosted()" before checking for parameters.
This is a bare-bones script that should be mostly deprecatable by the devs writing in proper
preload routines for all form items!
If you copy this, your version may become deprecated. The in-progress version is at http://wiki.riteme.site/wiki/User:Splarka/formfiller.js (but may move to MediaWiki: namespace
if it becomes popular, if so, that page will become a redirect, etc etc).
Instructions:
* Add an onload check for the page or action that has deficient unprefillable form fields like
action=protect. Eg: for Special:Movepage:
if(wgCanonicalSpecialPageName=='Movepage') addOnloadHook(formFillers)
* Add the IDs or names of the inputs (currently supports <input> (text boxes radio and checkboxes,
as well as submits if enabled), and dropdown <select> lists) to the appropriate arrays
* Profit!
If you want to be able to autosubmit by URL, add to your local monobook.js (warning: dangerous)
var submitByJsEnabled=true;
This might be good for, say, temporarily doing a bunch of batch protects or even deletes.
This trick has been used to delete thousands of images on commons and much spam on Wikia.
The parameter then, if enabled, is &submitForm=IDOFSUBMITBUTTON.
******************************************************************************************** */
if(wgAction=='protect' || wgAction=='unblock') addOnloadHook(formFillers)
function formFillers() {
//text inputs, eg &mwProtect-reason=foo
var inputs = ['mwProtect-reason','expires','wpUnblockReason'];
//checboxes/radioboxes, eg &mwProtect-cascade=1
var checks = ['mwProtect-cascade','mwProtectWatch','mwProtectUnchained'];
//select boxes/dropdown boxes (parameter is zero-base index), eg &mwProtect-level-edit=2
var selects = ['mwProtect-level-edit','mwProtect-level-move','mwProtect-level-create'];
for(var i=0;i<inputs.length;i++) {
var obj = getElementByIdOrName(inputs[i]);
if(queryString(inputs[i]) && obj) {
obj.value = queryString(inputs[i]);
}
}
for(var i=0;i<checks.length;i++) {
var obj = getElementByIdOrName(checks[i]);
if(queryString(checks[i]) && obj) {
var chk = queryString(checks[i]);
obj.checked = (chk=='true'||chk=='on'||chk=='1') ? true : false;
}
}
for(var i=0;i<selects.length;i++) {
var obj = getElementByIdOrName(selects[i]);
if(queryString(selects[i]) && obj) {
obj.selectedIndex = queryString(selects[i]);
}
}
var subobj = getElementByIdOrName(queryString('submitForm'));
if(queryString('submitForm') && subobj && window.submitByJsEnabled) {
subobj.click();
}
}
function getElementByIdOrName(idorname,par) {
// grab the first element by this ID= or name= (won't work for radio buttons).
var parent = (!par) ? document : par
if(parent.getElementById(idorname)) {
return parent.getElementById(idorname);
} else if(parent.getElementsByName(idorname)[0]) {
return parent.getElementsByName(idorname)[0];
} else {
return false;
}
}
function queryString(p) {
var re = RegExp('[&?]' + p + '=([^&]*)');
var matches;
if (matches = re.exec(document.location)) {
try {
return decodeURI(matches[1]);
} catch (e) {
}
}
return null;
}