gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:28:37 24 4
gpt4 key购买 nike

我需要访问 tab.Id 我的 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 移动到全局变量空间:

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

关于javascript - 将变量传递给 chrome.tabs.onUpdated.addListener() - Chrome 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7583720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com