本文介绍了Firefox扩展 - onPopupClose事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有弹出窗口,我需要以反应方式更新其中的数据。跟踪弹出窗口关闭时需要什么功能或事件?

I have popup in which I need to update the data inside it in reactively. What function or event is needed to track when a popup closes?

目前我的代码结构如下:

Currently my code structure is as follows:

let BG = browser.extension.getBackgroundPage();
let timer = BG.timer;

function updatePageTime(secondsElapsed) {
    const content = document.getElementById("content");
    content.innerHTML = "Current Time Spent: " + timer.getTimeElapsed();
}

document.addEventListener("DOMContentLoaded", function () {
    timer.subscribe(updatePageTime);
});

document.addEventListener("unload", function () {
    timer.unsubscribe(updatePageTime);
});

问题是卸载和beforeunload不会在弹出窗口关闭时触发,因此我无法停止对不存在的DOM的更新。

The problem is that unload and beforeunload do not trigger on popup closes, thus I am unable to stop updates to a non-existant DOM.

推荐答案

不幸的是我没有看到卸载是在窗口而不是文档上定义的。只需将 document.addEventListener(卸载更改为 window.addEventListener(卸载)即可解决问题。

Unfortunately I did not see that unload is defined on window and not document. Simply changing the document.addEventListener("unload" to window.addEventListener("unload" fixes the problem.

这篇关于Firefox扩展 - onPopupClose事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 01:26