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.
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