page_action

类型 对象
Mandatory No
范例
"page_action": {
  "browser_style": true,
  "default_icon": {
    "19": "button/geo-19.png",
    "38": "button/geo-38.png"
  },
  "default_title": "Whereami?",
  "default_popup": "popup/geo.html"
}
									

A page action is an icon that your extension adds inside the browser's URL bar.

Your extension may optionally also supply an associated 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 icon, 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 icon.

You can also create and manipulate page actions programmatically using the pageAction API .

Page actions are like browser actions, except that they are associated with particular web pages rather than with the browser as a whole. If an action is only relevant on certain pages, then you should use a page action and display it only on relevant pages. If an action is relevant to all pages or to the browser itself, use a browser action.

While browser actions are displayed by default, page actions are hidden by default. They can be shown for a particular tab by calling pageAction.show()  , passing in the tab's id . You can also change this default behavior using the show_matches 特性。

句法

page_action key is an object that may have any of three properties, all optional:

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

Use this to include a stylesheet in your popup that will make it 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 ,或 chrome://browser/content/extension-mac.css 在 macOS。

Firefox Style Guide 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.

default_icon 对象 or 字符串

Use this to specify an icon for the action.

It's recommended that you supply two icons here (19×19 pixels and 38×38 pixels), and specify them in an object with properties named "19" and "38" ,像这样:

    "default_icon": {
      "19": "geo-19.png",
      "38": "geo-38.png"
    }
									

If you do this, then the browser will pick the right size icon for the screen's pixel density.

You can just supply a string here:

"default_icon": "geo.png"
									

If you do this, then the icon will be scaled to fit the toolbar, and may appear blurry.

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, don't use <script> with embedded code, because you'll get a Content Violation Policy error. Instead, <script> must use the src attribute to load a separate script file.

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 icon, displayed when the user moves their mouse over 它。

这是 localizable property .

hide_matches 数组 of Match Pattern except <all_urls> Hide the page action by default for pages whose URLs match any of the given match patterns .

Note that page actions are always hidden by default unless show_matches is given. Therefore, it only makes sense to include this property if show_matches is also given, and will override the patterns in show_matches .

For example, consider a value like:

"page_action": {
  "show_matches": ["https://*.mozilla.org/*"],
  "hide_matches": ["https://developer.mozilla.org/*"]
}
										

This shows the page action by default for all HTTPS  URLs under the "mozilla.org" domain, except for pages under "developer.mozilla.org" .

show_matches 数组 of Match Pattern Show the page action by default for pages whose URLs match any of the given patterns.

另请参阅 hide_matches .

pinned 布尔 Optional. Defaults to true .

Controls whether or not the page action should appear in the location bar by default when the user installs the extension. This property is no longer supported since Firefox 89.

范例

"page_action": {
  "default_icon": {
    "19": "button/geo-19.png",
    "38": "button/geo-38.png"
  }
}

							

A page 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.pageAction.onClicked.addListener(handleClick);

							
"page_action": {
  "default_icon": {
    "19": "button/geo-19.png",
    "38": "button/geo-38.png"
  },
  "default_title": "Whereami?",
  "default_popup": "popup/geo.html"
}

							

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

浏览器兼容性

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