browser_action

类型 对象
Mandatory No
范例
"browser_action": {
  "browser_style": true,
  "default_icon": {
    "16": "button/geo-16.png",
    "32": "button/geo-32.png"
  },
  "default_title": "Whereami?",
  "default_popup": "popup/geo.html",
  "theme_icons": [{
    "light": "icons/geo-16-light.png",
    "dark": "icons/geo-16.png",
    "size": 16
  }, {
    "light": "icons/geo-32-light.png",
    "dark": "icons/geo-32.png",
    "size": 32
  }]
}
									

A browser action is a button that your extension adds to the browser's toolbar. The button has an icon, and may optionally have a popup whose content is specified using HTML, CSS, and JavaScript.

If you supply a popup, then the popup is opened when the user clicks the button, and your JavaScript running in the popup can handle the user's interaction with it. If you don't supply a popup, then a click event is dispatched to your extension's background scripts when the user clicks the button.

You can also create and manipulate browser actions programmatically using the browserAction API .

句法

browser_action key is an object that may have any of the following properties, all optional:

名称 类型 描述
browser_style 布尔 Optional, defaulting to false .

Use this to include a stylesheet in your popup that will make its look consistent with the browser's UI and with other extensions that use the browser_style property. Although this key defaults to false , it's recommended that you include it and set it to true in order to make your popups consistent with the look of the rest of the browser user interface.

In Firefox, the stylesheet can be seen at chrome://browser/content/extension.css, or chrome://browser/content/extension-mac.css on OS X. When setting dimensions, be aware that this style sheet currently sets box-sizing: border-box (见 box-sizing ).

Browser styles  describes the classes you can apply to elements in the popup in order to get particular styles.

latest-download example extension uses browser_style in its popup.

注意: 设置 browser_style to true prevents users from selecting text in an extension's popup or sidebar content. This is normal behavior. You can't select parts of the UI in the browser. However, You can work around this limitation to allow your users to select text in two ways:

  1. Set browser_style to false
  2. Use CSS styling on the body of your sidebar or popup's HTML to allow text selection by adding the rule -moz-user-select 采用值 all or text .
default_area 字符串 Defines the part of the browser in which the button is initially placed. This is a string that may take one of four values:

  • "navbar": the button is placed in the main browser toolbar, alongside the URL bar.
  • "menupanel": the button is placed in a popup panel.
  • "tabstrip": the button is placed in the toolbar that contains browser tabs.
  • "personaltoolbar": the button is placed in the bookmarks toolbar.

This property is only supported in Firefox.

This property is optional, and defaults to "navbar".

Firefox remembers the default_area setting for an extension, even if that extension is uninstalled and subsequently reinstalled. To force the browser to acknowledge a new value for default_area , the id of the extension must be changed.

An extension can't change the location of the button after it has been installed, but the user may be able to move the button using the browser's built-in UI customization mechanism.

default_icon 对象 or 字符串 Use this to specify one or more icons for the browser action. The icon is shown in the browser toolbar by default.

Icons are specified as URLs relative to the manifest.json file itself.

You can specify a single icon file by supplying a string here:

"default_icon": "path/to/geo.svg"
							

To specify multiple icons in different sizes, specify an object here. The name of each property is the icon's height in pixels, and must be convertible to an integer. The value is the URL. For example:

    "default_icon": {
      "16": "path/to/geo-16.png",
      "32": "path/to/geo-32.png"
    }
							

You cannot specify multiple icons of the same sizes.

Choosing icon sizes for more guidance on this.

default_popup 字符串

The path to an HTML file containing the specification of the popup.

The HTML file may include CSS and JavaScript files using <link> and <script> elements, just like a normal web page. However, <script> must have src attribute to load a file. Don't use <script> with embedded code, because you'll get a confusing Content Violation Policy error.

Unlike a normal web page, JavaScript running in the popup can access 所有 WebExtension APIs (subject, of course, to the extension having the appropriate permissions ).

这是 localizable property .

default_title 字符串 Tooltip for the button, displayed when the user moves their mouse over it. If the button is added to the browser's menu panel, this is also shown under the app icon.

这是 localizable property .

theme_icons 数组 This property enables you to specify different icons for themes depending on whether Firefox detects that the theme uses dark or light 文本。

If this property is present, it's an array containing at least one ThemeIcons 对象。 ThemeIcons 对象 contains three mandatory properties:

"dark"
A URL pointing to an icon. This icon displays when a theme using dark text is active (such as the Firefox Light theme, and the Default theme if no default_icon is specified).
"light"
A URL pointing to an icon. This icon displays when a theme using light text is active (such as the Firefox Dark theme).
"size"

The size of the two icons in pixels.

Icons are specified as URLs relative to the manifest.json file.

You should supply 16x16 and 32x32 (for retina display) ThemeIcons .

Choosing icon sizes

The browser action's icon may need to be displayed in different sizes in different contexts:

  • The icon is displayed in the browser toolbar. Older versions of Firefox supported the option of placing the icon in the browser's menu panel (the panel that opens when the user clicks the "hamburger" icon). In those versions of Firefox the icon in the menu panel was larger than the icon in the toolbar.
  • On a high-density display like a Retina screen, icons needs to be twice as big.

If the browser can't find an icon of the right size in a given situation, it will pick the best match and scale it. Scaling may make the icon appear blurry, so it's important to choose icon sizes carefully.

There are two main approaches to this. You can supply a single icon as an SVG file, and it will be scaled correctly:

"default_icon": "path/to/geo.svg"

					

Alternatively, you can supply several icons in different sizes, and the browser will pick the best match.

In Firefox:

So you can specify icons that match exactly, on both normal and Retina displays, by supplying three icon files, and specifying them like this:

"default_icon": {
  "16": "path/to/geo-16.png",
  "32": "path/to/geo-32.png",
  "64": "path/to/geo-64.png"
}

					

If Firefox can't find an exact match for the size it wants, then it will pick the smallest icon specified that's bigger than the ideal size. If all icons are smaller than the ideal size, it will pick the biggest icon specified.

范例

"browser_action": {
  "default_icon": {
    "16": "button/geo-16.png",
    "32": "button/geo-32.png"
  }
}

					

A browser action with just an icon, specified in 2 different sizes. The extension's background scripts can receive click events when the user clicks the icon using code like this:

 browser.browserAction.onClicked.addListener(handleClick);

					
"browser_action": {
  "default_icon": {
    "16": "button/geo-16.png",
    "32": "button/geo-32.png"
  },
  "default_title": "Whereami?",
  "default_popup": "popup/geo.html"
}

					

A browser action with an icon, a title, and a popup. The popup will be shown when the user clicks the button.

For a simple, but complete, extension that uses a browser action, see the walkthrough tutorial .

浏览器兼容性

BCD tables only load in the browser

另请参阅

Found a problem with this page?

最后修改: , 由 MDN 贡献者

  1. 浏览器扩展名
  2. 快速入门
    1. What are extensions?
    2. Your first extension
    3. Your second extension
    4. Anatomy of an extension
    5. Example extensions
    6. What next?
  3. 概念
    1. Using the JavaScript APIs
    2. Content scripts
    3. Match patterns
    4. Working with files
    5. 国际化
    6. Content Security Policy
    7. Native messaging
    8. Differences between API implementations
    9. Chrome incompatibilities
  4. 用户界面
    1. 用户界面
    2. Toolbar button
    3. Address bar button
    4. Sidebars
    5. Context menu items
    6. Options page
    7. Extension pages
    8. Notifications
    9. Address bar suggestions
    10. Developer tools panels
  5. 如何
    1. Intercept HTTP requests
    2. Modify a web page
    3. Insert external content
    4. Share objects with page scripts
    5. Add a button to the toolbar
    6. Implement a settings page
    7. Work with the Tabs API
    8. Work with the Bookmarks API
    9. Work with the Cookies API
    10. Work with contextual identities
    11. Interact with the clipboard
    12. Build a cross-browser extension
  6. Firefox differentiators
  7. JavaScript API
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browserSettings
    6. browsingData
    7. captivePortal
    8. clipboard
    9. 命令
    10. contentScripts
    11. contextualIdentities
    12. Cookie
    13. devtools
    14. dns
    15. downloads
    16. events
    17. extension
    18. extensionTypes
    19. find
    20. history
    21. i18n
    22. identity
    23. idle
    24. management
    25. menus
    26. notifications
    27. omnibox
    28. pageAction
    29. permissions
    30. pkcs11
    31. privacy
    32. proxy
    33. runtime
    34. search
    35. sessions
    36. sidebarAction
    37. storage
    38. tabs
    39. theme
    40. topSites
    41. 类型
    42. userScripts
    43. webNavigation
    44. webRequest
    45. windows
  8. Manifest keys
    1. 介绍
    1. 作者
    2. background
    3. browser_action
    4. browser_specific_settings
    5. chrome_settings_overrides
    6. chrome_url_overrides
    7. 命令
    8. content_scripts
    9. content_security_policy
    10. default_locale
    11. description
    12. developer
    13. devtools_page
    14. dictionaries
    15. externally_connectable
    16. homepage_url
    17. icons
    18. incognito
    19. manifest_version
    20. name
    21. offline_enabled
    22. omnibox
    23. optional_permissions
    24. options_page
    25. options_ui
    26. page_action
    27. permissions
    28. protocol_handlers
    29. short_name
    30. sidebar_action
    31. storage
    32. theme
    33. theme_experiment
    34. user_scripts
    35. version
    36. version_name
    37. web_accessible_resources
  9. Extension Workshop
    1. Develop
    2. Publish
    3. Manage
    4. Enterprise
  10. Contact us
  11. Channels
    1. Add-ons blog
    2. Add-ons forum
    3. Add-ons chat