开发者

pass a variable into chrome.tabs.onUpdated.addListener() - Chrome extension

I need to access the tab.Id of a window that my Chrome extension has created.

Here's the code I'm using to create a window:

    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);
                 });
            });             
    });

And the code where I'd like to be able to access that 'ourWindow' variable we previously set:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
     if(changeInfo.status == "loading") {
        if(开发者_如何学运维tabId == ourWindow) {
            alert('Holy smokes, this is the window we created!');
        }
     }
});

I can't seem to access the variable, since it was created outside of the onUpdated.addListener. Any ideas?


Just move this var into global variable space:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜