【问题标题】:Simple message passing between 'background.js' to 'contentScript.js''background.js' 到 'contentScript.js' 之间的简单消息传递
【发布时间】:2014-10-10 09:30:00
【问题描述】:

我正在尝试应用以下流程:

绑定background.js 中的键,当按下它们时: 从background.js -> contentScript.js发送消息 发送来自contentScript.js -> background.js的响应

这是menifit.json 的定义:

  "background" : {

    "scripts" : ["background.js"],
    "persistent" : true
    },
  "content_scripts": [
    {
      "matches": ["*://*/*"],      
      "js": ["contentScript.js"]
    }
    ],

绑定部分工作正常。

这是background.js中的代码:

    chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
        console.log(response.farewell);
        });
    });

这是contentScript.js中的代码:

chrome.runtime.onMessage.addListener(HandleMessage);

function HandleMessage(request, sender, sendResponse)
{
    if (request.greeting == "hello")
    {
        sendResponse({farewell: "goodbye"});        
    }
};

这是我得到的错误:

Error in event handler for (unknown): Cannot read property 'farewell' of undefined
Stack trace: TypeError: Cannot read property 'farewell' of undefined
    at chrome-extension://fejkdlpdejnjkmaeadiclinbijnjoeei/background.js:64:26
    at disconnectListener (extensions::messaging:338:9)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at EventImpl.dispatchToListener (extensions::event_bindings:397:22)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Event.publicClass.(anonymous function) [as dispatchToListener] (extensions::utils:93:26)
    at EventImpl.dispatch_ (extensions::event_bindings:379:35)
    at EventImpl.dispatch (extensions::event_bindings:403:17)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Event.publicClass.(anonymous function) [as dispatch] (extensions::utils:93:26) 

万一发生变化

console.log(response.farewell);

console.log("OK");

它工作正常,但那样我无法从contentScript.js 获取值

我应该改变什么?

【问题讨论】:

  • 应该编辑问题以说明它是关于 Chrome 扩展程序,而不是 Chrome 应用程序。我已经删除了 google-chrome-app 标签。

标签: javascript jquery google-chrome google-chrome-extension


【解决方案1】:

您正在尝试将消息从后台页面发送到内容脚本。从official documentation,您正在使用此代码 sn-p 从后台页面发送消息:

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
 console.log(response.farewell);
});

然而,文档清楚地提到上面提到的代码是发送一个消息内容脚本。这就是您可能会收到错误的原因:

Error in event handler for (unknown): Cannot read property 'farewell' of undefined
Stack trace: TypeError: Cannot read property 'farewell' of undefined

您想要实现的是从扩展程序(背景页面)向内容脚本发送请求,该请求需要您指定内容脚本所在的选项卡:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
     console.log(response.farewell);
  });
});

我希望这可以澄清事情并让您朝着正确的方向开始。

【讨论】:

  • 同样的事情,得到同样的错误。您能否指定“这需要您指定内容脚本所在的选项卡:”是什么意思? contentScript.js 仅在我的电脑上,不属于任何网页..
  • 内容脚本在打开选项卡的网页上运行。这只是指定您要将消息发送到哪个选项卡。
  • 好的,现在可以了。问题是该网页已经打开,必须关闭它并重新打开它..
【解决方案2】:

Chrome扩展中有3种发送消息的方式,可以参考Message Passing

通常使用一次性请求,例如:

在 background.js 中

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {  
  chrome.tabs.sendMessage(tabs[0].id, {message: "hello"}, function(response) {
    console.log(response);
  }); 
});

在 contentscripts.js 中

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  console.log(request.message);
  if (request.message == "hello") {
    sendResponse({farewell: "goodbye"});
  }
});

【讨论】:

  • 在这样做的时候,我在响应部分得到控制台日志的“未定义”..
  • @USer22999299 你必须在 contentscript 中的 sendResponse 使用监听函数的第三个参数,你可以试试上面的 demo
  • 虽然正确,但此答案并不能解释问题所在。因此,并不是很有用。与接受的答案进行比较。
猜你喜欢
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多