User:Jorgenev/AJAXSectionEditor.js: Difference between revisions

Content deleted Content added
Created page with 'importScript('User:JorgenevALT/vector.js');'
 
m Maintenance: Replacing addOnloadHook with native jQuery (mw:ResourceLoader/Migration_guide_(users)#addOnloadHook - phab:T130879)
 
(47 intermediate revisions by one other user not shown)
Line 1:
var editing=false;
importScript('User:JorgenevALT/vector.js');
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++;
}
 
});