User:Supadawg/secedit.js: Difference between revisions

Content deleted Content added
Supadawg (talk | contribs)
mNo edit summary
Supadawg (talk | contribs)
mNo edit summary
 
(17 intermediate revisions by the same user not shown)
Line 1:
/////////////////////////////////////////
var body;
// By Alek Storm
var xmlhttp;
// Please see talk page for instructions
var xmlhttp2;
/////////////////////////////////////////
var startNode;
 
var editSec;
var body; // shortcut for body node
var editForm;
var xmlhttp; // XMLHTTPRequest object
var preview;
var startNode; // div that includes section header and edit link
var oldContent;
var xmlhttpDoneeditSec; =// false;edit link
var editForm; // spliced edit form
var preview; // spliced preview or diff content
var oldContent; // original content of section
var xmlhttpDone = false; // kludge to prevent multiple calls to callback
 
importScript("User:Supadawg/util.js");
 
function inc(path) {
var lt = String.fromCharCode(60);
var gt = String.fromCharCode(62);
document.writeln(lt+'script type="text/javascript" src="/w/index.php?title='+path+'&action=raw&ctype=text/javascript&dontcountme=s"'+gt+lt+'/script'+gt);
}
 
function initSecEdit()
Line 15 ⟶ 27:
// apply to all divs of class "editsection"
var editSecs = document.getElementsByTagName("span");
var secCount = 01;
var pagetitleRe=/\/(wiki\/|w\/index\.php\?title=)([^&?]*)/; // from [Wikipedia:WikiProject User scripts/Techniques]
for ( var i = 0; i < editSecs.length; i++ ) {
if ( editSecs[i].getAttribute("class") == "editsection" ) {
for ( var k = 0; k < editSecs[i].childNodes.length; k++ ) {
if ( editSecs[i].childNodes[k].nodeName == "A" ) {
// grab editing uri, escape it, then put it back in
var editURI = editSecs[i].childNodes[k].getAttribute("href");
var editURI = "http://en.wikipedia.org/w/index.php?title="+encodeURIComponent2(pagetitleRe.exec(decodeURI(editSecs[i].childNodes[k].getAttribute("href")))[2]).replace(/\"/gi, "%22").replace(/\'/gi, "%27")+"&action=edit&section="+secCount;
// give it a unique id
editSecs[i].childNodes[k].setAttribute( "id", "editSection"+secCount );
// swap the href with a function call, passing the original href as the second parameter
editSecs[i].childNodes[k].setAttribute( "href", "javascript:editSection( document.getElementById('editSection" + secCount++ + "'), 'http://en.wikipedia.org"+editURI+"' );" );
secCount++;
}
}
Line 31 ⟶ 46:
}
 
// called on click of section edit link click
function editSection( elem, editURI )
{
cancelEdit(); // get rid of any other sections being edited
// initiate xmlhttprequest for section edit page
editSec = elem;
startNode = elem.parentNode.parentNode;
 
// initiate xmlhttprequest for section edit page
xmlhttpDone = false;
xmlhttp = null // kludge
xmlhttp = createXMLHTTP( "GET", editURI, stateChange );
}
 
// put raw input returned from XMLHTTPRequest into a div so we can grab specific elements
function makeDiv( rawHTML )
{
var div = createNode( body, "div", {style: "visibility: hidden; position: absolute;"} );
div.innerHTML = rawHTML.replace(/<script[^>]*><\/script>/gi, ""); // if script tags are placed into the DOM, they couldforce screwreload of files, and nasty things uphappen
return div;
}
Line 53 ⟶ 72:
}
 
// callback for onclick of an edit link
function stateChange()
{
Line 60 ⟶ 80:
return;
xmlhttpDone = true;
 
if ( oldContent == undefined ) {
// store old content of section - loop until we hit header of same spot in hierarchy
if ( !oldContent == undefined ) {
oldContent = makeDiv("");
var curElem = startNode.nextSibling;
Line 88 ⟶ 110:
var div = makeDiv( xmlhttp.responseText );
editForm = $("editform");
// change onclick of preview and diff buttons to our function
$("wpPreview").setAttribute( "type", "button" );
$("wpPreview").setAttribute( "onclick", "javascript:getEditData( previewChanged, $('wpPreview') );" );
Line 104 ⟶ 127:
}
 
// firefox hack, not sure if this is a problem in other browsers
function encodeURIComponent2( textcontent )
{
// from [http://en.wikipedia.org/wiki/User:Topaz/wputil.js]
return encodeURIComponent( text.replace( /\&amp\;/g, "&" ) );
content = content.replace(/\&lt\;/gi, "<");
content = content.replace(/\&gt\;/gi, ">");
content = content.replace(/\&quot\;/gi, "\"");
content = content.replace(/\&amp\;/gi, "&");
return encodeURIComponent( text.replace( /\&amp\;/g, "&" )content );
}
 
// encode differently based on type of form element
function field2Post( node, allowButton )
{
Line 114 ⟶ 144:
switch ( node.nodeName ) {
case "TEXTAREA":
reqBody += "&"+node.getAttribute("name")+"="+encodeURIComponent2( node.innerHTMLvalue );
break;
case "INPUT":
Line 123 ⟶ 153:
}
else if ( allowButton || (inputType != "submit" && inputType != "button") )
reqBody += "&"+node.getAttribute("name")+"="+encodeURIComponent2( node.getAttribute("value") );
break;
case "DIV":
Line 132 ⟶ 162:
}
 
// manually encodes a form element for XMLHTTPRequest
function form2Post( node )
{
Line 140 ⟶ 171:
}
 
// get preview or diff data
function getEditData( callback, clickedBut )
{
xmlhttpDone = false;
xmlhttp = undefinednull; // hackkludge
var action = editForm.getAttribute("action");
xmlhttp = createXMLHTTP( "POST", "http://en.wikipedia.org"+action, callback, {
Line 154 ⟶ 186:
}
 
// callback for preview data
function previewChanged()
{
Line 173 ⟶ 206:
}
 
// callback for diff data
function diffChanged()
{
Line 192 ⟶ 226:
}
 
// remove form and preview or diff data
function cancelEdit()
{
Line 197 ⟶ 232:
removeNode( preview );
preview = null;
removeNodeif ( editForm );
removeNode( editForm );
oldContent.setAttribute( "style", "position: static; visibility: visible;" );
var editForm = null;
insertAfter( oldContent, startNode );
if ( oldContent ) {
editSec.setAttribute( "href", editSec.getAttribute("oldHref") );
oldContent.setAttribute( "style", "position: static; visibility: visible;" );
editSec.innerHTML = "edit";
insertAfter( oldContent, startNode );
}
var oldContent = null;
var if ( editSec; ) {
editSec.setAttribute( "href", editSec.getAttribute("oldHref") );
editSec.innerHTML = "edit";
}
}