Jump to content

User:Rahk EX/instaedit.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.
// <source lang="JavaScript">

// This is what is loaded first
function clickevent() {
var article = document.getElementById('bodyContent');
// on doubleclick go to the change function
article.addEventListener('dblclick', change, false);
}

// Make it load!
addOnloadHook(clickevent);

// Create the insertAfter function (useful)
function insertAfter(parent, node, referenceNode) {
    parent.insertBefore(node, referenceNode.nextSibling);
}

function cancel() {
    // kill the textarea
    document.getElementById('content').removeChild(theeditbox);
    // kill the cancel button
    document.getElementById('content').removeChild(cancelbutton);
    // kill the save button
    document.getElementById('content').removeChild(editbutton);
    // kill the edit summary box
    document.getElementById('content').removeChild(summarybox);
    // kill the space
    document.getElementById('content').removeChild(space);
    // kill the edit summary text
    document.getElementById('content').removeChild(summarytext);
    // kill the br's
    document.getElementById('content').removeChild(br);
    document.getElementById('content').removeChild(br2);
    // kill the minor edit box
    document.getElementById('content').removeChild(minoreditbox);
    // kill the text for minor edit
    document.getElementById('content').removeChild(minoredittext);
    // make the article visible
    document.getElementById('bodyContent').style.opacity = 1;
}

function editpage() {
    frame = document.createElement("IFRAME");
    frame.setAttribute('src', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=' + wgPageName + '&action=edit');
    frame.setAttribute('height', '1');
    frame.setAttribute('width', '1');
    document.body.appendChild(frame);
    // Below line may be useful, commented for now
    // e = frame.contentDocument;
    document.all.wpTextbox1.innerHTML = document.getElementById('editbox').innerHTML;
    document.getElementById('wpSummary').value = document.getElementById('summarybox').value; // added summarybox
    document.getElementById('wpSave').click();
    cancel();
}

// Create the change function that is called when the article is double-clicked
function change() {
    if(document.getElementById('bodyContent').style.opacity != 0.2) {
        document.getElementById('bodyContent').style.opacity = 0.2; 
        theeditbox = document.createElement('textarea');
        theeditbox.setAttribute('id', 'editbox');
        theeditbox.setAttribute('rows', '25');
        theeditbox.setAttribute('cols', '80');
        theeditbox.setAttribute('innerHTML', 'A BUG IS PRESENT, PLEASE CONTACT THE CREATOR(S) OF THIS SCRIPT. SORRY FOR THE INCONVENIENCE.');
        cancelbutton = document.createElement('button');
        cancelbutton.setAttribute('id', 'cancelbutton');
        cancelbutton.innerHTML = 'Cancel';
        cancelbutton.addEventListener('click', cancel, false);
        editbutton = document.createElement('button');
        editbutton.setAttribute('id', 'editbutton');
        editbutton.setAttribute('accesskey', 's');
        editbutton.innerHTML = '<b>Save edit</b>';
        editbutton.addEventListener('click', editpage, false);
        summarybox = document.createElement('input');
        summarybox.setAttribute('id', 'summarybox');
        summarybox.setAttribute('type', 'text');
        summarybox.setAttribute('maxlength', '200');
        summarybox.setAttribute('size', '60');
        minoreditbox = document.createElement('input');
        minoreditbox.setAttribute('id', 'minoreditbox');
        minoreditbox.setAttribute('type', 'checkbox');
        minoredittext = document.createElement('label');
        minoredittext.setAttribute('for', 'minoreditbox');
        minoredittext.setAttribute('accesskey', 'i');
        minoredittext.setAttribute('title', 'Mark this as a minor edit [i]');
        minoredittext.innerHTML = '&nbsp;This is a minor edit (<a href="/wiki/Help:Minor_edit" title="Help:Minor edit">what'+"'"+'s this?</a>)';
        br = document.createElement('br'); // br element
        br2 = document.createElement('br'); // br element
        space = document.createElement('span'); // nbsp hack
        space.innerHTML = '&nbsp;';
        summarytext = document.createElement('p');
        summarytext.innerHTML = '<a href="/wiki/Help:Edit_summary" class="internal" title="Briefly describe the changes you have made" target="_blank">Edit summary</a> <small>(Briefly describe the changes you have made)</small>';
        //insert the form elements
        document.getElementById('content').insertBefore(theeditbox, document.getElementById('bodyContent'));
        insertAfter(document.getElementById('content'), summarytext, theeditbox);
        insertAfter(document.getElementById('content'), summarybox, summarytext);
        insertAfter(document.getElementById('content'), br2, summarybox);
        insertAfter(document.getElementById('content'), minoreditbox, br2);
        insertAfter(document.getElementById('content'), minoredittext, minoreditbox);
        insertAfter(document.getElementById('content'), br, minoredittext);
        insertAfter(document.getElementById('content'), editbutton, br);
        insertAfter(document.getElementById('content'), space, editbutton);
        insertAfter(document.getElementById('content'), cancelbutton, space);
        a=sajax_init_object();
        var request = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=' + wgPageName + '&action=raw'; // helps debugging
        a.open("GET", request, true);
        a.onreadystatechange = function(){
            if(a.readyState == 4){
                document.getElementById('editbox').innerHTML = a.responseText;
            }
        }
        a.send(null);
    }
}

// </source>