1

In my chrome's manifest file, I have a content_scripts declaration to call a js file on a certain site e.g.:

  "content_scripts": [
{
  "matches": ["https://example.com/*"],
  "js": ["jquery-1.11.3.js", "myjs.js"]
}

The matched site I am testing this extension on contains a javascript file which declares a global variable, var 'Foo'. I want to access this variable in my extension's own myjs.js file. However, when I attempt to reference the variable, I get an error in the console:

Uncaught ReferenceError: Foo is not defined 

I attempted to add a setTimeout of 2 seconds before using the variable, but it still produces the same error. If I open up Chrome's Developer tools (F12) Console and simply type the variable name 'Foo', it actually returns the variable without issues.

How can I get my chrome extension's js file to access the site's global js variable?

newwn
  • 11
  • 2
  • 2
    Possible duplicate of [Chrome extension - retrieving Gmail's original message](http://stackoverflow.com/questions/9602022/chrome-extension-retrieving-gmails-original-message) – rsanchez Dec 06 '15 at 14:16

1 Answers1

0

You can't access the variables defined by the web-page using content-scripts. Just follow the content scripts documentation here. Content scripts run in their own isolated environment, you can't even use the extension's variables and functions from the content script. The way you communicate with the extension from content script is through message passing. Please read the documentation carefully. I hope this helps.

Nikhil Sharma
  • 891
  • 1
  • 8
  • 11