2013-04-08 32 views
9

クロムエクステンションをjsonバージョン2にアップデートしましたが、エクステンションを再度動作させようとしています。問題は、sendRequestが途中で減価償却されたことです。だから私はhttps://developer.chrome.com/extensions/messaging.htmlから私のスクリプトにコードをコピーし、自分の変数名に変更し、それは動作しません。Chrome拡張機能sendMessageエラーがコンテンツスクリプトからバックグラウンドへhtml

私は元のコードに戻って入れますが、それでも動作しません。私は似ている複数の質問を読んでいる[そしてうまくいけば、彼らのどれも私の状況と同じではなかったので、これは重複として閉じられないだろう]。

manifest.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); 
}); 

今RU (:27.0.1453.15(公式ビルド191758)ベータ版Google Chromeの):[Craigslistの上]ページのNA、およびコンソールに移動し、これは誤りである

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ビットにクロームのベータ版を使用します

+0

あなたは外部ファイルにインラインコードを移動する必要があります。この質問は以前に尋ねられており、詳細な回答が提供されています。 –

+0

[マニフェストv1からv2へのクロム拡張を変更しているときにポートエラーが発生する可能性があります](http://stackoverflow.com/questions/11913575/port-error-while-changing-chrome-extension-from-manifest-v1-to- v2) –

答えて

30

あなたのバックグラウンドスクリプトとコンテンツスクリプトの両方からメッセージを送信していますが、受信しようとしていません。それらの場所の一方または両方のメッセージを聞いてみてください。また、インラインコードはagainst the CSPですので、すべてを外部ファイルに移動してください。例えば

manifest.jsonを

"background": { 
    "scripts": ["background.js"] 
}, 

background.js

chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){ 
    sendResponse({farewell:"goodbye"}); 
}); 

script.js

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

また、chrome.tabs.getSelected()も非推奨になっているので、代わりにchrome.tabs.query()を使用してください。

+0

私はbackground.htmlとスクリプトの両方で自分のコードを編集しました。jsが、私はまだエラーが発生します! – Matt

+0

私はGoogleのコードを外していました。笑。私はそれにまっすぐつながった。とにかく、私はそれを試してみましょう...私はバックグラウンドJSを作る方法がわからない、第二の考えで。あなたがコンテンツスクリプトを意味しない限り。私はすでにそれを持っています。 問題は、コンテンツスクリプトはlocalStorageにアクセスできないため、バックグラウンドHTMLを使用してlocalStorageにアクセスし、メッセージを介して送信します。 – Matt

+0

オハイオ州。オハイオ州私はそれを試みます。 – Matt

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!!!!') 
    } 
}); 
+0

tabs.getAllInWindowに対する応答でエラーが発生しました:エラー:引数1の値が無効です。 –

+0

@Olek、あなたのコードはbagsからmsgをviceversaの代わりにcontentscriptに送信しています – Pacerier

関連する問題