Jump to content

User:Manishearth/GadgetUS/generate.js

From Wikipedia, the free encyclopedia
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.
//Tabbed page stuff
var prefStarted=false
var inter;
function startPref(page,title,heading,formID,handler){
	if(mw.config.get('wgPageName')==page&&document.getElementById("bodyContent")){
		document.getElementById("bodyContent").innerHTML="<FORM class=visualClear id="+formID+"><UL ID=preftoc></UL><DIV ID=preferences class=jsprefs></DIV></FORM><DIV class=visualClear>"
		document.getElementById("firstHeading").innerHTML=heading
		document.title=title
		prefStarted=true;
		if(handler){
			handler()
		}
	}
}
var sections = 0
function addSection(buttonName,sectionText){
	if(!prefStarted){
		setTimeout("addSection('"+buttonName+"','"+sectionText+"')",1000)
		return;
	}
	document.getElementById('preftoc').innerHTML+="<LI ID='prefbutton-"+sections+"'><A HREF='#prefsection-"+sections+"' onclick='openSection("+sections+")'>"+buttonName+"</A></LI>"
	document.getElementById('preferences').innerHTML+="<fieldset id='prefsection-"+sections+"' class=prefsection>"+sectionText+"</fieldset>"
 
	
	sections++
	openSection(0)
	
	
 
}
 
function openSection(section){
	if(!prefStarted){return}
	for(i=0;i<sections;i++){
		document.getElementById("prefbutton-"+i).className=""
		document.getElementById("prefsection-"+i).style.display="none"
	}
	document.getElementById("prefbutton-"+section).className="selected"
	document.getElementById("prefsection-"+section).style.display=""
}
startPref("Special:Scripts","Userscripts","Userscripts","USForm",populateUSPage)

//Populate the page
function populateUSPage(){
	if(typeof USGroups=="undefined"){
		setTimeout("populateUSPage()",100)
		return;
	}
	for(var i=0;i<USGroups.length;i++){
	var USPageContent="<table>";
	with(USGroups[i]){
		for(var j=0;j<scripts.length;j++){
			USPageContent+="<tr> <td width=20px><input type=checkbox id='scriptbox-"+scripts[j].id+"' name='scriptbox-"+scripts[j].id+"'></td> <td width=90px>"+scripts[j].title+"</td> <td>"+scripts[j].description+"</td> </tr>"
		}
	USPageContent+="</table>"
	addSection(title,USPageContent)
	}		
	}
	inter=setInterval(checkBoxes,1000)

	
	document.getElementById("USForm").innerHTML+="<input type=button name='forgetme-save' value='Save' onclick='saveUS()'>"
	//forgetme tells the script not to import it

}

function checkBoxes(){
var undefined;
if(importsLoaded===undefined){
return;
}
clearInterval(inter);
for(i=0;i<myScripts.length;i++){
		document.getElementById("scriptbox-"+myScripts[i]).checked=true;
	}

}

function saveUS(){
var _api = sajax_init_object();
_api.open('GET', 'http://en.wikipedia.org/w/index.php?title=User:'+mw.config.get('wgUserName')+'/GadgetUSimports.js&action=render', true);
_api.onreadystatechange = function(){checkFirst(_api)};
_api.send(null);
}

function checkFirst(_api){
if(_api.readyState==4) {
	if(_api.status==200){
		if(_api.responseText.indexOf(importsHeader)==-1){
			var pageContent="\/\*"+importsHeader+"\*\/\n\/\/<scr>\nvar myScripts=[]\n\/\/</scr>\n\/\/<imports>\n\/\/</imports>"
			editPage("User:"+mw.config.get('wgUserName')+"/GadgetUSimports.js",pageContent)

		}
		condenseFormData()
	}
}
}

function condenseFormData(){
var frmElmt=document.getElementById("USForm").elements
for(var i=0;i<frmElmt.length;i++){


}
}



var api = sajax_init_object();
function editPage(page,text,handler){
	if(!handler){
		handler=function(){}
	}
	api.open('GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&prop=info&indexpageids=1&intoken=edit&titles=Whatever', true);
	api.onreadystatechange = function(){extract_token(page,text,handler)};
	api.send(null);

}
function extract_token(page,text,handler) {
	if(api.readyState==4) {
		if(api.status==200) {
			var response = eval('(' + api.responseText + ')');
			var token = response['query']['pages'][response['query']['pageids'][0]]['edittoken'];
			edit_page(token,page,text,handler);
		}
		else {
			alert('The token query returned an error.');
		}
	}
}
 
// edit page (must be done through POST)
function edit_page(_token,page,text,handler) {
	var parameters = 'action=edit&title='+page+'&text='+text+'&token=' + encodeURIComponent(_token);
	api.open('POST', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php', true); // just reuse the same query object
	api.onreadystatechange = editOver;
	api.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	api.setRequestHeader('Connection', 'keep-alive');
	api.setRequestHeader('Content-length', parameters.length);
	api.send(parameters);
	function editOver(){
		if(api.readyState==4) {
			if(api.status==200) {
				handler()
			}
			else {
				alert('Error saving');
				location.reload();
			}
		}

	}

}