1

From document.createEvent

The createEvent method is deprecated. Use event constructors instead.

In bootstap.js

// this works
let event = window.document.createEvent('Event');
event.initEvent('main-unload',false,false);
window.dispatchEvent(event);

// this doesn't work
let event = new CustomEvent('main-unload', {"detail":{"hazcheeseburger":true}});
window.dispatchEvent(event);
//Console error: 1404023846296  addons.xpi  WARN    Exception running bootstrap method shutdown on ***addon-id****

What am I missing?

erosman
  • 7,094
  • 7
  • 27
  • 46
  • 1
    Custom events from addon scope is tricky initially, there's this 4th argument on `addEventListener`. Check out this topic: [How to listen to custom events on all windows, bubbling issue?](http://stackoverflow.com/questions/22121652/how-to-listen-to-custom-events-on-all-windows-bubbling-issue) – Noitidart Jun 29 '14 at 07:23
  • 1
    @Noitidart Correct, but that only applies when a privileged context (`bootstrap.js`, overlay script, etc.) wants to listen to events dispatched by an unprivileged website, so it doesn't really apply here. – nmaier Jun 29 '14 at 07:31
  • 1
    It might be a good idea to enclose `startup`/`shutdown` in try-catch blocks that `Cu.reportError` any exceptions. Makes it easier to figure out *what* code is broken. The regualar `addons.xpi WARN` message is not really helpful in that regard. – nmaier Jun 29 '14 at 07:36

1 Answers1

1

You're missing the CustomEvent constructor. The bootstrap.js code does not have one of those, only windows do.

The following should work:

let event = new (window.CustomEvent)('main-unload',
                                     {"detail":{"hazcheeseburger":true}});
nmaier
  • 32,336
  • 5
  • 63
  • 78