User:Mr.Z-man/test.js

// TODO: // The other actions that there are buttons for // action=query would be nice // selector for result format // human/wikitext friendly output option (plain list, numbered list, table) // use an array to store query URLs so you can recall old ones // a checkbox handler that works

var querystring = 'action='; var querynum = 1; function APIrequest { document.title = 'API Query'; document.getElementsByTagName('h1')[0].innerHTML = "API Query"; form = ' '+ ' API Query: Action: '+ ''+ ''+ ' '+ ''+ ''+ ''+ ' '+   '  Result:  ';

document.getElementById('bodyContent').innerHTML = form; }

function doActionSitematrix { doRequest("action=sitematrix"); }

function doActionExpandtemplates { form = ' Page title (for context):  '+ ' Wikitext:  '+ ''+ ' '; document.getElementById('APIformInner').innerHTML += form; }

function doActionParse { form = ' Page title (for context):  '+ ' Wikitext:  '+ ' Page name:  '+ ' Oldid:  '+ 'Text '+ 'Langlinks '+ '<input type="checkbox" name="prop" value="categories">Categories '+ '<input type="checkbox" name="prop" value="links">Links '+ '<input type="checkbox" name="prop" value="templates">Templates '+ '<input type="checkbox" name="prop" value="images">Images '+ '<input type="checkbox" name="prop" value="externallinks">External links '+ '<input type="checkbox" name="prop" value="sections">Sections '+ '<input type="checkbox" name="prop" value="revid">Revid '+ '<input type="hidden" id="querytype" value="parse"/><input type="hidden" id="checkname" value="prop"/>'+ '<input type="button" value="Submit" onclick="makeQuery"/> '; document.getElementById('APIformInner').innerHTML += form; }

function getCheckBoxData { name = document.getElementById('checkname').value; action = document.getElementById('querytype').value; inputs = document.getElementById(action).getElementsByTagName('input'); output = "&"+name+"=" isfirst = true; for (var i=0; i<inputs.length-1; i++) { if (inputs[i].type = "checkbox" && inputs[i].name == name) { if (inputs[i].checked) { alert(inputs[i].value); //For some reason, this only works with this alert here... thing = inputs[i].value; if (isfirst) { output+=thing; isfirst = false } else { output+="|"+thing; }     }    }  }  if (output != "&"+name+"=" ) { return output; } else { return ''; } }

function makeQuery { action = document.getElementById('querytype').value; params = document.getElementById(action).getElementsByTagName('input'); querystring += action; for (var i=0; i<params.length-1; i++) { if (params[i].type != "checkbox" && params[i].type != "hidden" && params[i].value != '') { querystring+= '&' + params[i].id + '=' + params[i].value; } }  text = document.getElementById(action).getElementsByTagName('textarea')[0]; if (text != null) { if (text.value != '') { querystring+= '&text=' + text.value; } }  if (document.getElementById('checkname') != null) { querystring+=getCheckBoxData; } oldform = document.getElementById('APIformInner').removeChild(document.getElementById(action)); alert(querystring); doRequest(querystring); }

function doActionQuery {

}

function doRequest(parameters) { querystring = 'action='; url = mw.config.get('wgServer')+mw.config.get('wgScriptPath')+"/api.php";

a=sajax_init_object; a.onreadystatechange = writeresults; a.open('POST', url, true); a.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); a.setRequestHeader("Content-length", parameters.length); a.setRequestHeader("Connection", "close"); a.send(parameters); }

function writeresults { var result=''; if (a.readyState == 4) { if (a.status == 200) { result = a.responseText; } else { alert('There was a problem with the request.'); } }  document.getElementById('query-result').style.display = ''; document.getElementById('query-result-inner').innerHTML=result; }

if (mw.config.get('wgPageName') == "Special:API") { addOnloadHook(APIrequest); }