基本的に、Chrome の拡張機能のドキュメントにあるコードを実行しようとしています。そして、それが私が立ち往生しているところです。
コンテンツ スクリプト >> バックグラウンド ページ (XHR 用) から値を渡そうとしています。
そして、コンソールに表示されるエラーは次のとおりです。
ポート エラー: 接続を確立できませんでした。受信側が存在しません。miscellaneous_bindings : 236
'undefined' のイベント ハンドラーのエラー: 未定義のプロパティ 'farewell' を読み取れません TypeError: 未定義のプロパティ 'farewell' を読み取ることができません :281:11 at chrome.Event.dispatchToListener (event_bindings:387:21) at chrome.Event.dispatch_ (event_bindings:373:27) at chrome.Event.dispatch (event_bindings:393:17) at Object.chromeHidden.Port. dispatchOnDisconnect (miscellaneous_bindings:239:27)
content_script.jsは次のようになります。
function func1(){
$(function() {
$('.genericStreamStory').each(function(){
var link = $(this).find('.uiStreamSource a').attr('href');
$(this).find('.uiStreamFooter').find('.a1').remove();
$(this).find('.uiStreamFooter').append("<span class='a1' style='color:red !important;'> · Sample</span>");
$(this).find('.a1').click(function(){
alert('hi');
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
console.log('testing');
}); //end of click handler
}); //end of 'each' function
});
}
func1();
window.setInterval("func1()", 1000);
メッセージ送信ステートメントchrome.extension.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
Chrome 拡張機能のドキュメント から取得されます。
background.js :
$(function() {
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
console.log("background");
console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});// end of onMessage listener
});
onMessage リスナーのコードは、Chrome のドキュメントから取得されます。
そして最後にmanifest.json :
{
"name": "Facebook123",
"version": "0.1",
"description": "Sample",
"content_scripts": [
{
"matches": ["https://*.facebook.com/*"],
"js": [
"/js/external/jquery.js",
"/js/content_script.js"
]
}
],
"background": {
"scripts": ["/js/background.js"]
},
"manifest_version": 2
}
conrent_script.js によって送信されたメッセージを処理するために、リスナーが「利用可能」ではないようです。
content_script.js の Click Handler が機能しています。スパン「a1」をクリックすると、「hi」アラート ボックスがポップアップし、「testing」というメッセージがコンソールに出力されます。
エラーは何ですか?