Javascript 无法与内容脚本建立连接

Javascript 无法与内容脚本建立连接,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我不熟悉Chrome扩展,我尝试编写测试扩展,弹出页面和背景页面之间的消息按预期工作,但当我尝试从弹出页面向内容脚本发送消息时,总是出现错误“Unchecked runtime.lastError:无法建立连接。收件端不存在。”,这可能意味着没有注册的侦听器。我在弹出窗口和内容脚本文件中反复检查代码,但找不到哪里出错。谢谢你的帮助 以下是文件: manifest.json: { "manifest_version": 2, "name&q

我不熟悉Chrome扩展,我尝试编写测试扩展,弹出页面和背景页面之间的消息按预期工作,但当我尝试从弹出页面向内容脚本发送消息时,总是出现错误“Unchecked runtime.lastError:无法建立连接。收件端不存在。”,这可能意味着没有注册的侦听器。我在弹出窗口和内容脚本文件中反复检查代码,但找不到哪里出错。谢谢你的帮助

以下是文件:

manifest.json:

{
    "manifest_version": 2,
    
    "name": "Chrome extension test 01",
    "description": "test extension messaging",
    "version": "1.0.0.0",
    "permissions": [ "tabs", "activeTab", "https://*/*", "http://*/*"],
    
    "icons": {
        "128": "images/icon128.png"
    },

    "browser_action": {
        "default_title": "Here is the extension icon title",
        "default_popup": "popup.html",
        "default_icon": {
            "16": "images/icon16.png",
            "32": "images/icon32.png",
            "48": "images/icon48.png"
            }
        },  
        
    "content_scripts": 
        [ 
            {
            "matches":  ["<all_urls>"],  
            "js": ["content.js"]
            }
        ],

    "background": {
        "scripts": ["background.js"]
        }   

}
background.js

//background.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse){
        if(request.greeting=="hello"){
            sendResponse({data: "This is the message sent from the background."});
        }
        
    return true;         //send a response asynchronously, this will keep the message channel open to the other end until sendResponse is called.
    }
);
content.js

// content.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
    if(request.cmd=="GetSelectedText")
    {
        var selectedText=window.getSelection().toString().trim();
        if(selectedText&&selectedText.length)
        {
            console.log(selectedText);
            sendResponse({res: selectedText});
        }
    }
    
    return true;
}};

该错误意味着打开弹出窗口时内容脚本未运行,如果您在未重新加载选项卡的情况下重新加载扩展,或者选项卡仍在加载,则可能会发生这种情况。虽然这两个问题都有解决方法,但有一个更好的解决方案:编程注入()。它同时替换
content\u脚本
声明和消息。感谢您的消息,我尝试重新加载扩展并重新加载选项卡,但仍然出现错误。我将尝试在内容脚本中进行编程注入。我已经尝试了chrome.runtime.executeScript(),它可以工作。但是上面帖子中的代码不起作用,我找不到有什么问题。
//background.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse){
        if(request.greeting=="hello"){
            sendResponse({data: "This is the message sent from the background."});
        }
        
    return true;         //send a response asynchronously, this will keep the message channel open to the other end until sendResponse is called.
    }
);
// content.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
    if(request.cmd=="GetSelectedText")
    {
        var selectedText=window.getSelection().toString().trim();
        if(selectedText&&selectedText.length)
        {
            console.log(selectedText);
            sendResponse({res: selectedText});
        }
    }
    
    return true;
}};