【问题标题】:Sending message from popup.js in Chrome extension to background.js从 Chrome 扩展中的 popup.js 向 background.js 发送消息
【发布时间】:2013-09-09 08:45:57
【问题描述】:

在 Chrome 扩展程序中从 popup.js 向 background.js 发送消息(并获得响应)的正确方法是什么?我尝试的每种方法都会出现以下错误:

“端口:无法建立连接。接收端不存在。”

我更愿意使用 chrome.extension.sendMessage() 而不是 chrome.extension.connect() 和 port.postMessage(),但是这两种方法似乎都不起作用。

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

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

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

   $("#tabURL").text(tabURL);
}

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} );
    }
}

有什么想法吗?

【问题讨论】:

标签: google-chrome google-chrome-extension


【解决方案1】:

为了澄清,我们谈论的是 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

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

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

获取执行此脚本调用的选项卡。或许 如果从非选项卡上下文(例如:背景)调用,则未定义 页面或弹出视图)。

因此,您的所有请求都会未定义,因为您在后台页面中调用它。您需要做的是使用方法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} );
        });        
    }
}

popup.js

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

【讨论】:

  • 请注意,Chrome 扩展 API 似乎已弃用 chrome.extension.sendMessage 并将其替换为 chrome.runtime.sendMessage:developer.chrome.com/extensions/messaging
  • 它是如何工作的,反之亦然?从 background.js 到 popup.js?谢谢。 €dit:好的,明白了,它基本上完全一样,只是反过来..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-04
  • 2013-03-12
  • 2015-04-19
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多