Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Comparison with XUL/XPCOM extensions

我们的志愿者还没有将这篇文章翻译为 中文 (简体)加入我们帮助完成翻译!

This article is a technical comparison between WebExtensions and "classic" extensions developed using direct XUL manipulation and direct access to XPCOM. It's intended to help orient people who maintain an add-on like this, and who are planning to port the add-on to WebExtensions.

This article covers both overlay extensions and bootstrapped extensions, but not extensions developed using the Add-on SDK. For the Add-on SDK, please see Comparison with the Add-on SDK.

At a very basic level, XUL/XPCOM extensions are similar to WebExtensions. They both include:

  • manifest files defining metadata for the add-on and some aspects of its behavior.
  • JavaScript code that gets access to a set of privileged JavaScript APIs and that stays loaded for as long as the add-on itself is enabled.
  • the ability to add specific UI elements, such as buttons, to the browser.

Beyond that, though, the systems are very different. In particular:

  • Compared with XUL/XPCOM extensions, WebExtensions provide much more limited options for the add-on's UI, and a much more limited set of privileged JavaScript APIs.
  • WebExtensions can only access web content by injecting separate scripts into web pages and communicating with them using a messaging API (note, though, that this is also true of XUL/XPCOM extensions that expect to work with multiprocess Firefox).

Manifest

XUL/XPCOM extensions have two manifest files:

  • the install.rdf contains metadata about the extension such as its name, icons, and so on
  • the chrome.manifest, that tells Firefox where it can find the components of the extension, including XUL overlays for the extension's interface, scripts for its behavior, and files containing localized strings.

WebExtensions have a single manifest file called manifest.json, that has a similar purpose. You use it to specify the add-on's name, description, icons, and so on, as well as to specify any buttons it adds to Firefox and to list scripts it needs to run. To get an overview of the components of a WebExtension, and how they are specified in manifest.json, see "Anatomy of a WebExtension".

Learn more

UI

XUL/XPCOM extensions can build their UI by directly manipulating the XUL used to specify the browser's own UI. They do this either using overlays or, in the case of bootstrapped/restartless extensions, using JavaScript to modify the XUL document. They can not only add any elements to the browser's UI, they can also modify or remove existing elements. They can also use APIs like CustomizableUI.jsm to build their UI.

WebExtensions don't get this kind of direct access. Instead, a combination of manifest.json keys and JavaScript APIs enable them to add a limited set of UI components to the browser. The available components are:

Name Description Specified using
Browser action Button in the browser toolbar, with an optional popup panel.

browser_action manifest key

browserAction API

Page action Button in the URL bar, with an optional popup panel.

page_action manifest key

pageAction API

Commands Keyboard shortcuts.

commands manifest key

commands API

Context menu Adds items and submenus to the browser's context menu. contextMenus API

Privileged APIs

Both XUL/XPCOM extensions and WebExtensions can contain scripts that stay loaded for as long as the extension itself is enabled, and that have access to a set of privileged APIs. However, XUL/XPCOM extensions get access to a much wider range of APIs.

The scripts packaged with XUL/XPCOM extensions get access to the full set of XPCOM APIs and JavaScript code modules through the Components object. They also get direct access to the browser's internals through globals like gBrowser.

The equivalent WebExtension scripts are called background scripts, and they get access to a much smaller set of high-level JavaScript APIs. To see all the privileged APIs available to background scripts, see the summary API page. Background scripts also get a window global, with all the DOM objects that are available in a normal web page.

There are vastly more APIs available to XUL/XPCOM extensions than are available to WebExtensions, and for many XUL/XPCOM APIs, there isn't a WebExtensions substitute. The table below lists every API in the popular Services.jsm module, describe what the equivalent WebExtensions API would be, if there is one.

You'll see that many APIs have no WebExtensions equivalent yet. However we are intending to extend the WebExtension APIs to support the needs of add-on developers, so if you have ideas, we'd love to hear them. You can reach us on the dev-addons mailing list or #webextensions on IRC.

Services.jsm API WebExtensions equivalent
nsIAndroidBridge None
nsIXULAppInfo
nsIXULRuntime
None
nsIAppShellService None
nsIBlocklistService None
nsICacheService None
nsICacheStorageService None
nsIClipboard None
nsIConsoleService window.console
nsIContentPrefService None
nsICookieManager2 cookies
nsIMessageSender Content scripts
CrashManager.jsm None
nsIDirectoryService
nsIProperties
None
nsIDOMStorageManager None
nsIDOMRequestService None
nsIDownloadManager downloads
nsIDroppedLinkHandler None
nsIEventListenerService None
nsIEffectiveTLDService None
nsIFocusManager None
nsIIOService
nsIIOService2
None
nsILocaleService i18n
nsILoginManager None
nsIWinMetroUtils None
nsIMessageBroadcaster
nsIFrameScriptLoader
Content scripts
nsIObserverService None
nsIPermissionManager None
nsIMessageBroadcaster
nsIProcessScriptLoader
Content scripts
nsIPrefBranch
nsIPrefBranch2
nsIPrefService
See Settings.
nsIPromptService None
mozIJSSubScriptLoader None
nsIScriptSecurityManager None
nsIBrowserSearchService None
nsIAppStartup None
mozIStorageService storage
nsIStringBundleService i18n
nsIPropertyBag2 None
nsITelemetry None
nsIThreadManager None
nsIURIFixup None
nsIURLFormatter None
nsIVersionComparator None
nsIWindowMediator None
nsIWindowWatcher None

Learn more

Interacting with web content

Historically, XUL/XPCOM extensions have been able to get direct access to web content. For example, they can directly access and modify the page DOM using gBrowser:

gBrowser.contentWindow.document.querySelector("h1").innerHTML = "yadda yadda";

However, this is only possible in single-process Firefox. In multiprocess Firefox, web content and add-on code run in different processes, so this direct access is no longer possible, and extensions which rely on it will break. Multiprocess Firefox is coming soon, and multiprocess compatibility will be a necessity.

XUL/XPCOM extensions can still interact with web content in multiprocess Firefox by refactoring the code that accesses web content into separate scripts called frame scripts, and using the message manager to communicate with these scripts. But this is complex and can involve deep changes to the extension's code.

WebExtensions are multiprocess-compatible by default: code that interacts with web content is factored into separate scripts called content scripts, that can communicate with the rest of the add-on using a messaging API.

Learn more

Localization

In a XUL/XPCOM extension you handle localization by supplying DTD or properties for each supported language, and referring to them using locale statements inside the chrome.manifest. You can then include localized strings in UI elements or in code.

The general approach with WebExtensions is similar, but the details are all different. With WebExtensions you supply localized strings as a collection of JSON files, one for each locale.

To retrieve localized strings in add-on code, use the i18n API.

WebExtensions don't have direct support for localizing strings appearing in HTML, so you have to do this yourself, using JavaScript to retrieve localized strings and to replace the HTML with the localized version.

Learn more

Settings

XUL/XPCOM extensions typically store settings using the XPCOM preferences service or the inline options system.

With WebExtensions you write an HTML file that presents the settings UI, which can include a script for persisting the settings. The script gets access to all the WebExtensions APIs, and it's generally expected that you should use the storage API to persist settings.

You then assign the HTML file's URL to the options_ui key in manifest.json. Your settings page then appears in the add-on's entry in the Add-ons Manager.

Note that WebExtensions does not give you access to the Preferences API, so you can't directly get or set the browser's own preferences.

Learn more

文档标签和贡献者

 此页面的贡献者: wbamberg
 最后编辑者: wbamberg,