glorfindel-SE-Userscripts/admin-flag-notifier/admin-flag-notifier.user.js

68 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-08-08 21:35:12 +00:00
// ==UserScript==
// @name Admin Flag Notifier
// @namespace https://github.com/Glorfindel83/
// @description Refreshes the flag dashboard automatically and sends desktop notifications when there are new flags.
// @author Glorfindel
2017-08-11 17:55:50 +00:00
// @updateURL https://raw.githubusercontent.com/Glorfindel83/SE-Userscripts/master/admin-flag-notifier/admin-flag-notifier.user.js
// @downloadURL https://raw.githubusercontent.com/Glorfindel83/SE-Userscripts/master/admin-flag-notifier/admin-flag-notifier.user.js
// @version 0.2
2017-08-08 21:35:12 +00:00
// @match *://*.stackexchange.com/admin/dashboard*
// @match *://*.stackoverflow.com/admin/dashboard*
// @match *://*.superuser.com/admin/dashboard*
// @match *://*.serverfault.com/admin/dashboard*
// @match *://*.askubuntu.com/admin/dashboard*
// @match *://*.stackapps.com/admin/dashboard*
// @match *://*.mathoverflow.net/admin/dashboard*
// @grant none
// ==/UserScript==
(function () {
2017-12-18 21:52:22 +00:00
"use strict";
if (Notification.permission !== "granted") {
2017-08-08 21:35:12 +00:00
Notification.requestPermission();
}
2017-12-18 21:52:22 +00:00
2017-08-08 21:35:12 +00:00
// Determine site name
var host = window.location.host;
2017-12-18 21:52:22 +00:00
var sitename = host.split(".")[0];
2017-08-08 21:35:12 +00:00
// Determine current amount of flags
2017-12-18 21:52:22 +00:00
var currentTitle = $("title").text();
2017-08-08 21:35:12 +00:00
var currentFlags = parseInt(currentTitle);
console.log("Current: " + currentFlags);
2017-12-18 21:52:22 +00:00
2017-08-08 21:35:12 +00:00
setInterval(function () {
2018-04-20 08:56:26 +00:00
var url = `https://{host}/admin/dashboard?filtered={!document.getElementById("chk-apply-filters").checked}`;
2017-08-08 21:35:12 +00:00
console.log("Calling: " + url);
$.get(url, function (data) {
2017-12-18 21:52:22 +00:00
var updatedTitle = $("<html/>").html(data).find("title").text();
2017-08-08 21:35:12 +00:00
var updatedFlags = parseInt(updatedTitle);
console.log("Update: " + updatedFlags);
2017-12-18 21:52:22 +00:00
2017-08-08 21:35:12 +00:00
// TODO: check on flag IDs instead
if (updatedFlags < currentFlags) {
console.log("Less flags.");
// presumably, some flags have been handled and the page needs to be reloaded
window.location.reload();
} else if (updatedFlags > currentFlags) {
console.log("More flags.");
// new flags, create a notification. Remember the current number, so that we don't send a notification twice for the same flag
2017-12-18 21:52:22 +00:00
currentFlags = updatedFlags;
if (Notification.permission === "granted") {
var notification = new Notification("New flags (total: " + updatedFlags + ")", {
icon: "https://cdn.sstatic.net/Sites/" + sitename + "/img/apple-touch-icon.png",
2017-08-08 21:35:12 +00:00
requireInteraction: true // on macOS, this only has effect when you set the notification types of your browser to 'Alert' instead of 'Banner'.
});
console.log("Notification created.");
2017-12-18 21:52:22 +00:00
notification.onclick = function () {
2017-08-08 21:35:12 +00:00
console.log("Notification clicked.");
window.focus();
// reload only here, otherwise the notification will be dismissed
window.location.reload();
};
}
}
});
}, 60000);
2017-12-18 21:52:22 +00:00
})();