Content deleted Content added
mNo edit summary |
mNo edit summary |
||
(17 intermediate revisions by the same user not shown) | |||
Line 1:
/////////////////////////////////////////
// By Alek Storm
// Please see talk page for instructions
/////////////////////////////////////////
var editSec;▼
var body; // shortcut for body node
var editForm;▼
var xmlhttp; // XMLHTTPRequest object
var startNode; // div that includes section header and edit link
var oldContent;▼
var
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 =
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 = "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§ion="+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
secCount++;
}
}
Line 31 ⟶ 46:
}
// called on click of section edit link
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
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
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(
{
// from [http://en.wikipedia.org/wiki/User:Topaz/wputil.js]
return encodeURIComponent( text.replace( /\&\;/g, "&" ) );▼
content = content.replace(/\<\;/gi, "<");
content = content.replace(/\>\;/gi, ">");
content = content.replace(/\"\;/gi, "\"");
content = content.replace(/\&\;/gi, "&");
}
// 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.
break;
case "INPUT":
Line 123 ⟶ 153:
}
else if ( allowButton || (inputType != "submit" && inputType != "button") )
reqBody += "&"+node.getAttribute("name")+"="+encodeURIComponent2( node.
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 =
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;
removeNode( editForm );
oldContent.setAttribute( "style", "position: static; visibility: visible;" );▼
insertAfter( oldContent, startNode );▼
if ( oldContent ) {
editSec.setAttribute( "href", editSec.getAttribute("oldHref") );▼
▲ oldContent.setAttribute( "style", "position: static; visibility: visible;" );
editSec.innerHTML = "edit";▼
▲ insertAfter( oldContent, startNode );
}
▲ editSec.setAttribute( "href", editSec.getAttribute("oldHref") );
▲ editSec.innerHTML = "edit";
}
}
|