在' background.js'之间传递的简单信息to' contentScript.js'

时间:2014-10-10 09:30:00

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

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

按下时{p>绑定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

获取值

我应该改变什么?

2 个答案:

答案 0 :(得分:5)

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

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

我希望这能澄清事情,让你开始朝着正确的方向前进。

答案 1 :(得分:1)

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