Firefox WebExtension弹出窗口 - “点击”事件功能正常,但“卸载”无效(相反,“模糊”工作正常)

问题描述 投票:1回答:1

当我将“卸载”更改为“模糊”其工作时(不幸的是 - 即使我只从调用“滚动”功能的按钮中取走焦点)。我希望在用户关闭(点击外部)弹出窗口后执行“stopScrolling”。我也尝试了“beforeunload”事件,这也是不起作用的。以下是代码的残缺版本:

function scroll(tabs) {
    browser.tabs.sendMessage(tabs[0].id, {
        command: "scroll",
    })
}

function stopScrolling(tabs) {
    browser.tabs.sendMessage(tabs[0].id, {
        command: "stop",
    })
}

function reportError(error) {
    console.error(`Could not execute: ${error}`)
}


function init() {        
    window.addEventListener("unload", () => {
        browser.tabs.query({ active: true, currentWindow: true })
            .then(stopScrolling)
            .catch(reportError)
    })
    window.addEventListener("click", (e) => {
        browser.tabs.query({ active: true, currentWindow: true })
            .then(scroll)
            .catch(reportError)
    })
}

function reportExecuteScriptError(error) {
    document.querySelector("#popup-content").classList.add("hidden")
    document.querySelector("#not-received-msg").classList.remove("hidden")
    document.querySelector("#not-received-msg").textContent = "Script error!"
    console.error(`Failed to execute content script: ${error.message}`)
}

browser.tabs.executeScript({ file: "bcgscript.js" })
    .then(init)
    .catch(reportExecuteScriptError)
popupwindow firefox-webextensions dom-events
1个回答
1
投票

我提出了替代解决方案,这不是我想要的(如果有的话):

window.addEventListener("blur", (e) => {
    if(e.target == window)
        browser.tabs.query({ active: true, currentWindow: true })
            .then(stopScrolling)
            .catch(reportError)
})

我看到https://stackoverflow.com/users/8665598/alex-goico在这里应用“卸载”事件Firefox Extension - onPopupClose event?并说它有效。

© www.soinside.com 2019 - 2024. All rights reserved.