Page Actions

优质
小牛编辑
131浏览
2023-12-01

Page Actions

Contents

  1. Manifest
    1. UI的组成部分
      1. 提示
        1. 示例
          1. API reference: chrome.pageAction
            1. Methods
              1. hide
              2. setIcon
              3. setPopup
              4. setTitle
              5. show
            2. Events
              1. onClicked

          使用page actions把图标放置到地址栏里。page actions定义需要处理的页面的事件,但是它们不是适用于所有页面的。下面是一些page actions的示例:

          • 订阅该页面的RSS feed
          • 为页面的图片做一个幻灯片

          在下面的屏幕截图中的RSS图标,提供了一个可以让用户订阅当前页面RSS Feed的page action。

          想让扩展图标总是可见,则使用browser action

          注意: 打包的应用程序不能使用page actions。

          Manifest

          extension manifest 中用下面的方式注册你的page action:

          {
            "name": "My extension",
            ...
            "page_action": {
              "default_icon": "icons/foo.png", // optional 
              "default_title": "Do action",    // optional; shown in tooltip 
              "default_popup": "popup.html"    // optional 
            },
            ...
          }

          UI的组成部分

          同browser actions一样,page actions 可以有图标、提示信息、 弹出窗口。但没有badge,也因此,作为辅助,page actions可以有显示和消失两种状态。阅读browser action UI. 可以找到图标、提示信息、 弹出窗口相关信息。

          使用方法 show()hide() 可以显示和隐藏page action。缺省情况下page action是隐藏的。当要显示时,需要指定图标所在的标签页,图标显示后会一直可见,直到该标签页关闭或开始显示不同的URL (如:用户点击了一个连接)

          提示

          为了获得最佳的视觉效果,请遵循下列指导方针:

          • 只对少数页面使用page action;
          • 不要对大多数页面使用它,如果功能需要,使用 browser actions代替。
          • 没事别总让图标出现动画,那会让人很烦。

          示例

          你可以在examples/api/pageAction 找到使用page action的简单示例,其它例子和源代码帮助查看Samples

          API reference: chrome.pageAction

          Properties

          getLastError

          chrome.extensionlastError

          Methods

          hide

          void chrome.pageAction.hide(, integer tabId)

          Undocumented.

          隐藏page action.

          Parameters

          tabId ( optional enumerated Type array of integer )
          Undocumented.
          要执行这个动作的标签ID。
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

          Returns

          Callback function

          The callback parameter should specify a function that looks like this:

          If you specify the callback parameter, it should specify a function that looks like this:

          function(Type param1, Type param2) {...};

          This function was added in version . If you require this function, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

          setIcon

          void chrome.pageAction.setIcon(, object details)

          Undocumented.

          为page aciton设置图标。图标可以是一个图片的路径或者是从一个canvas元素提取的像素信息.。无论是图标路径还是canvas的 imageData,这个属性必须被指定。

          Parameters

          details ( optional enumerated Type array of object )
          Undocumented.
          Description of this parameter from the json schema.
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.
          tabId ( optional enumerated Type array of integer )
          Undocumented.
          要执行这个动作的标签ID。
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.
          imageData ( optional enumerated Type array of ImageData )
          Undocumented.
          图片的像素信息。必须是一个ImageData 对象(例如:一个canvas元素)。
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.
          path ( optional enumerated Type array of string )
          Undocumented.
          图片在扩展中的的相对路径。
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.
          iconIndex ( optional enumerated Type array of integer )
          Undocumented.
          不建议。manifest中定义的,0开始的图标数组下标。
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

          Returns

          Callback function

          The callback parameter should specify a function that looks like this:

          If you specify the callback parameter, it should specify a function that looks like this:

          function(Type param1, Type param2) {...};

          This function was added in version . If you require this function, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

          setPopup

          void chrome.pageAction.setPopup(, object details)

          Undocumented.

          设置一个点击page actions时显示在popup中的HTML。

          Parameters

          details ( optional enumerated Type array of object )
          Undocumented.
          Description of this parameter from the json schema.
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.
          tabId ( optional enumerated Type array of integer )
          Undocumented.
          要设置popup的标签ID。
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.
          popup ( optional enumerated Type array of string )
          Undocumented.
          popup中显示的html文件。如果设置为空字符(''),将不显示popup。
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

          Returns

          Callback function

          The callback parameter should specify a function that looks like this:

          If you specify the callback parameter, it should specify a function that looks like this:

          function(Type param1, Type param2) {...};

          这个功能已经在chromium 5.0.316.0版本添加。如果你需要这个功能,可以通过manifest的minimum_chrome_version键值来确认你的扩展不会运行在早期的浏览器版本。

          setTitle

          void chrome.pageAction.setTitle(, object details)

          Undocumented.

          设置page action的标题,这将显示在tooltip中。

          Parameters

          details ( optional enumerated Type array of object )
          Undocumented.
          Description of this parameter from the json schema.
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.
          tabId ( optional enumerated Type array of integer )
          Undocumented.
          要设置标题的标签ID。
          title ( optional enumerated Type array of string )
          Undocumented.
          提示信息字符串。
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

          Returns

          Callback function

          The callback parameter should specify a function that looks like this:

          If you specify the callback parameter, it should specify a function that looks like this:

          function(Type param1, Type param2) {...};

          This function was added in version . If you require this function, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

          show

          void chrome.pageAction.show(, integer tabId)

          Undocumented.

          显示page action,无论标签是否被选中。

          Parameters

          tabId ( optional enumerated Type array of integer )
          Undocumented.
          要执行这个动作的标签ID。
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

          Returns

          Callback function

          The callback parameter should specify a function that looks like this:

          If you specify the callback parameter, it should specify a function that looks like this:

          function(Type param1, Type param2) {...};

          This function was added in version . If you require this function, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

          Events

          onClicked

          chrome.pageAction.onClicked.addListener(function(Tab tab) {...});

          Undocumented.

          当page action图标被点击的时候调用,如果page action是一个popup,这个事件将不会触发。Fi

          Parameters

          tab ( optional enumerated Tab array of paramType )
          Undocumented.
          Description of this parameter from the json schema.
          This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

          Types

          type name