14

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
7
  • In the content script, runtime is undefined - what makes you think that? – Jaromanda X May 29 '17 at 4: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 – Jaromanda X May 29 '17 at 4:38
  • Here's how I know it's undefined: imgur.com/a/Ed9TZ – Tyler Biscoe May 29 '17 at 4:40
  • Also, the problem is in the content script, not the background script. – Tyler Biscoe May 29 '17 at 4:40
  • read this page - I'm not sure why you are getting the error you get though – Jaromanda X May 29 '17 at 5:03
24

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.

5
  • 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
  • 11
    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 developer.chrome.com/extensions/messaging#external-webpage – Matt Habermehl Oct 22 '19 at 21:10
  • 1
    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. – Jurik Feb 6 '20 at 16:25
  • still the issue can anyone help me how I can close my devtools in edge – Arun Apr 20 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.