コンテンツ スクリプトとバックグラウンド スクリプトを使用して Chrome プラグインを作成しており、この 2 つを通信させようとしています。
私のコンテンツスクリプトでは、私はやっています
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
そして私のバックグラウンドスクリプトでは、私はやっています
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
}
);
私のマニフェストは次のようになります。
{
"manifest_version": 2,
"name": "Tesing Phase",
"version": "1.0",
"background": {
"persistent": false,
"scripts": ["bgscript.js"]
},
"content_scripts": [{
"js": ["contentscript.js"],
"all_frames": true,
"run_at" : "document_start",
"matches": ["*://*/*"]
}],
"web_accessible_resources": ["script.js"]
}
プラグインを実行すると、次のエラーが表示されます。
Uncaught TypeError: Object #<Object> has no method 'sendMessage'
ロギングを試みchrome.runtime
ましたが、方法がありませんでしsendMessage
た。Ubuntu でバージョン 25.0 の Chromium を使用しています。私も使っsendRequest
てみたのですが、減価償却sendMessage
済みなので使うべきとのことでした。
ここで私が見逃していることを誰かが指摘できますか? これが機能するために必要な権限はありますか?