0

Google Chrome 拡張機能のメッセージ パッシングの問題:

この Chrome 拡張機能では

マイポップアップページ:

chrome.browserAction.onClicked.addListener(getMessage);
getMessage();

function getMessage()
{
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
            console.log(response.farewell);
        });//getting response from content script
    });
}

私のスクリプトページ:

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
    else
      sendResponse({}); 
  });

コンテンツ スクリプトから応答がありません。

編集:

@ serg に従って、コードをバックグラウンド ページに移動しました。しかし、それでも機能していません

4

1 に答える 1

4

chrome.browserAction.onClickedブラウザーのアクション ボタンにポップアップ ページが関連付けられている場合、リスナーを使用することはできません。リスナーは起動しません。

  • ポップアップを削除し、ボタンのみを残す
  • すべてを背景ページに移動します。
  • に置き換えtab.idますnull
  • この場合は何もしないので、最初の呼び出しを削除createFile();します (コンテンツ スクリプトはまだリッスンする準備ができていません)。
  • 拡張機能のデバッグにアラートを使用しないでくださいconsole.log()
于 2011-10-07T19:21:55.957 に答える