chrome.windows.getCurrent未返回已打开选项卡的列表

时间:2011-12-09 20:15:48

标签: javascript google-chrome-extension

var windows = chrome.windows.getCurrent(
    function(windows){
        try{
            // dont really know why this is null. it should be a list of tabs.
            if(windows.tabs == null) 
        alert(windows.type + " " + windows.id);
        }
        catch(e){
            alert(e);
        }
    });

我正在使用此代码获取当前窗口中的所有打开选项卡。但是,即使在当前窗口中打开了选项卡,window.tabs也始终为null。当前窗口的概念是否有问题。 任何人都可以解释一下我做错了什么。 感谢。

1 个答案:

答案 0 :(得分:5)

看起来传递给回调的windows对象没有tabs字段。请尝试使用此代码:

chrome.windows.getCurrent(function(win)
{
    chrome.tabs.getAllInWindow(win.id, function(tabs)
    {
        // Should output an array of tab objects to your dev console.
        console.debug(tabs);
    });
});

同时确保您拥有tabs权限。我也在后台页面上运行了这个,所以如果你没有在后台页面上运行它,你应该确保在你的上下文中有chrome.tabs

相关问题