繁体   English   中英

如何确定Firefox WebExtensions中实际触发的最后一个tabs.onRemoved?

[英]How to know for sure last tabs.onRemoved actually fired in Firefox WebExtensions?

我正在使用WebExtensions API开发Firefox插件。 我在tabs.onRemoved项中收听tabs.onRemoved事件,需要在回调函数中执行某些操作。 通常情况下,它运作良好。

问题是 ,如果只有一个Firefox窗口,并且选项卡关闭导致窗口关闭,则Firefox似乎会在不触发tabs.onRemoved情况下退出。

我试过了:

  • 在Firefox的about:debugging页面上,开始调试我的附加组件,在tabs.onRemoved添加断点tabs.onRemoved回调函数,Firefox退出而没有遇到断点
  • tabs.onRemoved回调中向本地消息应用程序发送消息,本地应用程序端未收到消息
  • windows.onRemoved 如果仅剩一个窗口,则最后一个windows.onRemoved不会被触发

我认为这可能是Firefox错误。 因此,我在BugZilla上发布了一个错误( https://bugzilla.mozilla.org/show_bug.cgi?id=1342322#add_comment )。 但是Firefox家伙说这不是错误。

有什么方法可以确定tabs.onRemoved是否tabs.onRemoved被触发了吗?

我想确保我的回调函数在Firefox进程退出之前完成运行,该怎么做?

编辑:

这是我创建的最小可复制示例,它基于Mozilla官方示例tabs-tabs-tabs 我删除了不相关的部分:

的manifest.json

{
  "browser_action": {
    "browser_style": true,
    "default_title": "Tabs, tabs, tabs",
    "default_popup": "tabs.html"
  },
  "description": "A list of methods you can perform on a tab.",
  "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/tabs-tabs-tabs",
  "manifest_version": 2,
  "name": "Tabs, tabs, tabs",
  "permissions": [
    "tabs"
  ],
  "version": "1.0",
  "background": {
    "scripts": ["background.js"]
  }
}

tabs.js

document.addEventListener("click", function(e) {
  function callOnActiveTab(callback) {
    browser.tabs.query({currentWindow: true}).then((tabs) => {
      for (var tab of tabs) {
        if (tab.active) {
          callback(tab, tabs);
        }
      }
    });
  }

  if (e.target.id === "tabs-remove") {
    callOnActiveTab((tab) => {
      browser.tabs.remove(tab.id);
    });
  }

  e.preventDefault();
});

tabs.html

<!DOCTYPE html>
<html>
<body>
  <a href="#" id="tabs-remove">Remove active tab</a><br>
  <script src="tabs.js"></script>
</body>
</html>

background.js

//onRemoved listener. fired when tab is removed
browser.tabs.onRemoved.addListener(function(tabId, removeInfo){
  console.log(`The tab with id: ${tabId}, is closing`);

  if(removeInfo.isWindowClosing) {
    console.log(`Its window is also closing.`);
  } else {
    console.log(`Its window is not closing`);
  }
});

我是Firefox的作​​者或ColorfulTabs。 这是我的处理方式:

browser.tabs.onRemoved.addListener(onRemoved);
onRemoved(tabId, removeInfo) {
            browser.tabs.remove(tabId, function () {
                browser.tabs.query({
                    currentWindow: true
                }, function (tabs) {
                    tabs = tabs.filter(tab => tab.id != tabId);
                    console.log(tabs);
                })
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM