page_action 编辑

TypeObject
MandatoryNo
Example
"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 property.

Syntax

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

NameTypeDescription
browser_styleBoolean

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, or chrome://browser/content/extension-mac.css on macOS.

The Firefox Style Guide describes the classes you can apply to elements in the popup in order to get particular styles.

The latest-download example extension uses browser_style in its popup.

default_iconObject or String

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", like this:

    "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_popupString

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 all the WebExtension APIs (subject, of course, to the extension having the appropriate permissions).

This is a localizable property.

default_titleString

Tooltip for the icon, displayed when the user moves their mouse over it.

This is a localizable property.

hide_matchesArray 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": ["/wiki/*"]
}

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

show_matchesArray of Match Pattern

Show the page action by default for pages whose URLs match any of the given patterns.

See also hide_matches.

pinnedBoolean

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.

Example

"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.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:61 次

字数:9762

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文