簡體   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