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 23:26, 16 January 2006 (use xml). 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

// idea 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

// but rewritten using AJAX

// <pre><nowiki>

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

    addTab('javascript:show_diff_since()', 'since', 'ca-since', "Changes since I last edited");
}

function show_diff_since() {
    var tab = document.getElementById('ca-since');
    // show the user we're doing something
    tab.innerHTML = '<b>since...</b>';

    var url = wikiPage.qurl + '&action=history';
    var req = new XMLHttpRequest();
    // using onload instead of onreadystatechange allows multiple asynchronous requests
    req.onload = show_diff_since_handleHistoryPage;
    req.overrideMimeType('text/xml');
    req.open("GET", url, true);
    req.send(null);
}

// <li>(<a href="/w/index.php?title=ARTICLETITLE&amp;diff=35459147&amp;oldid=35392123" title="ARTICLETITLE">cur</a>) *
// \(<a href[^>]+?> *<input[^>]+?>
//  <span class='history-user'><a href="/wiki/User:Quarl" title="User:Quarl">Quarl</a></span>

function show_diff_since_handleHistoryPage(event) {
    var req = event.target;
    if (req.readyState == 4) {
        if ((req.status == 200)) {
            // window.username is defined in wikipage.js
            //var re = '<li>\\(<a href="(/w/index\\.php\\?title=[^&"]+?&amp;diff=[0-9]+?&amp;oldid=[0-9]+?)" title="[^"]+?">cur</a>\\)[^\n]+?<span class=\'history-user\'><a href="[^"]+?" title="[^"]+?">'+window.username+'</a></span>';

            /*if (req.responseText.match(new RegExp(re))) {
                var url = RegExp.$1.replace('&amp;','&');
                document.location = url;
            } else {
                alert("Couldn't find your entry in the history page");
                // TODO: perhaps retry with a larger limit
            }*/
            show_diff_since_examineTree(req.responseXML);
        } else {
            alert("Error downloading history page");
        }
    }
}

function show_diff_since_examineTree(doc) {
    var hists = doc.getElementById("pagehistory").childNodes;
    for (n=0;n<hists.length;n++) {
        if (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>