1

I wrote a Firefox add-on that works fine as an overlay, but now I'm converting it to be boostrapped (restartless). It registers a tab listener, and then opens up an HTML5 notification under certain circumstances when a tab is closed.

The add-on debugger tells me that the Notification class is undefined:

ReferenceError: Notification is not defined

According to the Mozilla documentation, no special JSMs need to be included to use Notifications. Any idea what the problem is, and more importantly, how to fix it?

v0max
  • 13
  • 2

2 Answers2

1

According to the Mozilla documentation, no special JSMs need to be included to use Notifications.

That only applies to javascript contexts where the global object is a DOM window. Bootstrapped addons run in a sandbox which only have ecmascript-defined objects (Object, Promise, ...) , Components and a few others defined.

Check with the debugger to see what exactly is available in that scope.

So you either need to retrieve a window object (xul windows should work too) if you want to use the HTML5 API or import another service with similar functionality, e.g. alertservice

the8472
  • 40,999
  • 5
  • 70
  • 122
0

As the8472 has stated, bootstrapped add-ons do not automatically have access to a global window object. This is quite a bit different from the context under which most JavaScript is run. It is something that trips up a considerable number of people. It should also be noted that the code in bootstrap.js may be running at a time when no window exists, and thus you can not obtain one.

If a browser window exists, you can obtain a reference to the most recent browser window, document, and gBrowser with:

if (window === null || typeof window !== "object") {
    //If you do not already have a window reference, you need to obtain one:
    //  Add a "/" to un-comment the code appropriate for your add-on type.
    /* Add-on SDK:
    var window = require('sdk/window/utils').getMostRecentBrowserWindow();
    //*/
    //* Overlay and bootstrap (from almost any context/scope):
    var window=Components.classes["@mozilla.org/appshell/window-mediator;1"]
                         .getService(Components.interfaces.nsIWindowMediator)
                         .getMostRecentWindow("navigator:browser");        
    //*/
}
if (typeof document === "undefined") {
    //If there is no document defined, get it
    var document = window.content.document;
}
if (typeof gBrowser === "undefined") {
    //If there is no gBrowser defined, get it
    var gBrowser = window.gBrowser;
}
Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • Thanks, I figured it out: I had to go from calling Notification, to window.Notification – v0max Mar 20 '15 at 21:23