Sending message from popup.js in Chrome extension to background.js

Dan*_*Epp 11 google-chrome google-chrome-extension

What is the proper way to send a message (and get a response) to background.js from popup.js in a Chrome extension? Every method I try ends up with an error that:

"Port: Could not establish connection. Receiving end does not exist."

I would prefer to use chrome.extension.sendMessage() over chrome.extension.connect() with port.postMessage(), but neither method seems to have worked.

我想要做的是在popup.html中连接一个按钮,调用popup.js中的一些javascript,调用back.js以获取有关topMost的currentTab()的信息(即:获取popup.html中显示的当前URL字符串)

现在在popup.js(连接到按钮的动作)我有:

function getURL()
{
   chrome.extension.sendMessage({greeting: "GetURL"}, 
          function(response) { tabURL = response.navURL });

   $("#tabURL").text(tabURL);
}
Run Code Online (Sandbox Code Playgroud)

在background.js我有:

chrome.extension.onMessage.addListener( function(request,sender,sendResponse)
{
    if( request.greeting == "GetURL" )
    {
        var tabURL = "Not set yet";
        chrome.tabs.getCurrent(function(tab){
            tabURL = tab.url;
        });

        sendResponse( {navURL:tabURL} );
    }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

jus*_*sio 25

只是为了澄清一下,我们谈论的是来自browserAction和后台脚本的弹出页面之间的通信?

无论如何,你的代码中有很多错误.

首先,你完全忽略了chrome api中的所有回调都是异步的这一事实.

在背景页面

    var tabURL = "Not set yet";
    chrome.tabs.getCurrent(function(tab){
        tabURL = tab.url;
    }); //this will be invoked somewhere in the future

    sendResponse( {navURL:tabURL} ); 
    //navUrl will be always Not set yet because callback of getCurrent hasn't been called yet
Run Code Online (Sandbox Code Playgroud)

在popup.js中也是如此

chrome.runtime.sendMessage({greeting: "GetURL"}, 
          function(response) { tabURL = response.navURL });//callback will be invoked somewhere in the future

$("#tabURL").text(tabURL)//tabURL will display something totally different from what you have been expected
Run Code Online (Sandbox Code Playgroud)

第二个错误是chrome.tabs.getCurrent没有提供用户在主窗口中选择的当前选项卡.该文件说:

获取此脚本调用的选项卡.如果从非选项卡上下文调用(例如:后台页面或弹出视图),则可能未定义.

因此,您将对所有请求进行未定义,因为您在后台页面中调用它.您需要做的是使用方法chrome.tabs.query 来获取当前活动的选项卡.

所以在修复所有问题之后,新代码看起来应该是这样的:

background.js

chrome.runtime.onMessage.addListener( function(request,sender,sendResponse)
{
    if( request.greeting === "GetURL" )
    {
        var tabURL = "Not set yet";
        chrome.tabs.query({active:true},function(tabs){
            if(tabs.length === 0) {
                sendResponse({});
                return;
            }
            tabURL = tabs[0].url;
            sendResponse( {navURL:tabURL} );
        });        
    }
}
Run Code Online (Sandbox Code Playgroud)

popup.js

function getURL() {
    chrome.runtime.sendMessage({greeting: "GetURL"},
        function (response) {
            tabURL = response.navURL;
            $("#tabURL").text(tabURL);
        });
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,Chrome扩展API看起来已弃用chrome.extension.sendMessage并将其替换为chrome.runtime.sendMessage:https://developer.chrome.com/extensions/messaging (8认同)