与浏览器交互-标签页管理
chrome标签模块被用于和浏览器的标签系统交互。此模块被用于创建,修改,重新排列浏览器中的标签。.
Manifest
几乎所有chrome标签方法需要你在extension manifest中定义标签权限。例如:
{
"name": "My extension",
...
**"permissions": [
"tabs"
]**,
...
}
不需要"标签"权限的方法是:create 和 update.
示例
你可以在examples/api/tabs 目录下找到运用标签模块的示例。在源代码中查看帮助或者示例,请查阅Samples
。
API reference: chrome.tabs
Properties
getLastError
chrome.extensionlastError
Methods
captureVisibleTab
void chrome.tabs.captureVisibleTab(, integer windowId
, object options
, function callback
)
Undocumented.
在特定窗口中,抓取当前选中标签的可视区域。 这要求必须在 host permission 中指定对标签URL的访问权限。
Parameters
windowId
( optional enumerated Type array of integer )
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.
options
( optional enumerated Type array of object )
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.
format
( optional enumerated Type array of string ["jpeg", "png"] )
Undocumented.
生成的图像的格式。默认是jpeg。
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.
quality
( optional enumerated Type array of integer )
Undocumented.
如果格式是'jpeg',控制结果图像的质量。此参数对PNG图像无效。当质量降低的时候,抓取的画质会下降,需要存储的字节数也会递减。
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.
callback
( optional enumerated Type array of function )
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.
Returns
Callback function
回调 参数 应该如下定义:
If you specify the callback parameter, it should specify a function that looks like this:
function(string dataUrl) {...};
dataUrl
( optional enumerated Type array of string )
Undocumented.
被抓取标签的可视区域的URL。此URL可能会作为图像的src属性。
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.
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.
connect
Port chrome.tabs.connect(, integer tabId
, object connectInfo
)
Undocumented.
连接到特定标签中的content script(s)。 事件 chrome.extension.onConnect 将被触发给每个指定页面上运行的content script扩展。了解更多请查看 Content Script Messaging。
Parameters
tabId
( optional enumerated Type array of integer )
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.
connectInfo
( 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.
name
( optional enumerated Type array of string )
Undocumented.
会被传输到监听连接事件的content scirpt的onConnect函数当中。
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
paramName
( optional enumerated Port array of paramType )
Undocumented.
可以和在指定标签中运行的内容脚本通信。当标签被关闭或者不存在时,端口的 onDisconnect 事件被触发。
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.
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.
create
void chrome.tabs.create(, object createProperties
, function callback
)
Undocumented.
创建新的标签。注: 无需请求manifest的标签权限,此方法也可以被使用。
Parameters
createProperties
( 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.
windowId
( optional enumerated Type array of integer )
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.
index
( optional enumerated Type array of integer )
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.
url
( optional enumerated Type array of string )
Undocumented.
标签导航的初始页面。完整的URL 必须包含一个前缀 (如 'http://www.google.com', 不能写为 'www.google.com')。 相对 URL则与扩展所在的页面相对, 默认值为新标签页面。
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.
selected
( optional enumerated Type array of boolean )
Undocumented.
标签是否成为选中标签。默认为true。
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.
pinned
( optional enumerated Type array of boolean )
Undocumented.
标签是否被固定。默认值为false。
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.
callback
( optional enumerated Type array of function )
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.
Returns
Callback function
The callback parameter should specify a function that looks like this:
回调 参数 应该如下定义:
function(Tab tab) {...};
tab
( optional enumerated Tab array of paramType )
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.
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.
detectLanguage
void chrome.tabs.detectLanguage(, integer tabId
, function callback
)
Undocumented.
检测标签内容的主要语言。
Parameters
tabId
( optional enumerated Type array of integer )
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.
callback
( optional enumerated Type array of function )
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.
Returns
Callback function
回调 参数 应该如下定义:
If you specify the callback parameter, it should specify a function that looks like this:
function(string language) {...};
language
( optional enumerated Type array of string )
Undocumented.
ISO 语言编码,例如en 或者 fr。若要查看此方法支持的完整语言列表,请参阅kLanguageInfoTable 。第2列和第4列会被检测,而且第一个不为空的值会被返回。 简体中文是个例外,返回zh-CN。对于未知语言,返回und。
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.
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.
executeScript
void chrome.tabs.executeScript(, integer tabId
, object details
, function callback
)
Undocumented.
向页面注入JavaScript 脚本执行。要了解详情,请查阅内容脚本文档的 programmatic injection 部分。
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.
details
( optional enumerated Type array of object )
Undocumented.
要执行的脚本内容,可选code或者file,但不能同时选两者。
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.
code
( 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.
file
( 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.
allFrames
( optional enumerated Type array of boolean )
Undocumented.
true的时候,给所有frame执行脚本。默认为false,只给顶级frame执行脚本。
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.
callback
( optional enumerated Type array of function )
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:
回调 参数 应该如下定义:
function() {...};
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.
get
void chrome.tabs.get(, integer tabId
, function callback
)
Undocumented.
获取指定标签的细节信息。
Parameters
tabId
( optional enumerated Type array of integer )
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.
callback
( optional enumerated Type array of function )
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.
Returns
回调函数
回调 参数 应该如下定义:
If you specify the callback parameter, it should specify a function that looks like this:
function(Tab tab) {...};
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.
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.
getAllInWindow
void chrome.tabs.getAllInWindow(, integer windowId
, function callback
)
Undocumented.
获取指定窗口所有标签的细节信息。
Parameters
windowId
( optional enumerated Type array of integer )
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.
callback
( optional enumerated Type array of function )
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.
Returns
回调函数
T 回调 参数 应该如下定义:
If you specify the callback parameter, it should specify a function that looks like this:
function(array of Tab tabs) {...};
tabs
( optional enumerated Type array of Tab array of paramType 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.
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.
getCurrent
void chrome.tabs.getCurrent(, function callback
)
Undocumented.
获取生成脚本调用的标签。此函数不适用于脚本被非标签内容调用的情况。(例如: 背景页 或者 弹出视图) 。
Parameters
callback
( optional enumerated Type array of function )
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.
Returns
回调函数
回调 参数 应该如下定义:
If you specify the callback parameter, it should specify a function that looks like this:
function(Tab tab) {...};
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.
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.
getSelected
void chrome.tabs.getSelected(, integer windowId
, function callback
)
Undocumented.
获取特定窗口指定的标签。
Parameters
windowId
( optional enumerated Type array of integer )
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.
callback
( optional enumerated Type array of function )
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.
Returns
回调函数
回调 参数 应该如下定义:
If you specify the callback parameter, it should specify a function that looks like this:
function(Tab tab) {...};
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. 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.
insertCSS
void chrome.tabs.insertCSS(, integer tabId
, object details
, function callback
)
Undocumented.
向页面注入CSS。要了解详情,请参阅内容脚本文档中的 programmatic injection 部分。
Parameters
tabId
( optional enumerated Type array of integer )
Undocumented.
要注入CSS的标签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.
details
( optional enumerated Type array of object )
Undocumented.
要注入的CSS的内容,可选code或者file,但不能同时选两者。
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.
code
( optional enumerated Type array of string )
Undocumented.
要注入的CSS代码。
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.
file
( optional enumerated Type array of string )
Undocumented.
要注入的CSS文件。
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.
allFrames
( optional enumerated Type array of boolean )
Undocumented.
true的时候,给所有frame注入CSS。默认为false,只给顶级frame注入CSS。
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.
callback
( optional enumerated Type array of function )
Undocumented.
当所有的CSS 被注入后,回调被调用。
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
回调函数
回调 参数 应该如下定义:
function() {...};
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.
move
void chrome.tabs.move(, integer tabId
, object moveProperties
, function callback
)
Undocumented.
把标签移动至窗口内特定的位置,或者移至一个新窗口。请注意只能在普通窗口之间切移(window.type === "normal") 。
Parameters
tabId
( optional enumerated Type array of integer )
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.
moveProperties
( 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.
windowId
( optional enumerated Type array of integer )
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.
index
( optional enumerated Type array of integer )
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.
callback
( optional enumerated Type array of function )
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.
Returns
回调函数
回调 参数 应该如下定义:
function(Tab tab) {...};
tab
( optional enumerated Tab array of paramType )
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.
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.
remove
void chrome.tabs.remove(, integer tabId
, function callback
)
Undocumented.
关闭标签。
Parameters
tabId
( optional enumerated Type array of integer )
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.
callback
( optional enumerated Type array of function )
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.
Returns
回调函数
回调 参数 应该如下定义:
function() {...};
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.
sendRequest
void chrome.tabs.sendRequest(, integer tabId
, any request
, function responseCallback
)
Undocumented.
向特定的标签content script发送一个的请求, 并在响应返回时,可附带一个回调。在所有content script响应请求后, chrome.extension.onRequest 事件将会为当前扩展触发。
Parameters
tabId
( optional enumerated Type array of integer )
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.
request
( optional enumerated Type array of any )
Undocumented.
Description of this parameter from the json schema.
- 场景 案例分析,SQL优化这么做就对了!
- 抖音直播违禁词有哪些?这6类直播违禁敏感词,一定不要用
- 用CSS写出更炫丽的渐变
- 如何用 CSS 中写出超级美丽的阴影效果
- 如何写 CSS 重置(RESET)样式?
- 如何过渡到 Web 3.0?
- 如何用30天时间,建立您自己的社交媒体营销矩阵?
- 与浏览器交互-chrome.devtools.* APIs(chrome调试台)
- 开始设计一个浏览器扩展-加载脚本(Content Scripts)
- 开始设计一个浏览器扩展-跨域 XMLHttpRequest 请求
- 与浏览器交互-标签页管理
- 开始设计一个浏览器扩展-插件发布在不同国家和地区 (国际化 i18n)
- 与浏览器交互-chrome.history(浏览器浏览记录)
- 开始设计一个浏览器扩展-浏览器权限申请及管理(Optional Permissions )
- 改变浏览器外观-发送桌面通知
- 基础文档-chrome插件的调试