何が間違っているのか理解できません。非同期コールバックメソッドからの呼び出しでonRequestがトリガされない場合、コンテンツスクリプトからの同じ要求が機能します。以下のサンプルコード。Chrome拡張機能asyncコールバックからの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'を含むあなたのコードをすべて表示できますか? –
'manifest.json'が追加されましたが、' methods.js'の実行方法はまだ示されていません。それはどこにありますか? '...'を展開できますか? –