将变量传递给chrome.tabs.onUpdated.addListener() - Chrome扩展
问题描述:
我需要访问我的Chrome扩展创建的窗口的tab.Id。将变量传递给chrome.tabs.onUpdated.addListener() - 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之外创建的。有任何想法吗?
答
这个变种刚进入全局变量空间:
var ourWindow = null;
...
chrome.tabs.getSelected(window.id,
function (response){
ourWindow = response.id;
});
这个做到了SERG,谢谢! –