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/diffsince.js]] - utility functions for doing a "diff since I
// last edited"

// depends: wikipage.js, util.js

// Synopsis:
//    url = diffsince.makeUrl(wikiPage);
//    href = '<a onclick="javascript:diffsince.diffThis()" href="'+url+'">diff since</a>';

// Or:
//    url = diffsince.makeUrl(wp);
//    href = '<a onclick="javascript:diffsince.diffPageAsync(wp)" href="'+url+'">diff since</a>';


// Navigating to the value of makeUrl will open a history page, parse it, and
// jump to the appropriate diff.

// The diffPageAsync() function asynchronously downloads the history page and
// then navigates to the diff.  It is appropriate as an onClick handler.

// The diffThis() function does the diff directly if already looking at a
// history page, else calls diffPageAsync(wikiPage).

// diffPageAsync and diffThis take status_node and status_text parameters for
// showing progress.

// quarl 2006-01-16 (asynchronous code originally written in show_diff_since.js)
// quarl 2006-02-03 factored as library

// <pre><nowiki>

var diffsince = new Object();

diffsince.historyLimit = [200];

// return a URL that points to a "fakeaction" page for doing a diff.
diffsince.makeUrl = function(wp) {
    return wp.qurl + '&fakeaction=diffsince&action=history&limit='+diffsince.historyLimit[0];
}

diffsince._load = function() {
    if (WikiPage.queryVars.fakeaction == 'diffsince') {
        diffsince._parseHistory(document);
    }
}

diffsince.diffPageAsync = function(wp, statusNode, statusText) {
    var url = wp.qurl + '&action=history&limit='+diffsince.historyLimit[0];
    var req = new XMLHttpRequest();

    // change the button to show the user we're doing something and
    // prevent another click
    buttonShowStatus(statusNode, statusText); // (statusNode, statusText are optional)

    asyncDownloadXML(url, diffsince._handleHistoryPage,
                     { statusNode: statusNode });
    return false;
}

diffsince.diffThis = function(statusNode, statusText) {
    if (wikiDoc.historyP) {
        // already looking at a history page
        diffsince._parseHistory(document);
    } else {
        // not yet a history page -- asynchronously download it first
        diffsince.diffPageAsync(wikiPage, statusNode, statusText);
    }
    return false;
}

diffsince._handleHistoryPage = function(req) {
    // restore button in case this fails, so user can click again
    buttonRestoreStatus(req.statusNode);

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

diffsince._parseHistory = function(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==wikiDoc.username)
        {
            document.location = hists[n].childNodes[1].href;
            return;
        }
    }

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

$(diffsince._load);

// </nowiki></pre>