Jump to content

User:Animum/watchlistUpdate.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Animum (talk | contribs) at 23:26, 11 June 2009 (Preliminary draft). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
function watchlistUpdate() {}

watchlistUpdate.init = function() {
    var tables = document.getElementById("bodyContent").getElementsByTagName("table");
    var pages = new Array();
    for(i=0; i<tables.length; i++) {
        var table = tables[i];
        href = table.getElementsByTagName("td")[1].getElementsByTagName("a")[0].href;
        alert(table.parentNode.id);
        if(href.indexOf("Special:") == -1 && table.parentNode.id.indexOf("mw-rc-subentries") == -1) {
            pages[pages.length] = href.split("/wiki/")[1].replace(/_/g, " "); //Populates the "pages" array with the links (exludes special pages)
        }
    }
    this.getRevisionIDs(pages);
}

watchlistUpdate.getRevisionIDs = function(pages) {
    var revids = new Array();
    var req = sajax_init_object();
    req.open("GET", wgScriptPath + "/api.php?action=query&format=json&prop=revisions&titles=" + encodeURIComponent(pages.join("|").toString()), false);
    req.send(null);
    var info_ = eval("(" + req.responseText + ")").query.pages;
    for (var index in info_) {
        var info = info_[index];
        revids[pages.indexOf(info.title)] = info.revisions[0].revid;
    }
    window.setTimeout(function() { watchlistUpdate.doUpdate(pages, revids); }, 5000);
}

watchlistUpdate.doUpdate = function(pages, revids) {
    jsMsg("Any updates on your watchlisted pages will appear below:\n<ul></ul>", "watchlistUpdateContainer");
    var req = sajax_init_object();
    req.open("GET", wgScriptPath + "/api.php?action=query&format=json&prop=revisions&titles=" + encodeURIComponent(pages.join("|").toString()), false);
    var info_ = eval("(" + req.responseText + ")").query.pages;
    for (var index in info_) {
        var info = info_[index];
        if(revids[pages.indexOf(info.title)] != info.revisions[0].revid) {
            document.getElementById("mw-js-message").getElementsByTagName("ul")[0].innerHTML += "<li> Update on <a href=\"/wiki/" + encodeURIComponent(info.revisions[0].title) + "\">" + info.revisions[0].title + "</a> (<a href=\"" + wgScript + "?title=" + info.revisions[0].title + "&diff=" + info.revisions[0].revid + "\">diff</a>)</li>";
        }
    }
}