User:Splarka/formfiller.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

/* ********************************************************************************************
Form Filler Inner 0.0.2,
- base dev page: http://en.wikipedia.org/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://en.wikipedia.org/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;
}