User:Splarka/electrocute.js

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button. Firefox: hold down the Shift key while clicking Reload (or press Ctrl-Shift-R). Google Chrome and Safari users can just click the Reload button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
addOnloadHook(function() {
  addPortletLink('p-tb','/wiki/Special:Electrocute','Electrocutions');
});

if(wgPageName == 'Special:Electrocute') {
  document.title = 'User electrocution';
  appendCSS('#content {visibility:hidden;}');
  addOnloadHook(electrocuteForm);
}

function electrocuteForm() {
  var con = (document.getElementById('content')) ? document.getElementById('content') : document.getElementById('mw_content')
  var bcon = (document.getElementById('bodyContent')) ? document.getElementById('bodyContent') : document.getElementById('mw_contentholder')
  var fh = getElementsByClassName(con,'h1','firstHeading')[0];
  while(fh.firstChild) fh.removeChild(fh.firstChild)
  fh.appendChild(document.createTextNode('User electrocution'));
  for(var i=0;i<bcon.childNodes.length;i++) {
    bcur = bcon.childNodes[i];
    if(bcur.id != 'siteSub' && bcur.id != 'contentSub' && bcur.className != 'visualClear') {
      while(bcur.firstChild) bcur.removeChild(bcur.firstChild)
      if(bcur.nodeType == 3) bcur.nodeValue = '';
    }
  }
  if(queryString('submit')) {
    bcon.innerHTML += '<img src="http://upload.wikimedia.org/wikipedia/en/thumb/a/a6/Shock_sign.jpg/200px-Shock_sign.jpg" style="border:1px dashed red;" /><br>'
    + '<b style="color:#990000;">User "<i>' + queryString('user') + '</i>" has had an electric shock per the following parameters:'
    + '<dl><dd> voltage = ' + queryString('voltage')
    + '<br> amperage = ' + queryString('amperage')
    + '<br> type = ' + queryString('acdc')
    + '<br> duration = ' + queryString('duration')
    + '</dd></dl>'
  }

  //Note: I was writing this as nice dom, but since it is for a gag anyways, it is too much work'
  bcon.innerHTML += '<form method="get" action="' + wgServer + wgScript + '"><input name="title" type="hidden" value="Special:Electrocute" />'
    + '<fieldset><legend>Electrocute a deserving user</legend>'
    + '<label for="user">User: </label><input id="user" type="text" name="user" /><p></p>'
    + '<label for="amperage"> Amps: </label><select id="amperage" name="amperage">'
    + '<option selected="selected" value="0.001">0.001</option><option value="0.01">0.01</option><option value="0.1">0.1</option>'
    + '<option value="1">1</option><option value="10">10</option><option value="100">100</option></select>'
    + '<label for="voltage"> Voltage: </label><select id="voltage" name="voltage">'
    + '<option value="6">6</option><option value="12">12</option><option value="24">24</option>'
    + '<option value="36">36</option><option value="48">48</option><option selected="selected" value="120">120</option>'
    + '<option value="240">240</option><option value="480">480</option><option value="2000">2000</option><option value="60000">60,000</option></select>'
    + '<input id="acdc_ac" value="ac" name="acdc" type="radio" checked="checked" /><label for="acdc_ac">AC</label>'
    + '<input id="acdc_dc" value="dc" name="acdc" type="radio" /><label for="acdc_dc">DC</label> ' 
    + '<small> (<a href="http://en.wikipedia.org/wiki/Electric_shock#Lethality_of_a_shock">help</a>)</small><p></p>'
    + '<label for="duration">Duration: </label><input id="duration" type="text" name="duration" value="100 milliseconds"/><p></p>'
    + '<input type="submit" value="Apply" name="submit" />'
    + '</fieldset></form>'
  appendCSS('#content {visibility:visible;}');

  if(queryString('user')) document.getElementById('user').value = queryString('user');
  if(queryString('duration')) document.getElementById('duration').value = queryString('duration');
}

function appendCSS(text) {
  var s = document.createElement('style');
  s.type = 'text/css';
  s.rel = 'stylesheet';
  if (s.styleSheet) s.styleSheet.cssText = text //IE
  else s.appendChild(document.createTextNode(text + '')) //Safari sometimes borks on null
  document.getElementsByTagName('head')[0].appendChild(s);
  return s;
}

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