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

int*_*ild 3 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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

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

Jim*_*zuk 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);
    });
});
Run Code Online (Sandbox Code Playgroud)

还要确保您拥有该tabs权限.我也在后台页面上运行它,所以如果你没有在后台页面上运行它,你应该确保chrome.tabs在你的上下文中可用.