3

script/contentscript.js

'use strict';

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
     alert("hi");
     return true;
});

chrome.runtime.sendMessage(null, {hello:"hello"});

I'm at a loss. Either addListener isn't registering the callback, the callback isn't being fired when a message is sent, or something in between. I've done what I can to limit the possible errors. At one point the sendMessage was in my popup.js and after hours of trying different variations, I ported it to another computer. When I got desperate enough to put the sendMessage in the same file it was a surprise to find sendMessage wouldn't work even in the same contentscript!

I've used both runtime and tabs objects as well as every variation of both sendMessage and onMessage.addListener.

One odd thing is that when on a breakpoint in the content script, I've seen hasListener usually return false and hasListeners return true after my listener is added.

{
  "name": "__MSG_appName__",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "__MSG_appDescription__",
  "icons": {
  "16": "images/icon-16.png",
  "128": "images/icon-128.png"
},
  "default_locale": "en",
  "content_scripts":[
{
   "matches": ["http://*/*", "https://*/*"],
   "js":["scripts/contentscript.js"]
}
],
   "browser_action": {
   "default_icon": {
   "19": "images/icon-19.png",
   "38": "images/icon-38.png"
 },
   "default_title": "pluralsight dl",
   "default_popup": "popup.html"
},
   "options_page": "options.html",
   "permissions":[
   "activeTab",
   "tabs",
   "http://*/*",
   "https://*/*"
]
}

Any ideas on why this wouldn't work? Both computers are using Version 43.

A popup js message that also doesn't register. Same extension package.

script\popup.js

'use strict';

console.log('\'Allo \'Allo! Popup');
// File executed, it's ready for the message
chrome.runtime.sendMessage(null, { action: "start"});
user2397965
  • 83
  • 3
  • 7

1 Answers1

4

sendMessage never fires the event in the same context. Only other contexts receive the message.

As for your initial problem, there are 2 forms of sendMessage: runtime.sendMessage sends messages to extension's own pages (origin chrome-extension://yourId/) and tabs.sendMessage that sends messages to content scripts. See this question for more details.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • that definitely makes sense. I have a `popup.js` that also sends a message, I added the `sendMessage` to the contentscript only because this wasn't working originally for debug purposes. Using either tabs or runtime still doesn't fire my event. – user2397965 Jul 02 '15 at 19:21
  • It will not fire an event in the same page. – Xan Jul 02 '15 at 19:21
  • Sorry, I added a bit to the end of my question. I meant my popup.js will not fire the event either, regardless of using `chrome.runtime.sendMessage` or `chrome.tabs.sendMessage` – user2397965 Jul 02 '15 at 19:23
  • 1
    Could it be that you [reloaded the extension and did not reload the page with the content script](http://stackoverflow.com/a/23895822/934239)? – Xan Jul 02 '15 at 19:26
  • Nope, I've been refreshing constantly for debugging purposes. At one time I was using an executeScript function from the popup.js but reverted once I ran into these issues. – user2397965 Jul 02 '15 at 19:30
  • Um, why do you return true? That's actually a special thing in the context of this event listener, meaning "will reply later, wait". – Xan Jul 02 '15 at 19:31
  • I was trying to solve the initial issue and ran into some advice somewhere but it really didn't apply, wasn't like it was firing anyways. But I've got it working using `chrome.tabs.query` and `chrome.tabs.sendMessage` like you suggested. – user2397965 Jul 02 '15 at 19:35
  • One question though, at one point I had this working by using executeScript to open my "contentscript" and then send a message to it. At some point it just stopped working. Any clue what may have caused that? No worries if you don't. – user2397965 Jul 02 '15 at 19:36
  • Race condition? Everything is async, you should only try to send the message from the callback of `executeScript`. – Xan Jul 02 '15 at 19:37