Jump to content

User:Quarl/show diff since.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Quarl (talk | contribs) at 22:52, 29 January 2006 (todo). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// User:Quarl/show_diff_since.js - add 'since' tab to show change since I last edited
//    Compared to JesseW's, this is an asynchronous version that doesn't first open the history page

// requires: wikipage.js, addlilink.js

// based on http://en.wikipedia.org/wiki/Wikipedia:WikiProject_User_scripts/Scripts/Changes_since_I_last_edited
// from http://en.wikipedia.org/w/index.php?title=User:JesseW/monobook.js&oldid=20755510

// quarl 2006-01-16 rewritten to asynchronously download history page

// TODO: use a non-async version for href, and async for onClick

// <pre><nowiki>

function addTab_DiffSince() {
    if (wikiPage.nsSpecialP) return;

    var url;
    if (historyP) {
        // already looking at history page
        url = 'javascript:show_diff_since_thisHistoryPage()';
    } else {
        // not yet a history page -- asynchronously download it first
        url = 'javascript:show_diff_since_thisPage()';
    }

    addTab(url, 'since', 'ca-since', "Show changes since I last edited");
}

function show_diff_since_thisPage() {
    show_diff_since(wikiPage, document.getElementById('ca-since'), ' <b>since...</b> ');
}

function show_diff_since(wp, statusnode, statustext) {
    var url = wp.qurl + '&action=history';
    var req = new XMLHttpRequest();

    if (statusnode) {
        // change the button to show the user we're doing something and prevent another click
        req.statusnode = statusnode;
        req.save_text = statusnode.innerHTML;
        statusnode.innerHTML = statustext;
    }

    req.onload = show_diff_since_handleHistoryPage;
    req.overrideMimeType('text/xml');
    req.open("GET", url, true);
    req.send(null);
}

function show_diff_since_handleHistoryPage(event) {
    var req = event.target;
    if (req.readyState == 4) {
        // restore button in case this fails, so user can click again
        if (req.statusnode) {
            req.statusnode.innerHTML = req.save_text;
        }

        if (req.status == 200) {
            show_diff_since_examineTree(req.responseXML);
        } else {
            alert("Error downloading history page!");
        }
    }
}

function show_diff_since_thisHistoryPage() {
    show_diff_since_examineTree(document);
}

function show_diff_since_examineTree(doc) {
    if (!doc) { alert ("## no doc?!"); return; }
    var hists = doc.getElementById("pagehistory").childNodes;
    if (!hists) { alert("Couldn't parse history from page"); return; }
    for (n=0;n<hists.length;n++) {
        // window.username is defined in wikipage.js
        if (hists[n].getElementsByTagName && hists[n].getElementsByTagName("span")[0].textContent==window.username) {
            document.location = hists[n].childNodes[1].href; 
            return;
        }
    }

    alert("Couldn't find your entry in the history page!");
    // TODO: perhaps retry with a larger limit
}

addOnloadHook(addTab_DiffSince);

// </nowiki></pre>