I have a userscript that pops up a notification if certain content exists on the target page.
Under Tampermonkey/Chrome, this is not a problem. I can use the GM_Notification()
function to create notifications with ease.
When I try to do this under Firefox, it does not have the same behaviour whatsoever.
Checking in the logs there are no errors regarding the function, nor are their any notifications popping up.
Here is some sample code which does not work in Firefox+Greasemonkey or Firefox+Tampermonkey, but does work in Chrome+Tampermonkey:
// ==UserScript==
// @name Test Notifier
// @include *
// @grant GM_notification
// @grant window.focus
// ==/UserScript==
console.log('I am a pretty test script');
var notificationDetails = {
text: 'THIS IS A TEST NOTIFICATION!!!',
title: 'TEST',
timeout: 15000,
onclick: function() { window.focus(); },
};
GM_notification(notificationDetails);
Is this standard behaviour for Firefox? Does it handle HTML5 notifications in a completely different way (if at all)? and what's the common practice for enabling notifications in a Firefox userscript?
GM_notification()
is not (yet) supported in Greasemonkey (Firefox). And if you had checked the error console, you would have seen this error:
GM_notification is not defined
There is an old feature request to add GM_notification()
to Greasemonkey; you can go there and urge the lead GM developer to try and catch up to Tampermonkey. :)
Until such time as that feature is added, you can "shim" support for GM_notification by using the HTML5(ish) Notifications API, which is supported on many modern browsers.
Your test script, with the shim added is as follows. Tested on both Firefox and Chrome but should work on Safari and Opera too:
// ==UserScript==
// @name _Cross browser notifications
// @match http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant GM_notification
// @grant window.focus
// ==/UserScript==
console.log ('Test script start.');
shim_GM_notification ()
var notificationDetails = {
text: 'Test notification body.',
title: 'Test notice title',
timeout: 6000,
onclick: function () {
console.log ("Notice clicked.");
window.focus ();
}
};
GM_notification (notificationDetails);
/*--- Cross-browser Shim code follows:
*/
function shim_GM_notification () {
if (typeof GM_notification === "function") {
return;
}
window.GM_notification = function (ntcOptions) {
checkPermission ();
function checkPermission () {
if (Notification.permission === "granted") {
fireNotice ();
}
else if (Notification.permission === "denied") {
alert ("User has denied notifications for this page/site!");
return;
}
else {
Notification.requestPermission ( function (permission) {
console.log ("New permission: ", permission);
checkPermission ();
} );
}
}
function fireNotice () {
if ( ! ntcOptions.title) {
console.log ("Title is required for notification");
return;
}
if (ntcOptions.text && ! ntcOptions.body) {
ntcOptions.body = ntcOptions.text;
}
var ntfctn = new Notification (ntcOptions.title, ntcOptions);
if (ntcOptions.onclick) {
ntfctn.onclick = ntcOptions.onclick;
}
if (ntcOptions.timeout) {
setTimeout ( function() {
ntfctn.close ();
}, ntcOptions.timeout);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With