Search code examples
javascripthtmlfirefox-addonfirefox-addon-restartless

Converting to bootstrapped: Notification undefined


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?


Solution

  • 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