javascript - Userscript notifications work on Chrome but not Firefox? -
i have userscript pops notification if content exists on target page.
under tampermonkey/chrome, not problem. can use gm_notification()
function create notifications ease.
when try under firefox, not have same behaviour whatsoever.
checking in logs there no errors regarding function, nor notifications popping up.
here sample code not work in firefox+greasemonkey or firefox+tampermonkey, work in chrome+tampermonkey:
// ==userscript== // @name test notifier // @include * // @grant gm_notification // @grant window.focus // ==/userscript== console.log('i pretty test script'); var notificationdetails = { text: 'this test notification!!!', title: 'test', timeout: 15000, onclick: function() { window.focus(); }, }; gm_notification(notificationdetails);
is standard behaviour firefox? handle html5 notifications in different way (if @ all)? , what's common practice enabling notifications in firefox userscript?
gm_notification()
not (yet) supported in greasemonkey (firefox). , if had checked error console, have seen error:
gm_notification not defined
there an old feature request add gm_notification()
greasemonkey; can go there , urge lead gm developer try , catch tampermonkey. :)
until such time feature added, can "shim" support gm_notification using the html5(ish) notifications api, supported on many modern browsers.
your test script, shim added follows. tested on both firefox , chrome should work on safari , 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 page/site!"); return; } else { notification.requestpermission ( function (permission) { console.log ("new permission: ", permission); checkpermission (); } ); } } function firenotice () { if ( ! ntcoptions.title) { console.log ("title required 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); } } } }
Comments
Post a Comment