WebExtension JavaScript API 可以在附加组件的后台脚本和附加组件定义的任何浏览器动作页面动作中使用。这里的部分API也可以通过附加组件的内容脚本访问(见内容脚本指南列表)。

要使用更强大的 API,您需要在您的 manifest.json 中申请权限

您可以使用 browser 命名空间访问这些 API。

function logTabs(tabs) {
  console.log(tabs);
}

browser.tabs.query({currentWindow: true}, logTabs);

许多 API 为异步,返回一个 Promise

function logCookie(c) {
  console.log(c);
}

function logError(e) {
  console.error(e);
}

var setCookie = browser.cookies.set(
  {url: "https://developer.mozilla.org/"}
);
setCookie.then(logCookie, logError);

请注意,这不同于 Google Chrome 的扩展系统,它使用 chrome 而非 browser 名字空间,并且对异步函数使用回调而不是 promises。为辅助移植,Firefox 实现的 WebExtensions 支持 chrome 和回调以及 browser 和 promises。Mozilla 也写了一个 polyfill 使使用 browser 和 promises 的代码能不经修改的在 Chrome 中使用:https://github.com/mozilla/webextension-polyfill

微软 Edge 使用 browser 名字空间,但尚不支持基于 promise 的异步API。目前在 Edge 中,异步 API 必须使用回调。

并非所有浏览器都支持这里的所有 API:详情见浏览器对 JavaScript API 的支持

alarms

Schedule code to run at a specific time in the future.

API reference documentation

bookmarks

The WebExtensions bookmarks API lets an extension interact with and manipulate the browser's bookmarking system. You can use it to bookmark pages, retrieve existing bookmarks, and edit, remove, and organize bookmarks.

API reference documentation

browserAction

Adds a button to the browser's toolbar.

API reference documentation

browserSettings

API reference documentation

browsingData

Enables extensions to clear the data that is accumulated while the user is browsing.

API reference documentation

clipboard

The clipboard API enables an extension to copy items to the system clipboard. Currently the API only supports copying images, but it's intended to support copying text and HTML in the future.

API reference documentation

commands

Listen for the user executing commands that you have registered using the commands manifest.json key.

API reference documentation

contentScripts

With the contentScripts API, an extension can register and unregister scripts at runtime.

API reference documentation

contextualIdentities

Work with contextual identities: list, create, remove, and update contextual identities.

API reference documentation

cookies

Enables extensions to get and set cookies, and be notified when they change.

API reference documentation

devtools.inspectedWindow

The devtools.inspectedWindow API lets a devtools extension interact with the window that the developer tools are attached to.

API reference documentation

devtools.network

The devtools.network API lets a devtools extension get information about network requests associated with the window that the devtools are attached to (the inspected window).

API reference documentation

devtools.panels

The devtools.panels API lets a devtools extension define its user interface inside the devtools window.

API reference documentation

dns

API reference documentation

downloads

Enables extensions to interact with the browser's download manager. You can use this API module to download files, cancel, pause, resume downloads, and show downloaded files in the file manager.

API reference documentation

events

Common types used by APIs that dispatch events.

API reference documentation

extension

Utilities related to your extension. Get URLs to resources packages with your extension, get the Window object for your extension's pages, get the values for various settings. Note that the messaging APIs in this module are deprecated in favor of the equivalent APIs in the runtime module.

API reference documentation

extensionTypes

Some common types used in other WebExtension APIs.

API reference documentation

find

Finds text in a web page, and highlights matches.

API reference documentation

history

Use the history API to interact with the browser history.

API reference documentation

i18n

Functions to internationalize your extension. You can use these APIs to get localized strings from locale files packaged with your extension, find out the browser's current language, and find out the value of its Accept-Language header.

API reference documentation

identity

Use the identity API to get an OAuth2 authorization code or access token, which an extension can then use to access user data from a service which supports OAuth2 access (such as a Google or a Facebook account).

API reference documentation

idle

Find out when the user's system is idle, locked, or active.

API reference documentation

management

Get information about installed add-ons.

API reference documentation

Add items to the browser's menu system.

API reference documentation

notifications

Display notifications to the user, using the underlying operating system's notification mechanism. Because this API uses the operating system's notification mechanism, the details of how notifications appear and behave may differ according to the operating system and the user's settings.

API reference documentation

omnibox

Enables extensions to implement customised behavior when the user types into the browser's address bar.

API reference documentation

pageAction

A page action is a clickable icon inside the browser's address bar.

API reference documentation

permissions

Extensions need permissions to access more powerful WebExtension APIs. They can ask for permissions at install time, by including the permissions they need in the permissions manifest.json key. The main advantages of asking for permissions at install time are:

API reference documentation

pkcs11

The pkcs11 API enables an extension to enumerate PKCS #11 security modules, and to make them accessible to the browser as sources of keys and certificates.

API reference documentation

privacy

Access and modify various privacy-related browser settings.

API reference documentation

proxy

Use the proxy API to proxy web requests. There are two different ways you can do this:

API reference documentation

runtime

This module provides information about your extension and the environment it's running in.

API reference documentation

Retrieves search engines and executes a search with a specific search engine.

API reference documentation

sessions

Use the sessions API to list, and restore, tabs and windows that have been closed while the browser has been running.

API reference documentation

sidebarAction

Gets and sets properties of an extension's sidebar.

API reference documentation

storage

Enables extensions to store and retrieve data, and listen for changes to stored items.

API reference documentation

tabs

Interact with the browser's tab system.

API reference documentation

theme

Enables browser extensions to update the browser theme.

API reference documentation

topSites

Use the topSites API to get an array containing pages that the user has visited often and frequently.

API reference documentation

types

Defines the BrowserSetting type, which is used to represent a browser setting.

API reference documentation

webNavigation

Add event listeners for the various stages of a navigation. A navigation consists of a frame in the browser transitioning from one URL to another, usually (but not always) in response to a user action like clicking a link or entering a URL in the location bar.

API reference documentation

webRequest

Add event listeners for the various stages of making an HTTP request. The event listener receives detailed information about the request, and can modify or cancel the request.

API reference documentation

windows

Interact with browser windows. You can use this API to get information about open windows and to open, modify, and close windows. You can also listen for window open, close, and activate events.

API reference documentation

文档标签和贡献者

最后编辑者: SphinxKnight,