停止缓存邮件的Safari扩展
问题描述:
我在创建一直让我头痛的Safari浏览器扩展时遇到了问题。停止缓存邮件的Safari扩展
问题是这样的:我创建了一个扩展,使用注入的脚本从网页获取图像。我在显示网页图像的弹出窗口中有一个函数,并允许用户单击并将选定的图像发送到后端。这一切都经历处理信号和消息的全球页面。场景是这样的:
- 当弹出窗口打开时,它向全局页面发送一个信号,以从注入的脚本启动响应(图像URL)。
- 当全局页面从注入脚本获取消息时,它会调用popover中的函数,将来自注入脚本的数据作为参数传入。
- 弹出窗口显示通过注入脚本从全局页面返回的所有图像。
问题是,我每次打开popover时,都会追加上次调用的图像,而不是给出新的图像列表。例如,打开第一个弹出窗口时,我会看到一个图像(假设页面只有一个图像)。如果我关闭popover并再次打开它,我会得到两个相同的图像。如果我第三次关闭并打开弹出窗口,它会从第二次将第一张图像与第一张图像相加,并给我3张相同的图像。在popover的第四次打开时,我得到1 + 1 + 1 + 1,所以4张图像。所以它似乎在追加信息,而不是每次都给我一个新的信息。
我的问题是:如何销毁每个弹窗关闭后缓存的消息?我希望我清楚。也许还有其他事情正在发生在我不知道的代码上。如果可以的话请帮忙。这里是我从全球HTML代码:
function popoverHandler(event) {
//check for popover opening
if (event.target.identifier === "MyPopUp") {
//send message to injected script to send page info
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("getContent", '', false);
//this works fine, I get this message every time popover opens
console.log('getContent message sent');
//listen for message containing page info from injected script
safari.application.addEventListener('message', function (messageEvent) {
//only get message from current tab
if (messageEvent.name === "pageInfo" && messageEvent.message.url === safari.application.activeBrowserWindow.activeTab.url) {
pageInfo = messageEvent.message;
//the problem seems to be in here. Every time I open the popover, //I get the current page info plus all the page info messages from //the previous time I open the pover, all duplicates of the previous //messges
console.log(pageInfo);
// call a function in the popover, passing the pageInfo data //received from the injected script
safari.extension.popovers[0].contentWindow.onPageDetailsReceived(pageInfo);
}
});
}
}
答
好吧,所以我能够解决这个问题。首先,我将popoverHandler从eventListener中分离出来。出于某种原因,它多次触发该功能并返回几张相同图像的列表。然而,主要问题是在popover.js中,我将图像列表存储为var。当我删除var时,数据停止了,并且每次都得到一个新的列表。
好的,所以我能够解决这个问题。 – mcinteemasud