【问题标题】:Chrome extension: Port: Could not establish connectionChrome 扩展程序:端口:无法建立连接
【发布时间】:2013-08-27 02:03:27
【问题描述】:

我的清单:

{
    "name":"name",
    "version":"0.1",
    "manifest_version":2,
    "description":"name app",
    "background":{
        "scripts":[
            "scripts/modernizr.min.js", 
            "scripts/background.js"
            ],
        "persistent": false
    },
    "content_scripts": [
      {
        "matches": ["https://*/*", "http://*/*"],
        "js": ["scripts/content.js"],
        "run_at": "document_end"
      }
    ],
    "permissions":[
        "contextMenus", 
        "tabs",
        "http://*/*",
        "https://*/*"
        ],
    "icons":{
        "16":"images/icon_16.png",
        "128":"images/icon_128.png"
    }
}

Background.js 有一个函数叫做:

    chrome.contextMenus.onClicked.addListener( function( info, tab )
        {
... stuff ...
    chrome.tabs.sendMessage( tab.id, { action: "showLibraryUI", library: library }, function( response ){} );

    } );

在 content.js 中,我有一个监听函数,但它从未被调用:

chrome.runtime.onMessage.addListener( function( request, sender, sendResponse )
    {
...stuff...
} );

在 _generated_background_page.html 中我看到了这个扩展:

端口:无法建立连接。接收端不存在。 上次错误:29 设置最后一个错误:29 dispatchOnDisconnect miscellaneous_bindings:259

我在 SO 上的其他帖子中找不到清单 > 2.0 的解决方案。

【问题讨论】:

  • 代码看起来不错。您确定代码的chrome.runtime.onMessage 位运行了吗?
  • background.js 中的一切都运行良好。在 background.js 中我不确定 chrome.tabs.sendMessage( tab.id, { action: "showLibraryUI", library: library } 是否正常工作,因为 content.js 中的监听器没有被调用。
  • @RobW 我找到了解决方案:)
  • @RobW 另外,我看到有人建议您在后台添加一个侦听器并使用内容脚本发送消息,以便您在任何消息传递发生之前始终知道内容已准备好。你怎么看待这件事?如果您的消息传递依赖于 background.js 中发生的事件,我认为它不会起作用。

标签: javascript google-chrome-extension message-passing


【解决方案1】:

我找到了解决问题的方法。我的 content.js 中有语法错误。这导致 content.js 没有运行,并且监听器不存在。

【讨论】:

    猜你喜欢
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    相关资源
    最近更新 更多