开发者

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

开发者 https://www.devze.com 2023-04-08 19:50 出处:网络
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:

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

精彩评论

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

关注公众号