10

Chrome拡張機能をjsonバージョン2に更新したばかりで、拡張機能を再び機能させようとしています。問題は、sendRequest が途中で減価償却されたことです。そのため、 https://developer.chrome.com/extensions/messaging.htmlからコードをスクリプトにコピー し、独自の変数名に変更しましたが、機能しません。

それで、戻って元のコードを挿入しましたが、それでも機能しません。類似した複数の質問を読みました [私の状況と同じものはなかったので、これが重複してクローズされないことを願っています]。

マニフェスト.json:

{
   "background": {
        "page": "background.html"
        },
    ... ... ...
   "content_scripts": [ {
      "css": [ "style.css" ],
      "js": [ "jq.js", "script.js" ],
      "matches": [ "http://*.craigslist.org/*/*.htm*" ]
   } ],
   ... ... ...
   "permissions": [ "tabs", "http://*.craigslist.org/*/*.htm*" ],
   "manifest_version": 2,
   "update_url": "http://clients2.google.com/service/update2/crx",
   "version": "3.0"
}

background.html:

<html>
<script type='text/javascript'>
   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"});
  });
    });
</script>
</html>

script.js:

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
  console.log(response.farewell);
});

[craigslist で] ページを実行し、コンソールに移動すると、次のエラーが表示されます。

Port error: Could not establish connection. Receiving end does not exist.
TypeError: Cannot read property 'farewell' of undefined
    at chrome-extension://dhmjefbokfkjpdbigkadjpgjeflchgea/script.js:9:23

Ubuntu 12.10 64 ビット(Google Chrome: 27.0.1453.15 (公式ビルド 191758) ベータ版)で Chrome ベータ版を使用しています。

4

2 に答える 2

0

バックグラウンド スクリプト

chrome.tabs.getAllInWindow(null, function(tabs) {
      $.each(tabs, function() {
        chrome.tabs.sendRequest(this.id, {"action":"action_name"} );
      });
    });

コンテンツスクリプト

chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
  if(request.action === 'action_name')
  {
    alert('handle event in the content script!!!!')
  }
});
于 2017-06-08T21:26:39.217 に答える