contents.jsからbackground.jsにメッセージを渡す方法 / Chrome extension
contents.jsからbackground.jsにメッセージを渡す方法
contents.jsからbackground.jsにデータを渡す
background.js
待ち受けのListiner
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
switch (request.type) {
case "Message1":
if(request.text === "Message text"){
myFunction();
}
break;
default:
console.log("Error: Unkown request.")
console.log(request);
}
sendResponse({response: "listeher is successful"});
}
);
contents.js
chrome.runtime.sendMessage({
type: "Message1",
text: "Message text"
},
function (response) {
if (response) {
alert(response);
}
});
Unchecked runtime.lastError: The message port closed before a response was received.
chrome.runtime.sendMessageからメッセージを受け取った後に
sendResponseで値を返す必要があるが、値を返し忘れている場合に発生するトラブル。
大きなトラブルになった話は聞かないが、極力修正しておくべき内容。
大手メーカのextentionでも、このミスは結構あるようですが…。
関連
Tag : Java Script
Tag : JQuery
おすすめ記事
Unityでキャラクターを動かす(左右&前進加速)
Unchecked runtime.lastError: The message port closed before a response was received. / Chrome extension
to_excelでxlsx,xlsファイルを書き込む / Python Pandas
OBJファイルにPythonでテクスチャ貼りたいですか?
Bestな3D 開発プログラミング環境とは… OpenGL , DierctX , Unity …
サーバー以外の端末から接続する方法(開発環境版向け、本番環境以外向け) - Django
Supponsered
外部サイト
↓プログラムを学んでみたい場合、学習コースなどもおすすめです!