User:Animum/watchlistUpdate.js

/** Lists updates to watchlisted pages in a box at the top of [[Special:Watchlist]] after clicking on the "auto-update" tab** Only works on watchlists with fewer than 500 pages** If you add a new page to your watchlist after the script has been initiated, you will have to reload your watchlist and * re-initiate the script for it to detect the new page.** 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):    importScript("User:Animum/watchlistUpdate.js");    var updateSeconds = 10;** To disable alerts upon an update to a page, add this line to your monobook.js (or other skin.js) file:    var alertOnUpdate = false;*/if(typeof(updateSeconds) == "undefined") var updateSeconds = 5;if(typeof(alertOnUpdate) == "undefined") var alertOnUpdate = true;var isSysop = /sysop/.test(mw.config.get('wgUserGroups'));var isRollbacker = /rollbacker/.test(mw.config.get('wgUserGroups'));function watchlistUpdate() {}watchlistUpdate.clearContainer = function() {    getElementsByClassName(document, "div", "mw-js-message-watchlistUpdateContainer")[0].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 req = sajax_init_object();    req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&format=json&list=watchlist&wllimit=500&wlprop=ids|title", false);    req.send(null);    var pagelist = eval("(" + req.responseText + ")").query.watchlist;    var pages = new Array();    var revids = new Array();    for(i=0; i<pagelist.length; i++) {        var page = pagelist[i];        var addData = function() {            pages.push(page.title);            revids.push(page.revid);        }        if(typeof WatchlistConfig != "undefined") {             if(typeof WatchlistConfig.ignorePages != "undefined") {                if(WatchlistConfig.ignorePages.length > 0) {                    if(WatchlistConfig.ignorePages.indexOf(page.title) == -1) { //Remove pages that [[User:Gary King/hide pages in watchlist.js]] ignores                        addData(); //Populates the "pages" array with the links (exludes special pages)                    }                } else {                    addData();                }            } else { //User has hide_pages_in_watchlist.js installed but does not have a list of pages to ignore.                addData();            }        } else { //User doesn't use hide_pages_in_watchlist.js.            addData();        }    }    window.setInterval(function() { watchlistUpdate.doUpdate(pages, revids); }, updateSeconds*1000);    delete req;}watchlistUpdate.formatSummary = function(comment, title) {    comment = comment.replace("<", "&lt;").replace(">", "&gt;"); //Preliminary replacement to prevent formatting errors    if(comment.search(/\/\*.*\*\//) != -1) { //Section name        var section = comment.split(/\/\* ?/)[1].split(/ ?\*\//)[0];        comment = comment.replace(/\/\*.*\*\//g, "<span class=\"autocomment\"><a href=\"" + mw.config.get('wgScript') + "?title=" + encodeURIComponent(title.replace(/ /g, "_")) + "#" + encodeURIComponent(section.replace(/ /g, "_")).replace(/\%/g, ".") + "\" target=\"_blank\">&rarr;</a>" + section + (comment.split(/ ?\*\//)[1].length > 0 ? ":" : "") + "</span>");    }    comment = comment.replace(/\[\[([^\|\]]+)\|?(.*?)\]\]/g, function (ignore, link, display) { return "<a href=\"" + mw.config.get('wgScript') + "?title=" + encodeURIComponent(link.replace(/ /g, "_")) + "\" target=\"_blank\">" + (display || link) + "</a>"}); //Stolen from amelvand.js (yes, I'm lazy)    return comment;}watchlistUpdate.doUpdate = function(pages, revids) {    var container = getElementsByClassName(document, "div", "mw-js-message-watchlistUpdateContainer")[0];    if(!container) { //If, for some reason, another jsMsg has overtaken ours...        jsMsg("Any updates to your watchlisted pages will appear below (<a href=\"javascript:watchlistUpdate.clearContainer()\">clear list</a>):\n<ul></ul>", "watchlistUpdateContainer");        container = getElementsByClassName(document, "div", "mw-js-message-watchlistUpdateContainer")[0]; //Error-catcher    }    var req = sajax_init_object();    req.open("GET", mw.config.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];        var revision = info.revisions[0];        if(revids[pages.indexOf(info.title)] != revision.revid) {            var title = info.title;            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)            };            container.getElementsByTagName("ul")[0].innerHTML += "<li> <tt>" + timestamp[0] + ":" + timestamp[1]                + (typeof(revision.minor) == "string" ? " <b>m" + (typeof(revision.bot) == "string" ? "b</b>" : "</b>") : "") + " </tt>"                + "<a href=\"/wiki/" + encoded.title + "\" target=\"_blank\">" + title + "</a> "                + "(<a href=\"" + mw.config.get('wgScript') + "?title=" + encoded.title + "&curid=" + info.pageid + "&diff=" + revision.revid + "&oldid=" + revision.parentid + "\" target=\"_blank\">diff</a>)"                + " by <a href=\"/wiki/" + (this.isIP(user) ? "Special:Contributions/" : "User:") + encoded.user + "\" target=\"_blank\">" + user + "</a>"                + " (<a href=\"/wiki/User_talk:" + encoded.user + "\" target=\"_blank\">talk</a>" + (this.isIP(user) ? (isSysop ? " | <a href=\"/wiki/Special:Block" + encoded.user + "\" target=\"_blank\">block</a>)" : ")") : " | <a href=\"/wiki/Special:Contributions/" + encoded.user + "\" target=\"_blank\">contribs</a>" + (isSysop ? " | <a href=\"/wiki/Special:Block/" + encoded.user + "\" target=\"_blank\">block</a>)" : ")"))                + (summary ? " <span class=\"comment\">(" + this.formatSummary(summary, title) + ")</span>" : "")                + (isSysop || isRollbacker ? " <span class=\"mw-rollback-link\">[<a href=\"" + mw.config.get('wgScript') + "?title=" + encoded.title + "&action=rollback&from=" + encoded.user + "&token=" + encoded.token + "\" target=\"_blank\">rollback</a>]</span>" : "") + "</li>";            revids[pages.indexOf(info.title)] = revision.revid;            if(alertOnUpdate) alert("Updates encountered");        }    }    delete req;}$(function() {    if(mw.config.get('wgCanonicalSpecialPageName') == "Watchlist" && document.title == "My watchlist - Wikipedia, the free encyclopedia") { //document.title is included to catch things such as editing the raw watchlist.        mw.util.addPortletLink("p-cactions", "javascript:watchlistUpdate.init()", "auto-update", "ca-watchlistupdate", "Automatically reports changes to pages in your watchlist");    }});