【问题标题】:how to get current tabId from background page如何从后台页面获取当前tabId
【发布时间】:2011-09-05 02:38:22
【问题描述】:

如何从后台页面获取当前的tabId? 当前的tabId是用户可以看到其内容的标签。

背景.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>

【问题讨论】:

    标签: google-chrome-extension


    【解决方案1】:

    getSelected 一直是deprecated。新的做法是:

    chrome.tabs.query(
      {currentWindow: true, active : true},
      function(tabArray){...}
    )
    

    如果你想在活动标签上执行一些回调,你可以把上面的包装成这样:

    function doInCurrentTab(tabCallback) {
        chrome.tabs.query(
            { currentWindow: true, active: true },
            function (tabArray) { tabCallback(tabArray[0]); }
        );
    }
    

    例如

    var activeTabId;
    doInCurrentTab( function(tab){ activeTabId = tab.id } );
    

    【讨论】:

    • @neaumusic 我对您的编辑有疑问。它修改了作者的代码有点过多,并且包含虚假信息(tabArray[0] 不是 ID)。我正在回滚。
    • @neaumusic 也就是说,您绝对可以发布您的代码作为答案。
    • 这似乎完全不起作用了。如果我在回调中执行console.log(activeTabId);(例如function(tab){ ... }),我可以看到 tab.id 已设置,但一旦 doInCurrentTab 返回,activeTabId 仍未定义。谷歌是否改变了一些打破这一点的东西?
    【解决方案2】:

    在后台页面运行它

    chrome.tabs.query({active:true,windowType:"normal", currentWindow: true},function(d){console.debug(d);})
    

    甚至更好

    chrome.tabs.query({active:true,windowType:"normal", currentWindow: true},function(d){console.debug(d[0].id);})
    

    【讨论】:

    • 这将获得所有窗口中的所有活动选项卡。因此,如果您打开了多个窗口,则会返回多个选项卡。
    • 供围观者使用 - 使用 currentWindow 编辑以解决 Soviut 的问题。
    • 请注意,仅在后台页面的 devtools 窗口中运行此代码不会返回任何内容。如果您在超时后运行它然后切换到常规窗口,您会看到它返回活动选项卡:setTimeout(() =&gt; chrome.tabs.query({ currentWindow: true, active: true }, console.log), 2000)
    【解决方案3】:

    许多 API 方法将 null 解释为当前选项卡。 chrome.tabs.sendRequest 就是其中之一。

    否则:

    chrome.tabs.getSelected(null, function(tab) { ... })
    

    【讨论】:

    【解决方案4】:

    如果你有tabs用户权限,查询方法是这样的:chrome.tabs.query


    getCurrentWindowActiveTabIndex().then(tabIndex => {
        // do something
    });
    
    // asnyc getter, not just a regular 'thunk'
    function getCurrentWindowActiveTabIndex () {
        return new Promise((resolve, reject) => {
            chrome.tabs.query({
                currentWindow: true,
                active: true,
            }, (currentWindowActiveTabs = []) => {
                if (!currentWindowActiveTabs.length) reject();
                resolve(currentWindowActiveTabs[0].index);
            });
        });
    }
    

    【讨论】:

    • 欢迎解释一下这段代码的作用,以及为什么这个答案比其他答案更好。
    猜你喜欢
    • 1970-01-01
    • 2011-06-17
    • 2012-11-08
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 2012-12-04
    相关资源
    最近更新 更多