2016-04-21 1 views
3

J'ai un script d'utilisateur qui affiche une notification si un certain contenu existe sur la page cible. Avec Tampermonkey/Chrome, ce n'est pas un problème. Je peux utiliser la fonction GM_Notification() pour créer des notifications avec facilité. Lorsque j'essaie de faire cela sous Firefox, il n'a pas le même comportement que ce soit.
En vérifiant dans les journaux il n'y a aucune erreur concernant la fonction, et leurs notifications n'apparaissent pas non plus.Les notifications UserScript fonctionnent sur Chrome mais pas sur Firefox?

Voici quelques exemples de code qui ne fonctionne pas dans Firefox + Greasemonkey ou Firefox + Tampermonkey, mais fonctionne dans 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); 

Est-ce comportement standard pour Firefox? Gère-t-il les notifications HTML5 d'une manière complètement différente (voire pas du tout)? et quelle est la pratique courante pour activer les notifications dans un script d'utilisateur Firefox?

+0

Firefox 46 est maintenant stable et GM_Notification ne fonctionne toujours pas dans Tampermonkey à ce moment. Cependant le code que vous avez fourni ci-dessous fonctionne parfaitement, donc je vais accepter que la bonne réponse :) – Saintwolf

Répondre

9

GM_notification() is not (yet) supported in Greasemonkey (Firefox). Et si vous aviez checked the error console, vous auriez vu cette erreur:

GM_notification is not defined

Il est an old feature request to add GM_notification() à Greasemonkey; vous pouvez y aller et demander au développeur principal de GM d'essayer de rattraper Tampermonkey. :)

Jusqu'à ce que cette fonctionnalité soit ajoutée, vous pouvez "shim" le support pour GM_notification en utilisant the HTML5(ish) Notifications API, qui est pris en charge sur de nombreux navigateurs modernes.

Votre script de test, avec le shim ajouté est le suivant. Testé sur Firefox et Chrome, mais devrait travailler sur Safari et Opera aussi:

// ==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); 
      } 
     } 
    } 
}