2016-04-21 12 views
3

Ich habe ein Benutzer-Skript, das eine Benachrichtigung anzeigt, wenn bestimmte Inhalte auf der Zielseite vorhanden sind.Userscript-Benachrichtigungen funktionieren in Chrome, aber nicht in Firefox?

Unter Tampermonkey/Chrome ist dies kein Problem. Ich kann die GM_Notification() Funktion verwenden, um Benachrichtigungen mit Leichtigkeit zu erstellen.

Wenn ich versuche, dies unter Firefox zu tun, hat es nicht das gleiche Verhalten.
Beim Einchecken der Protokolle gibt es keine Fehler in Bezug auf die Funktion, noch werden irgendwelche Benachrichtigungen angezeigt.

Hier ist ein Beispielcode, die in Firefox + Grease oder Firefox + Tampermonkey, aber funktioniert in Chrome + Tampermonkey nicht funktioniert:

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

dieses Standardverhalten für Firefox ist? Behandelt es HTML5-Benachrichtigungen auf völlig andere Weise (wenn überhaupt)? und wie ist es üblich, Benachrichtigungen in einem Firefox-Benutzerkonto zu aktivieren?

+0

46 Firefox ist jetzt stabil und GM_Notification noch nicht zu diesem Zeitpunkt innerhalb Tampermonkey funktioniert. Allerdings funktioniert der Code, den Sie unten angegeben haben, perfekt und ich werde das als die richtige Antwort akzeptieren :) – Saintwolf

Antwort

9

GM_notification() is not (yet) supported in Greasemonkey (Firefox). Und wenn Sie checked the error console hätten, würden Sie diesen Fehler haben gesehen:

GM_notification is not defined

Es gibt an old feature request to add GM_notification() zu Grease; Sie können dorthin gehen und den leitenden GM-Entwickler auffordern, Tampermonkey zu erreichen. :)

Solange diese Funktion hinzugefügt wird, können Sie „Shim“ Unterstützung für GM_notification von the HTML5(ish) Notifications API verwenden, die auf vielen modernen Browsern unterstützt wird.

Ihr Testskript mit dem Shim ist wie folgt. Getestet auf Firefox und Chrome aber sollte Arbeit auf Safari und Opera auch:

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