Search code examples
javascriptfirefoxfirefox-addonfirefox-addon-restartless

CustomEvent error in Firefox bootstrapped addon


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?


Solution

  • 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}});