Why is chrome.runtime undefined in the content script?
Asked Answered
M

1

18

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
Mica answered 29/5, 2017 at 4:30 Comment(7)
In the content script, runtime is undefined - what makes you think that?Liken
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 docsLiken
Here's how I know it's undefined: imgur.com/a/Ed9TZMica
Also, the problem is in the content script, not the background script.Mica
read this page - I'm not sure why you are getting the error you get thoughLiken
Sounds like you're injecting code in the page, in which case it's no longer a content script but just a page script.Betwixt
I'm not injecting any code. What I've provided above is literally the entire application.Mica
M
28

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.

Mica answered 30/5, 2017 at 0:1 Comment(7)
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!!!!!!!!!!!!!!!!!Patsy
2 years later and this bug is still here, using chrome 75.0.3735.0Citystate
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 developer.chrome.com/extensions/messaging#external-webpagePanchito
I've this bug in a different way . 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.Grillo
still the issue can anyone help me how I can close my devtools in edgeImes
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?Dacy
Yet a year later and this just happened to me.Blacken

© 2022 - 2024 — McMap. All rights reserved.