Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is chrome.runtime undefined in the content script?

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
like image 982
Tyler Biscoe Avatar asked May 29 '17 04:05

Tyler Biscoe


People also ask

How do I fix Chrome extensions not working?

Head to More tools > Extensions. Use the toggle for each extension to turn it off. Restart Chrome and go back to the extensions list. Re-enable the extensions.

What is content JS in Chrome extension?

Content scripts are files that run in the context of web pages. By using the standard Document Object Model (DOM), they are able to read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.

What is chrome Runtime API?

Description Use the chrome.runtime API to retrieve the background page, return details about the manifest, and listen for and respond to events in the app or extension lifecycle. You can also use this API to convert the relative path of URLs to fully-qualified URLs.

Is it possible to use content_script with chrome tabs?

chrome.tabs is only available in background scripts and popup scripts. If you wanna to use chrome.tabs then pass message from content_script to background script and play with chrome.tabs. Content scripts have only limited access to Chrome APIs. This access does not include chrome.tabs.

Can content scripts access the chrome API?

Content scripts have only limited access to Chrome APIs. This access does not include chrome.tabs. If you need to use chrome.tabs, you will have to do so in a background script1.

How do I get the URL of an extension using runtime content?

Content scripts can access Chrome APIs used by their parent extension by exchanging messages with the extension. They can also access the URL of an extension's file with chrome.runtime.getURL () and use the result the same as other URLs. var imgURL = chrome. runtime.getURL("images/myimage.png");


1 Answers

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.

like image 164
Tyler Biscoe Avatar answered Sep 30 '22 18:09

Tyler Biscoe