Jump to content

User:Splarka/formfiller.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Splarka (talk | contribs) at 07:22, 27 December 2007 (Created page with '/* ******************************************************************************************** Form Filler Inner 0.0.1 Work in progress, text-example of a form-fi...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
/* ********************************************************************************************
Form Filler Inner 0.0.1

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 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: a bit insecure): 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') addOnloadHook(formFillers)
function formFillers() {
  //text inputs, eg &mwProtect-reason=foo
  var inputs = ['mwProtect-reason','mwProtect-cascade','expires'];
  //checboxes/radioboxes, eg &mwProtect-cascade=1
  var checks = ['mwProtect-cascade','mwProtectWatch','mwProtectUnchained','wpmin'];
  //select boxes/dropdown boxes (parameter is zero-base index), eg &mwProtect-level-edit=2
  var selects = ['mwProtect-level-edit','mwProtect-level-move'];

  for(var i=0;i<inputs.length;i++) {
    if(queryString(inputs[i]) && document.getElementById(inputs[i])) {
      document.getElementById(inputs[i]).value = queryString(inputs[i]);
    }
    //if(queryString(inputs[i]) && document.getElementsByName(inputs[i])[0]) {
    //  document.getElementsByName(inputs[i])[0] = queryString(inputs[i]);
    //}
  }

  for(var i=0;i<checks.length;i++) {
    if(queryString(checks[i]) && document.getElementById(checks[i])) {
      var chk = queryString(checks[i]);
      if(chk='true'||chk=='on'||chk=='1') {
        document.getElementById(checks[i]).checked = true;
      } else {
        document.getElementById(checks[i]).checked = false;
      }
    }
  }

  for(var i=0;i<selects.length;i++) {
    if(queryString(selects[i]) && document.getElementById(selects[i])) {
      document.getElementById(selects[i]).selectedIndex = queryString(selects[i]);
    }
  }

  if(queryString('submitForm') && document.getElementById(queryString('submitForm')) && window.submitByJsEnabled) {
    document.getElementById(queryString('submitForm')).click();
  }
}


function queryString(p) {
  var re = RegExp('[&?]' + p + '=([^&]*)');
  var matches;
  if (matches = re.exec(document.location)) {
    try { 
      return decodeURI(matches[1]);
    } catch (e) {
    }
  }
  return null;
}