無法弄清楚什麼是錯的。在異步回調方法調用時,onRequest不會觸發,但內容腳本中的相同請求有效。下面的示例代碼。來自異步回調的Chrome擴展程序sendRequest無法正常工作?
background.js
=============
...
makeAsyncRequest();
...
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
switch (request.id) {
case "from_content_script":
// This works
console.log("from_content_script");
sendResponse({}); // clean up
break;
case "from_async":
// Not working!
console.log("from_async");
sendResponse({}); // clean up
break;
}
});
methods.js
==========
makeAsyncRequest = function() {
...
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
...
// It works
console.log("makeAsyncRequest callback");
chrome.extension.sendRequest({id: "from_async"}, function(response) { });
}
}
...
};
UPDATE:清單配置文件。不要在這裏有什麼問題。
{
"name": "TestExt",
"version": "0.0.1",
"icons": {
"48": "img/icon-48-green.gif"
},
"description": "write it later",
"background_page": "background.html",
"options_page": "options.html",
"browser_action": {
"default_title": "TestExt",
"default_icon": "img/icon-48-green.gif"
},
"permissions": [
"tabs", "http://*/*", "https://*/*", "file://*/*",
"webNavigation"
]
}
'methods.js'放置在哪裏?你能顯示你的所有代碼,包括'manifest.json'嗎? – 2012-03-18 09:40:40
添加了'manifest.json',但您仍未顯示如何執行'methods.js'。它位於哪裏?你可以擴大'...'嗎? – 2012-03-19 13:47:13