我知道它已经在这里像千次,但我现在卡住了。我读了很多答案,并研究了code.google.com,但没有成功。我正试图发送一个从background.html
到contentscript.js
的Chrome扩展请求。尽管我设法让它以另一种方式工作。卡住消息传递background.html >> contentscript.js(扩展名为Chrome)
代码中background.html
:
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
代码中contentscript.js
:
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
else
sendResponse({farewell: "nope"});
});
作为通信正在向后manifest.json
应该罚款和其他任何工作正常。谢谢!
是contentscript收到的消息? – 2012-01-13 23:44:02
不,即使我添加'alert(“嘿”);'在函数内部(request,sender,sendResponse){}'没有任何反应。 – 2012-01-13 23:53:06