User:Yair rand/watchlistnotifications.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:Yair rand/watchlistnotifications. |
// Notifies the user when something new shows up on their watchlist, excluding one's own edits.
// Desktop notifications only work for Chrome at the moment.
// To enable, add importScript("User:Yair rand/watchlistnotifications.js"); to Special:MyPage/common.js,
// go to your watchlist, and click the "Enable watchlist desktop notifications" button in the top-right corner.
// This is a modified version of the earlier [[wikt:User:Yair rand/watchlistnotifications.js]].
function watchlistNotifier(){
var t, nc = window.webkitNotifications || window.mozNotifications || window.oNotifications || window.msNotifications || window.notifications || null;
function checkWatchlistForUpdates(){
$.get(mw.config.get('wgServer')+mw.config.get('wgScriptPath')+"/api.php?format=json&action=query&list=watchlist&wlexcludeuser="+mw.config.get('wgUserName')+"&wlprop=user|title|timestamp&wlend="+(t?t:1),function(r){
r = r.query.watchlist;
for(var i = 0; t && t < r[i].timestamp; i++){
var w=nc.createNotification('',r[i].title,'has been edited by ' + r[i].user)
w.show()
w.onclick = function(){w.cancel();window.focus();}
document.title = r[i].title+' has been edited by ' + r[i].user
onfocus = function(){location.reload(true)}
}
t = r[0].timestamp;
setTimeout(checkWatchlistForUpdates, 15000)
})
}
nc && nc.checkPermission() == 0 && checkWatchlistForUpdates()
if(nc && nc.checkPermission() == 1){
var a = document.getElementById('firstHeading').appendChild(document.createElement('a'));
a.appendChild(document.createTextNode("Enable watchlist desktop notifications"));
a.style.float = 'right'; a.style.cursor = 'pointer'; a.style.fontSize = '13px';
a.onclick = function(){nc.requestPermission(function(){location.reload()}); this.innerHTML = 'Waiting for permission to use desktop notifications...'}
}
}
mw.config.get('wgPageName') == "Special:Watchlist" && jQuery(document).ready(watchlistNotifier)