0

選択したタブに挿入されたスクリプトに Chrome 拡張機能 (popup.html) からメッセージを送信する際に問題が発生しています。popup.html のコードは次のとおりです。

alert("sending MSG");
chrome.tabs.sendMessage(null, {greeting: "hello"}, function(response) {
console.log(response.farewell); 
});
alert("MSG send");

問題は、"sending MSG" アラートのみが表示され、2 番目のアラート "MSG sent" が表示されないことです。コードをブロックしているようです。

この関数を使用する場合でも:

chrome.tabs.getSelected(null, function(tab) {  
chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) { 
console.log(response.farewell); alert("MSG sent _ in");
                                         });
                        });
alert("MSG send _ out");

ここでも同じ問題が発生します。「MSG send _ out」は表示されますが、「MSG send _ in」は表示されません。誰かがこの問題について何か考えを持っているなら、私に知らせてください。

4

1 に答える 1

0

ポップアップインスペクターを見ると、ポップアップにランタイムエラーがあることがわかります。したがって、最後のエラーalertは機能していません。

解決策は簡単です。chrome.extension.sendMessage代わりにを使用する必要がありますchrome.tabs.sendMessage

alert("sending MSG");
chrome.extension.sendMessage(null, {greeting: "hello"}, function(response) {
    console.log(response.farewell); 
});
alert("MSG send");
于 2012-12-02T00:00:13.057 に答える