16

I have a very simple chrome extension, where I'm trying to pass a message from the background script to the content script. But chrome.runtime is undefined.

Here's literally all the code, as you can see there's almost nothing to it. In the content script, runtime is undefined.

Background Script:

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.runtime.sendMessage({action: 'someAction'}, 
    function(response) {
      console.log('Response: ', response);
    });
});

Content Script:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  sendResponse({
    user: 'user'
  });
});

manifest.json

{
  "manifest_version": 2,
  "name": "My Extension",
  "version": "1.0",

  "description": "Some stupid extension",

  "browser_action": {
    "default_icon": "icons/MyExtensionIcon.png"
  },
  "icons": {
    "48": "icons/MyExtensionIcon.png"
  },
  "permissions": [
    "tabs",
    "storage",
    "https://*/",
    "http://*/"
  ],
  "content_scripts": [
    {
      "matches": ["*://*.twitter.com/*", "https://twitter.com/*"],
      "js": ["js/content.js"]
    }
  ],
  "background": {
    "scripts": ["js/background.js"],
    "persistent": true
  },
  "web_accessible_resources": [
    "js/*",
    "css/*"
  ]
}

Other Info:

  • Chrome Version 58.0.3029.110 (64-bit)
  • Installing my extension as an "Unpacked extension" with developer mode
Tyler Biscoe
  • 2,322
  • 3
  • 20
  • 33
  • `In the content script, runtime is undefined` - what makes you think that? – Jaromanda X May 29 '17 at 04:33
  • I would think that in the background script, `chrome.runtime.sendMessage` would be `chrome.tabs.sendMessage` - because that's how one sends messages to content scripts [docs](https://developer.chrome.com/extensions/runtime#method-sendMessage) – Jaromanda X May 29 '17 at 04:38
  • Here's how I know it's undefined: http://imgur.com/a/Ed9TZ – Tyler Biscoe May 29 '17 at 04:40
  • Also, the problem is in the content script, not the background script. – Tyler Biscoe May 29 '17 at 04:40
  • read [this page](https://developer.chrome.com/extensions/messaging) - I'm not sure why you are getting the error you get though – Jaromanda X May 29 '17 at 05:03
  • Sounds like you're injecting code in the page, in which case it's no longer a content script but just a page script. – wOxxOm May 29 '17 at 06:44
  • I'm not injecting any code. What I've provided above is literally the entire application. – Tyler Biscoe May 29 '17 at 15:28

1 Answers1

27

Ok I figured it out. It's absolutely stupid, but it appears this is simply a Heisenbug. By adding a breakpoint, or debugger statement, it causes that value to be undefined. Maybe a chrome bug?

I swear, every day Chrome feels more, and more like Internet Explorer.

Tyler Biscoe
  • 2,322
  • 3
  • 20
  • 33
  • 1
    I found it too... I've lost like 8 working hours trying to solve this bug... but it appears like when I refresh page with DevTools (F12) being opened - the chrome.runtime is undefined... but when I close DevTools, refresh page, and open after page is loaded - the chrome.runtime works correctly... it's crazy!!!!!!!!!!!!!!!!! – Maciej Pszczolinski Jun 25 '17 at 18:16
  • 13
    2 years later and this bug is still here, using chrome 75.0.3735.0 – user2677034 Mar 19 '19 at 17:44
  • 2
    Still experiencing this bug in Chrome build 77.0.3865.120 where having dev tools open causes chrome.runtime to be undefined, but closing dev tools it works fine when using https://developer.chrome.com/extensions/messaging#external-webpage – Matt Habermehl Oct 22 '19 at 21:10
  • 1
    I've this bug in a [different way](https://stackoverflow.com/questions/60099122/why-does-chrome-runtime-does-not-work-on-local-ip-address) . When I have no dev tools open and no breakpoints set, this bug still appears, if you run your simple html page locally and open it with an ip address instead of localhost. – Jurik Feb 06 '20 at 16:25
  • still the issue can anyone help me how I can close my devtools in edge – Arun Apr 20 '21 at 23:25
  • Confirming this super weird behavior still exists! Chrome.runtime is undefined when devTools are open, and then reappears when devTools are closed. Could it be a security feature that disables when a developer opens the devTools so they can't post messages? – Sebastian Scholl Jan 10 '23 at 16:21