Chrome Extension sendMessage error from content script to background html -
i updated chrome extension json version 2, , trying extension work again. problem sendrequest depreciated along way. copy code https://developer.chrome.com/extensions/messaging.html script , modify own variable names, , doesn't work.
so go , put in original code , still doesn't work. have read multiple questions similar [and won't closed duplicate, because none of them same situation].
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 content script:" + sender.tab.url :                 "from extension");     if (request.greeting == "hello")       sendresponse({farewell: "goodbye"});   });     }); </script> </html>   script.js:
chrome.runtime.sendmessage({greeting: "hello"}, function(response) {   console.log(response.farewell); });   now run page [on craigslist], , go console , error:
port error: not establish connection. receiving end not exist. typeerror: cannot read property 'farewell' of undefined     @ chrome-extension://dhmjefbokfkjpdbigkadjpgjeflchgea/script.js:9:23   i use chrome beta on ubuntu 12.10 64-bit (google chrome: 27.0.1453.15 (official build 191758) beta)
you sending messages both background , content script, not trying receive them @ all. try listening messages in 1 or both of places. also, inline code against csp move external file.
for example:
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); });   also, chrome.tabs.getselected() has been deprecated well, use chrome.tabs.query() instead.
Comments
Post a Comment