我知道它已經在這裏像千次,但我現在卡住了。我讀了很多答案,並研究了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