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:17, 29 June 2009 (...). 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.
/*
* Lists updates to watchlisted pages in a box at the top of [[Special:Watchlist]] after clicking on the "auto-update" tab
*
* Checks, by default, every 5 seconds
*
* This can be custom-set by adding this line to your JavaScript file ([[Special:MyPage/[skin-name].js]]) after the importScript 
* declaration for this script (importScript("User:Animum/watchlistUpdate.js");) (e.g., setting timeout to 10 seconds): 
    var updateSeconds = 10;
*
*/

if(typeof(updateSeconds) == "undefined") var updateSeconds = 5;
var isSysop = /sysop/.test(wgUserGroups);
var isRollbacker = /rollbacker/.test(wgUserGroups);

function watchlistUpdate() {}

watchlistUpdate.clearContainer = function() {
    document.getElementById("mw-js-message").getElementsByTagName("ul")[0].innerHTML = "";
}

watchlistUpdate.isIP = function(ip) { //From [[MediaWiki:Sysop.js]]
    return /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/.test(ip);
}

watchlistUpdate.init = function() {
    if(getElementsByClassName(document, "div", "mw-js-message-watchlistUpdateContainer")[0]) return; //User has already initiated the script once; abort.
    jsMsg("Any updates to your watchlisted pages will appear below (<a href=\"javascript:watchlistUpdate.clearContainer()\">clear list</a>):\n<ul></ul>", "watchlistUpdateContainer");
    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;
        if(href.indexOf("Special:") == -1 && table.parentNode.id.indexOf("mw-rc-subentries") == -1) {
            var title = decodeURIComponent(href.split("/wiki/")[1].replace(/_/g, " "));
            if(typeof WatchlistConfig != "undefined") { 
                if(typeof WatchlistConfig.ignorePages != "undefined") {
                    if(WatchlistConfig.ignorePages.length > 0) {
                        if(WatchlistConfig.ignorePages.indexOf(title) == -1) { //Remove pages that [[User:Gary King/hide pages in watchlist.js]] ignores
                            pages.push(title); //Populates the "pages" array with the links (exludes special pages)
                        }
                    } else {
                        pages.push(title);
                    }
                } else { //User has hide_pages_in_watchlist.js installed but does not have a list of pages to ignore.
                    pages.push(title);
                }
            } else { //User doesn't use hide_pages_in_watchlist.js.
                pages.push(title);
            }
        }
    }
    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=" + 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.setInterval(function() { watchlistUpdate.doUpdate(pages, revids); }, updateSeconds*1000);
    delete req;
}

watchlistUpdate.formatSummary = function(comment, title) {
    if(comment.search(/\/\*.*\*\//) != -1) { //Section name
        var section = comment.split(/\/\* ?/)[1].split(/ ?\*\//)[0];
        comment = comment.replace(/\/\*.*\*\//g, "<span class=\"autocomment\"><a href=\"" + wgScript + "?title=" + encodeURIComponent(title.replace(/ /g, "_")) + "#" + encodeURIComponent(section.replace(/ /g, "_")) + "\">&rarr;</a>" + section + (comment.split(/ ?\*\//)[1].length > 0 ? ":" : "") + "</span>");
    }
    comment = comment.replace(/\[\[([^\|\]]+)\|?(.*?)\]\]/g, function (ignore, link, display) { return "<a href=\"" + wgScript + "?title=" + encodeURIComponent(link.replace(/ /g, "_")) + "\">" + (display || link) + "</a>"}); //Stolen from amelvand.js (yes, I'm lazy)
    return comment;
}

watchlistUpdate.doUpdate = function(pages, revids) {
    var req = sajax_init_object();
    req.open("GET", wgScriptPath + "/api.php?action=query&format=json&prop=revisions&rvtoken=rollback&titles=" + pages.join("|").toString(), false);
    req.send(null);
    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) {
            var title = info.title;
            var revision = info.revisions[0];
            var user = revision.user;
            var timestamp = revision.timestamp.split("T")[1].split("Z")[0].split(":");
            var summary = revision.comment;
            var encoded = {
                "title": encodeURIComponent(title.replace(/ /g, "_")),
                "user" : encodeURIComponent(user.replace(/ /g, "_")),
                "token": encodeURIComponent(revision.rollbacktoken)
            };
            document.getElementById("mw-js-message").getElementsByTagName("ul")[0].innerHTML += "<li> <tt>" + timestamp[0] + ":" + timestamp[1]
                + (typeof(revisions.minor) != "undefined" ? " <b>m</b>" : "") + "</tt> "
                + "<a href=\"/wiki/" + encoded.title + "\">" + title + "</a> "
                + "(<a href=\"" + wgScript + "?title=" + encoded.title + "&curid=" + info.pageid + "&diff=" + revision.revid + "&oldid=" + revision.parentid + "\">diff</a>)"
                + " by <a href=\"/wiki/" + (this.isIP(user) ? "Special:Contributions/" : "User:") + encoded.user + "\">" + user + "</a>"
                + " (<a href=\"/wiki/User_talk:" + encoded.user + "\">talk</a>" + (this.isIP(user) ? (isSysop ? " | <a href=\"/wiki/Special:Block" + encoded.user + "\">block</a>)" : ")") : " | <a href=\"/wiki/Special:Contributions/" + encoded.user + "\">contribs</a>" + (isSysop ? " | <a href=\"/wiki/Special:Block/" + encoded.user + "\">block</a>)" : ")"))
                + (summary ? " <i>(" + this.formatSummary(summary, title) + ")</i>" : "")
                + (isSysop || isRollbacker ? " <span class=\"mw-rollback-link\">[<a href=\"" + wgScript + "?title=" + encoded.title + "&action=rollback&from=" + encoded.user + "&token=" + encoded.token + "\">rollback</a>]</span>" : "") + "</li>";
            revids[pages.indexOf(info.title)] = revision.revid;
        }
    }
    delete req;
}

addOnloadHook(function() {
    if(wgCanonicalSpecialPageName == "Watchlist" && document.title == "My watchlist - Wikipedia, the free encyclopedia") { //document.title included to catch things such as editing the raw watchlist
        addPortletLink("p-cactions", "javascript:watchlistUpdate.init()", "auto-update", "ca-watchlistupdate", "Automatically reports changes to pages in your watchlist");
    }
});