如何从背景页面获取当前标签 [英] how to get current tabId from background page
本文介绍了如何从背景页面获取当前标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从背景页面获取当前的tabId?
当前tabId是用户可以看到其内容的标签。
how to get current tabId from background page? current tabId is the tab that user can see its content.
background.html
background.html
<html>
<head>
<script>
if(typeof localStorage.state == 'undefined')
localStorage.state = 'off'
chrome.browserAction.onClicked.addListener(function(tab) {
if(localStorage.state == 'on')
{
localStorage.state = 'off';
}
else
{
localStorage.state = 'on';
}
chrome.browserAction.setBadgeText({text: localStorage.state, tabId: tab.id});
chrome.tabs.sendRequest(tab.id, {state: localStorage.state});
//chrome.tabs.sendRequest(tab.id, {state: localStorage.state});
});
</script>
</head>
推荐答案
getSelected
已被已弃用。新的方式是:
getSelected
has been deprecated. The new way to do it is:
chrome.tabs.query(
{currentWindow: true, active : true},
function(tabArray){...}
)
如果您想在活动选项卡上执行一些回调,您可以按如下方式包装:
If you want to perform some callback on the active tab, you can wrap the above as so:
function doInCurrentTab(tabCallback) {
chrome.tabs.query(
{ currentWindow: true, active: true },
function (tabArray) { tabCallback(tabArray[0]); }
);
}
例如
For example
var activeTabId;
doInCurrentTab( function(tab){ activeTabId = tab.id } );
这篇关于如何从背景页面获取当前标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文