将变量传递给chrome.tabs.onUpdated.addListener() - Chrome扩展程序

时间:2011-09-28 13:06:38

标签: javascript google-chrome-extension global-variables

我需要访问我的Chrome扩展程序已创建的窗口的标签。

这是我用来创建窗口的代码:

    chrome.windows.create({
        url: fullUrl,
        width: w,
        height: h,
        type: 'normal'
        }, function() {
            chrome.windows.getCurrent(function(window) {
                chrome.tabs.getSelected(window.id,
                function (response){
                    var ourWindow = response.id
                    alert('created a window with a tab id of: ' + ourWindow);
                 });
            });             
    });

我希望能够访问我们之前设置的'ourWindow'变量的代码:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
     if(changeInfo.status == "loading") {
        if(tabId == ourWindow) {
            alert('Holy smokes, this is the window we created!');
        }
     }
});

我似乎无法访问该变量,因为它是在onUpdated.addListener之外创建的。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

将此var移动到全局变量空间:

var ourWindow = null;
...
chrome.tabs.getSelected(window.id,
function (response){
    ourWindow = response.id;
});