User:Animum/watchlistUpdate.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Animum/watchlistUpdate. |
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[pages.length] = title; //Populates the "pages" array with the links (exludes special pages)
}
} else {
pages[pages.length] = title;
}
} else { //User has hide_pages_in_watchlist.js installed but does not have a list of pages to ignore.
pages[pages.length] = title;
}
} else { //User doesn't use hide_pages_in_watchlist.js.
pages[pages.length] = 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, "_")) + "\">→</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];
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 + "</tt> "
+ "Update on <a href=\"/wiki/" + encoded.title + "\">" + title + "</a> "
+ "(<a href=\"" + wgScript + "?title=" + encoded.title + "&diff=" + revision.revid + "\">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");
}
});