【问题标题】:browser.runtime.sendMessage API send large data slowly on Firefox extensionbrowser.runtime.sendMessage API 在 Firefox 扩展上缓慢发送大数据
【发布时间】:2017-06-28 17:19:46
【问题描述】:

我使用 firefox 扩展(firefox 54-64 位)的 browser.runtime.sendMessage API 发送消息具有从 contentscript 到后台的大量二进制数据。

发送消息非常缓慢,根据 contentscript 发送的数据大小,Firefox 在 3-5 秒内没有响应。然后正常工作,后台接收消息。

在 chrome 上 sendMessage API 工作很快而且非常流畅。

如何在 Firefox 中修复它?

browser.runtime.sendMessage(tab.id, { name: "sendScreen", data: { screen: screen} })

对象中的屏幕值是二进制数据(长度约为 1135609 的数组)

在 background.js 我添加监听消息:

browser.runtime.onMessage.addListener (message, sender, sendResponse);

清单文件:

{
  "name": "abc",

  "background": {
    "page": "background.html"
  },
  "browser_action": {
    "default_icon": "icons/icon_19.png",
    "default_popup": "login.html",
  },
  "content_scripts": [
    {
      "web_accessible_resources": [


        "js/contentscripts/Browser.js",

      ],
      "js": [
        "js/contentscripts/ContentScript.js"
      ],
      "matches": [
        "file://*/*",
        "http://*/*",
        "https://*/*"
      ],
      "run_at": "document_end",
      "all_frames": true
    },
    {
      "js": [

        "js/contentscripts/Browser.js",
      ],
      "matches": [
        "file://*/*",
        "http://*/*",
        "https://*/*"
      ],
      "run_at": "document_start",
      "all_frames": true
    }
  ],
  "icons": {
    "16": "icons/icon_16.png",
    "19": "icons/icon_19.png"
  },
  "incognito": "spanning",
  "permissions": [
    "activeTab",
    "tabs",
    "http://*/*",
    "https://*/*",
    "<all_urls>"
  ],
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}

【问题讨论】:

  • 最近在该区域修复了一个非常重要的错误:bugzilla.mozilla.org/show_bug.cgi?id=1356546 虽然该修复仅在 Firefox 55 中,但您可以尝试 Beta 版或开发版或 Nightly 构建,看看是否有明显的区别?
  • 将数据分成多个部分并分别发送,发送之间有setTimeout()延迟。即使setTimeout() 中的延迟为 0 毫秒,它仍然允许 FF 在不同消息之间执行其他操作。虽然这不一定会加快整个过程,但应该可以防止 FF 冻结。
  • 您是在多进程模式下运行,还是在单进程模式下运行?请提供完整的minimal reproducible example 可用于复制问题。让 MCVE 只发送虚拟数据可能比包含构建实际数据所需的所有内容更容易。如果问题在FF55中没有解决,我们将需要足够的file a bug
  • 您可以尝试使用广播频道 api:developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API
  • 它在 Firefox 55 beta 上运行良好 :),如何在 FF54 中临时修复?看着FF55发布?谢谢。

标签: firefox firefox-addon firefox-addon-webextensions


【解决方案1】:

如果提到的bug 1356546 没有解决问题,那么file a new firefox bug

否则,您可以尝试按照 cmets 中所述拆分数据,这不会减少总体时间,但可能会减少对 UX 的影响,或者在他们修复 webextensions 错误之前不支持 Firefox。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 2010-10-26
    • 1970-01-01
    • 2011-01-05
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多