Firefox Extension - onPopupClose event?

一曲冷凌霜 提交于 2019-12-01 07:29:58

问题


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);
});

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.


回答1:


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.



来源:https://stackoverflow.com/questions/51047924/firefox-extension-onpopupclose-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!