Skip to content Skip to sidebar Skip to footer

Chrome Extensions: Variable Undefined In Chrome Extension Even Though It Exists In Console

In my content scripts for my chrome extension, I dynamically load a external js file onto a html page once it is finished loading. This js file I load will define a variable called

Solution 1:

Isolated world problem.

Content scripts execute in a special environment called an isolated world. They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page. It looks to each content script as if there is no other JavaScript executing on the page it is running on. The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts.

Your content script will never see the variables defined by the page. And in the console you're (by default) looking at the page's context, not content script's context. You can switch that (in the <top frame> dropdown) to test.

You need to inject a page-level script to do anything with the page's own JS.

Post a Comment for "Chrome Extensions: Variable Undefined In Chrome Extension Even Though It Exists In Console"