Closure handler gives TypeError: foo is not a function
Closure handler gives TypeError: foo is not a function
我正在做一个 Chrome 扩展,让人们可以在特定站点上使用上下文菜单,从中获取一些图像并将它们加载到新选项卡(html 模板文件在扩展文件夹)。
问题是我希望图像在选项卡关闭之前一直保留在选项卡上;所以不在 reloads.
不久前,我发现了一些可以实现这一点的代码,但我不断收到错误消息,指出我的变量/函数不是函数。非常感谢任何帮助。
在 magic(tabId);
为 'called' 的行上抛出错误。
这是 magic
和 pendingTabs
里面的内容
let pendingTabs = new Array();
chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo, tab) {
//Execute the handler
const magic = pendingTabs[tabId];
magic(tabId);
// Delete the handler
delete pendingTabs[`foobar`];
});
function sendImages(tabId, response, tabTitle) {
const urls = response.urls;
chrome.tabs.create({url: chrome.runtime.getURL('html/images.html')}, function(tab) {
function handler(tabId) {
chrome.tabs.sendMessage(tabId, {urls: urls});
};
pendingTabs[tab.id] = handler;
});
}
我需要在 magic() 上添加一个 if 来测试它是否未定义,然后错误奇迹般地消失了。
我正在做一个 Chrome 扩展,让人们可以在特定站点上使用上下文菜单,从中获取一些图像并将它们加载到新选项卡(html 模板文件在扩展文件夹)。
问题是我希望图像在选项卡关闭之前一直保留在选项卡上;所以不在 reloads.
不久前,我发现了一些可以实现这一点的代码,但我不断收到错误消息,指出我的变量/函数不是函数。非常感谢任何帮助。
在 magic(tabId);
为 'called' 的行上抛出错误。
这是 magic
和 pendingTabs
里面的内容
let pendingTabs = new Array();
chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo, tab) {
//Execute the handler
const magic = pendingTabs[tabId];
magic(tabId);
// Delete the handler
delete pendingTabs[`foobar`];
});
function sendImages(tabId, response, tabTitle) {
const urls = response.urls;
chrome.tabs.create({url: chrome.runtime.getURL('html/images.html')}, function(tab) {
function handler(tabId) {
chrome.tabs.sendMessage(tabId, {urls: urls});
};
pendingTabs[tab.id] = handler;
});
}
我需要在 magic() 上添加一个 if 来测试它是否未定义,然后错误奇迹般地消失了。