firefox-addonxulfirefox-addon-webextensionsfirefox-addon-overlay

How do I migrate a legacy XPCOM extension to WebExtensions?


Embedded WebExtensions talks all about how to "... embed a WebExtension in a classic bootstrapped extension or an Add-on SDK add-on."

But I've got neither a "bootstrapped" nor "Add-on SDK" extension, just a decade old plain old XPCOM/overlay extension. I don't have a bootstrap.js to have a startup, nor do I use the Add-on SDK for whatever it does.

I tried adding

<em:bootstrap>true</em:bootstrap>

But all that accomplishes is completely destroying the extension, it only loads that (empty) new bootstrap.js file.

Indeed, I want to migrate: The data that my classic extension has needs to be exported to the webext version, for good user experience.


Solution

  • At our tracking bug a user has posted a helpful link:

    https://github.com/mdn/webextensions-examples/tree/master/embedded-webextension-overlay

    Which boils down to

    const {
      LegacyExtensionsUtils,
    } = Components.utils.import("resource://gre/modules/LegacyExtensionsUtils.jsm");
    
    const myOverlayEmbeddedWebExtension = LegacyExtensionsUtils.getEmbeddedExtensionFor({
      id: addonId, resourceURI: baseURI,
    });
    
    myOverlayEmbeddedWebExtension.startup().then(({browser}) => {
      dump(`${addonId} - embedded webext started\n`);
      browser.runtime.onMessage.addListener(msg => {
        dump(`${addonId} - received message from embedded webext ${msg}\n`);
      });
    }).catch(err => {
      Components.utils.reportError(
        `${addonId} - embedded webext startup failed: ${err.message} ${err.stack}\n`
      );
    });
    

    Which is surely the equivalent of what the bootstrap/SDK code is doing for you.