Jump to content

User:Awesome Aasim/rcnotify.js

From Wikipedia, the free encyclopedia
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.
if (mw.config.get("wgPageName").toLowerCase() == "Special:RecentChanges".toLowerCase()) {
	//sends notifications on every new bad edit
	var RCNotify = {};
	$.get(mw.config.get("wgScriptPath") + "/api.php", {
		"action": "query",
		"format": "json",
		"list": "recentchanges",
		"rcnamespace": "0|3",
		"rcprop": "title|timestamp|flags|loginfo|oresscores|parsedcomment|user|ids|tags",
		"rcshow": "oresreview",
		"rctoponly": true,
		"rclimit": "1",
		"rctype": "edit|new"
	}).done(function(result) {
		RCNotify.oldedit = result.query.recentchanges[0].revid;
		if (Notification.permission !== 'granted') {
			Notification.requestPermission();
			if (Notification.permission != 'granted') {
				mw.notify("RC Notification alert: Please enable notifications on your browser to get notified every time an edit needing review is made.");
			}
		}
		RCNotify.fetch();
	});
	RCNotify.notifications = [];
	RCNotify.revids = [];
	RCNotify.fetch = function() {
		$.get(mw.config.get("wgScriptPath") + "/api.php", {
			"action": "query",
			"format": "json",
			"list": "recentchanges",
			"rcnamespace": "0|3",
			"rcprop": "title|timestamp|flags|loginfo|oresscores|parsedcomment|user|ids|tags",
			"rcshow": "oresreview",
			"rctoponly": true,
			"rclimit": "1",
			"rctype": "edit|new"
		}).done(function(result) {
			if (RCNotify.oldedit < result.query.recentchanges[0].revid) {
				RCNotify.oldedit = result.query.recentchanges[0].revid;
				RCNotify.revids.push(result.query.recentchanges[0].revid);
				RCNotify.notifications.push(new Notification("New recent change to " + mw.config.get("wgSiteName") + " needs review", {
					body: result.query.recentchanges[0].user + " made a potentially problematic edit to \"" + result.query.recentchanges[0].title + "\". Click to review."
				}));
				RCNotify.notifications[RCNotify.notifications.length-1].onclick = function() {
					console.log(this);
					var revindex = RCNotify.notifications.indexOf(this);
					window.open(location.origin + "/wiki/Special:Diff/" + RCNotify.revids[revindex]);
					this.close(); // focus our tab and close notif
				};
			}
			window.setTimeout(RCNotify.fetch, 1000);
		});
	};
}