User:Jorgenev/AJAXSectionEditor.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Jorgenev/AJAXSectionEditor. |
var editing=false;
var edittoken = null;
var PageName = null;
function cancel()
{
var element = document.getElementById("editing1");
element.parentNode.removeChild(element);
editing = false;
}
function save(section)
{
section+=1
$.ajax({
url: wgScriptPath + '/api.php',
data: {
'action': 'edit',
'title': PageName,
'section': section,
'summary': document.getElementById("editsummary1").value,
'text': document.getElementById("thesectiondata").value,
'format': 'json',
'minor' : document.getElementById("minor1").checked,
'token': edittoken
},
dataType: 'json',
type: 'POST',
success: function( data ) {
if ( data.edit && data.edit.result == 'Success' ) {
cancel();
window.location.reload(); // reload page if edit was successful
} else if ( data.error ) {
alert( 'Error: API returned error code' + data.error.code + ' with info ' + data.error.info );
} else {
alert( 'Error: Unknown result from API.' );
}
},
error: function( xhr ) {
alert( 'Error: Edit failed.' );
}
});
}
function initsection(count)
{
editsection(count);
setTimeout("updateheight()",300);
setTimeout("updateheight()",500);
setTimeout("updateheight()",2000);
}
function editsection(header)
{
if(editing)
{return;}
editing=true;
$.getJSON(
wgScriptPath + '/api.php?',
{
action: 'query',
prop: 'info',
intoken: 'edit',
titles: PageName,
indexpageids: '',
format: 'json'
},
function( data ) {
if ( data.query.pages && data.query.pageids ) {
var pageid = data.query.pageids[0];
edittoken = data.query.pages[pageid].edittoken;
}
}
)
$.get('http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles='+PageName+'&rvprop=content&rvsection='+(header+1)+'&format=xml', function(data) {
var ccontent = $(data).find("rev").text();
document.getElementsByClassName("mw-headline")[header].innerHTML = document.getElementsByClassName("mw-headline")[header].innerHTML + "<table id=\"editing1\" width=\"100%\"><tr><td colspan=3><textarea id=\"thesectiondata\" onkeyup=javascript:updateheight();>"+ccontent+"</textarea></td><td width=\"5px\"/></tr><tr><td width=\"200px\"><button type=\"button\" onclick=javascript:save("+header+")>Submit</button><button type=\"button\" onclick=javascript:cancel()>Cancel</button><input id=\"minor1\" type=\"checkbox\" name=\"minor1\" value=\"minor1\" /><label for=\"minor1\"><small>Minor</small></label> </td><td colspan=2><input id=editsummary1 type=\"text\" style=\"width:100%; display:block;\" /></span></td></tr></table>";
})
}
function updateheight()
{
var h1 = document.getElementById("thesectiondata").style.height.replace("px","");
var h = parseInt(h1)-25;
var sh = document.getElementById("thesectiondata").scrollHeight;
if(!h1 || h < sh)
{
document.getElementById("thesectiondata").style.height = 10+"px";
document.getElementById("thesectiondata").style.height = (25+document.getElementById("thesectiondata").scrollHeight)+"px";
}
}
$(function (){
var i = 0;
while(i<document.getElementsByClassName("editsection").length)
{
var elem = document.getElementsByClassName("editsection")[i].getElementsByTagName("a")[0];
var page = String(elem.href).substring(String(elem.href).search("title=")+6,String(elem.href).search("&"));
document.getElementsByClassName("editsection")[i].innerHTML = "[<a href=\"javascript:PageName='"+page+"'; initsection("+i+");\" >edit</a>]";
i++;
}
});