10

I just updated my chrome extension to json version 2, and am trying to get my extension to work again. The problem is sendRequest was depreciated along the way. So I copy the code from https://developer.chrome.com/extensions/messaging.html into my script and modify it to my own variable names, and it doesn't work.

So then I go back and put in the original code and it still doesn't work. I have read multiple questions that are similar [and hopefully this won't get closed as a duplicate, because none of them were the same as my 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 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);
});

Now I run a page [on craigslist], and go to the Console and this is the error:

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

I use Chrome Beta on Ubuntu 12.10 64-bit (Google Chrome: 27.0.1453.15 (Official Build 191758) beta)

grexter89
  • 1,091
  • 10
  • 23
Matt
  • 2,790
  • 6
  • 24
  • 34
  • You need to move the inline code to an external file. This question has been asked before, and a detailed answer is provided. – Rob W Apr 08 '13 at 21:27
  • possible duplicate of [Port error while changing chrome extension from manifest v1 to v2](http://stackoverflow.com/questions/11913575/port-error-while-changing-chrome-extension-from-manifest-v1-to-v2) – Rob W Apr 08 '13 at 21:27

2 Answers2

32

You are sending messages from both your background and your content script, but not trying to receive them at all. Try listening for messages in one or both of those places. Also, inline code is against the CSP so move it all to an 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 as well, so use chrome.tabs.query() instead.

Community
  • 1
  • 1
BeardFist
  • 8,031
  • 3
  • 35
  • 40
  • Ok I have just edited my code in both background.html and script.js, but I STILL get the error! – Matt Apr 08 '13 at 22:38
  • I was going off Google's code. lol. I linked straight to it. Anyway no, let me try that... on second thought I'm not even sure how to make a background JS. Unless you mean content-script. I already have that. The problem, content-scripts can't access localStorage, hence using background HTML to access localStorage and send it across a message. – Matt Apr 08 '13 at 22:44
  • yay! That suggestion worked. can you add it to the original answer? then I will accept it – Matt Apr 08 '13 at 22:51
  • And to be clear, what made it work was making "back.js" and adding the js code in background.html of my current answer. Then I put src="back.js" in background.html Thanks again, this was really holding me up. – Matt Apr 08 '13 at 22:53
  • @Matt The change I suggested for your manifest file does that without the need to have a background.html file. – BeardFist Apr 08 '13 at 22:56
  • Ughhhhhhhh.. Ok so I just realized.. now, the code **works** for sending static messages. But in **back.js** I cannot access localStorage! So am back at square 1. Ugh this is so frustrating. Ok so apparently.. back.js has its own localStorage. It can store a value, but it cannot retreive the correct value stored in my popup.html file. – Matt Apr 08 '13 at 23:01
  • So basically.. I use a single dedicated background js file to do the localStorage. From my other js files I send it in a message, and that way I can successfully transmit the variable to anywhere in my extension. Thanks @BeardFirst for your help, not sure I would have gotten it without you. – Matt Apr 08 '13 at 23:50
0

background script

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

content script

chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
  if(request.action === 'action_name')
  {
    alert('handle event in the content script!!!!')
  }
});
Aleksandr Golovatyi
  • 1,033
  • 1
  • 11
  • 18