Reputation: 124646
The purpose of this userscript is, when you visit the /review
page of a Stack Exchange website, it adds a Desktop Notifications link near the top, and if you visit that page, it generates a desktop notification if there are moderator flags or posts to review. The script reloads itself periodically.
This works out of the box in any Chrome/Tampermonkey I tried in multiple OS-es (OSX, Linux, Windows). In Firefox/Greasemonkey it doesn't work in OSX (the Desktop Notifications doesn't appear). If I recall correctly, it does work in Linux. The Web Developer Console doesn't show anything when I visit the page. The userscript is of course installed and enabled.
What could I be missing? How to debug this?
// ==UserScript==
// @name Moderator Flag Notification
// @author Simon Forsberg
// @description Shows a desktop notification when there are flags or review items to be handled.
// @namespace https://github.com/Zomis/SE-Scripts
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_notification
// @match *://*.stackexchange.com/review*
// @match *://*.stackoverflow.com/review*
// @match *://*.superuser.com/review*
// @match *://*.serverfault.com/review*
// @match *://*.askubuntu.com/review*
// @match *://*.stackapps.com/review*
// @match *://*.mathoverflow.net/review*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==
if (window.location.href.indexOf('/desktop-notifications') === -1) {
$('.tools-rev h1').append('<span class="lsep">|</span><a href="/review/desktop-notifications">Desktop Notifications</a></h1>');
} else {
var KEY_NEXT = 'NextReload';
var DELAY = 60 * 1000;
var currentTime = Date.now ? Date.now() : new Date().getTime();
var lastTime = GM_getValue(KEY_NEXT, 0);
var nextTime = currentTime + DELAY;
GM_setValue(KEY_NEXT, nextTime);
var timeDiff = Math.abs(lastTime - currentTime);
setTimeout(function(){ window.location.reload(); }, DELAY);
$('.subheader h1').html('Desktop Notifications');
$('.leftcol').html('Keep your browser open on this page and it will be automatically reloaded and alert you when something wants your attention.').removeClass('leftcol');
$('.rightcol').remove();
var title = document.title.split(' - '); // keep the site name
document.title = 'Desktop Notifications - ' + title[1];
// a way to detect that the script is being executed because of an automatic script reload, not by the user.
if (timeDiff <= DELAY * 2) {
var notifications = [];
var topbarFlag = $('.topbar-links .mod-only .icon-flag .flag-count').html();
var topbarFlagCount = parseInt(topbarFlag);
if (topbarFlagCount > 0) {
notifications.push(topbarFlagCount + ' Flags');
}
var reviewItems = $('.icon-tools-flag span');
var reviewCount = 0;
if (reviewItems.length > 0) {
reviewCount = parseInt(reviewItems.html());
if (reviewCount > 0) {
notifications.push(reviewCount + ' Review Items');
}
}
if (notifications.length > 0) {
var details = {
title: document.title,
text: notifications.join('\n'),
timeout: 0
};
GM_notification(details, null);
}
}
}
Upvotes: 0
Views: 1416
Reputation: 40811
According to GreaseMonkey's source, the GM_notification
API is not implemented yet.
Issue #1194 is the relavent issue. (I don't think that it will be added anytime soon)
As a temporary polyfill, you can have at the start of the file:
if (!GM_notification) {
GM_notification = function(details) {
alert(details.title + '\n' + details.text);
}
}
This makes the tab look like:
But won't produce a notification. You should also have information in the document.title
now, as there won't be any actual notifications (Like the number of notifications in square brackets).
There isn't much else you could do that won't just be an inconvenience when users were doing other things, like window.open
to steal focus (But most attempts will probably be blocked) to a newly opened tab.
A long term solution to this is to make this into an extension, as both Firefox and Chrome extensions support notifications.
Upvotes: 1