Chrome扩展:端口:无法建立连接

Chrome extension: Port: Could not establish connection

本文关键字:建立 连接 端口 扩展 Chrome      更新时间:2023-09-26

我的清单:

{
    "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中,我看到了以下扩展:

端口:无法建立连接。接收端不存在。lastError:29设置lastError:29dispatchOnDisconnect杂项标记:259

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

我找到了问题的解决方案。我的content.js中出现语法错误。这导致content.js无法运行,侦听器也不存在。