Krmtlis
Krmtlis

Reputation: 23

Unable to use var tabs = require("sdk/tabs"); in WebExtension add-on

"sdk/tabs" seems impossible to use in my Firefox add-on. I wonder what's wrong. My background.js stops processing any code after this part:

var tabs = require("sdk/tabs");

E.g. if I run this code, the console will output "error1?" -- including everything above, but nothing from underneath that snippet.

console.log('error1?');
var tabs = require("sdk/tabs");
console.log('error2?');

// Listen for tab content loads.
tabs.on('ready', function(tab) {
  console.log('error3?');
  console.log('tab is loaded', tab.title, tab.url);
  console.log('error4?');
});

console.log('error5?');

I have included "tabs" as permission in my manifest.json file. Do I need to include anything else to use "sdk/tabs"?

Upvotes: 1

Views: 333

Answers (1)

Makyen
Makyen

Reputation: 33306

You mention a manifest.json file and having the tabs permission in it. manifest.json files are only used in WebExtension based add-ons. You desire to use require("sdk/tabs") which is only available in Add-on SDK extensions.

You appear to mixing APIs between WebExtensions and the Firefox Add-on SDK. These are two of the four different types of extensions for Firefox. None of the Add-on SDK APIs (neither High-Level, or Low-Level) are available from a WebExtension. Similarly, the WebExtension JavaScript APIs are not available from an Add-on SDK based extension.

Specifically, you appear to be developing a WebExtension. The sdk/tabs API is for the Add-on SDK. It will definitely not work in a WebExtension based add-on. In general, if you see require() you are almost certainly dealing with the Add-on SDK which will not work in your WebExtension add-on. Thus, you will not be able to use require("sdk/tabs") in your WebExtension based add-on.

Firefox/Mozilla has four different types of extensions:

  1. Add-on SDK: These add-ons are described by a package.json file which is initially generated by executing jpm init. These extensions will often use require() to load either High-Level, or Low-Level APIs to interface with Firefox. Currently, these add-ons are wrapped into a bootstrapped extension when they are loaded for testing by jpm run or consolidated into an .xpi file by jpm xpi for distribution (i.e. upload to AMO/Mozilla). In other words, they are bootstrapped extensions with an SDK wrapper.
    Mozilla appears to be committed to continuing to support Add-on SDK based extensions as long as the extension does not use require("chrome"), or otherwise depend on XUL, XPCOM, or XBL.
    Most of the things that can be done in a bootstrapped extension can be done in an Add-on SDK based one. However, many such things bypass the SDK which forfeits a significant portion of the benefits of using the Add-on SDK.
  2. WebExtensions: These add-ons are described by a manifest.json file. This API is similar to what is used for Google Chrome extensions. While Mozilla is claiming that this API is the future of Firefox extensions, this API is still in development. For now, you are probably best off developing and testing your WebExtension add-on with Firefox Developer Edition, or Firefox Nightly. You should also make careful note of what version of Firefox is required for the functionality you desire to use. This information is contained in the "Browser compatibility" section of the MDN documentation pages.
    WebExtensions use a significantly different API. There is, intentionally, no ability to use the interfaces provided by any of the other add-on types.
  3. Bootstrapped: These extensions are also commonly called "restartless" because they were the first type of Mozilla extension which did not require the application to be restarted in order to load/unload the add-on. However, restartless is a descriptor of how they function. Using "restartless" as the name for this type of add-on is confusing because both Add-on SDK and WebExtension add-ons also do not require the application to be restarted upon load or unload of the add-on. For that reason, there is a tendency to no longer use "restartless" as the name for this type of add-on.
    These add-ons have a JavaScript file called bootstrap.js which must contain entry points (functions) which are called for add-on startup(), shutdown(), install() and uninstall(). These add-ons contain a install.rdf that describes the add-on.
    They usually, but not always, also contain a chrome.manifest file that describes how the files and directories in the extension relate to the Mozilla application (e.g. Firefox).
    Most, but not all, of the things that can be done in overlay/XUL/Legacy extensions can be accomplished in bootstrapped add-ons. Anything that can be done in the Add-on SDK can be done in a bootstrapped extension (Add-on SDK extensions are bootstrapped add-ons with some JavaScript based API layers). Mozilla has stated that they plan to deprecate "add-ons that depend on XUL, XPCOM, and XBL." While not all bootstrapped add-ons depend on these technologies, there is a tendency for for bootstrapped add-ons to operate at a lower level than Add-on SDK and WebExtension add-ons. Thus, they are more likely to use these technologies. While there are some that are saying that all bootstrapped add-ons are planned to be deprecated, it is not clear that is the case. After all, Add-on SDK extensions are not being deprecated (unless they use require("chrome"), or otherwise depend on XUL, XPCOM, or XBL) and all Add-on SDK extensions are bootstrapped extensions, just with an SDK wrapper.
  4. Overlay/XUL/Legacy: These add-ons contain a install.rdf that describes the add-on and a chrome.manifest file to describe how the add-on's files relate to (e.g. overlay) the application's files. How the add-on functions with the application is completely dependent on the relationships described in the chrome.manifest file. The only exceptions to this are a few things like icons for the extension and the file describing the extension's options which are indicated in the install.rdf file. These extensions interact with the application (e.g. Firefox) at a very low level. This tends to make them more likely to break when changes are made to the application.
    All Overlay/XUL/Legacy extensions are planned to be deprecated.

Upvotes: 1

Related Questions