I have a simple code i want to inject into a webpage using a chrome extension. When i paste the code into the devtools console it works just fine, but when i paste it inside my content script i keep getting errors like "cannot read property of undefined". What's the difference between running a script through the console and running a script through the content page? And is it possible to make the content page inject a script like the console does?
Asked
Active
Viewed 109 times
0
-
A you using content script? – sabithpocker May 24 '17 at 04:13
-
1Possible duplicate of [Insert code into the page context using a content script](https://stackoverflow.com/questions/9515704/insert-code-into-the-page-context-using-a-content-script) – sabithpocker May 24 '17 at 04:16
-
Content scripts executes their code in a isolated scope. So they cannot read and run js code created by the page. Take a look of sabithpocker comment. – Denis L May 24 '17 at 04:36
-
I've followed sabithpocker script and i still got some errors. The page i'm trying to inject the script in is using an external script, and i want to get a property from a object of this script. When i use the chrome's console it doesn't give me any error at all. But appending the script to the head element doesn't seem to work. When the console script is injected, is it "appended" somewhere? – Hebert AG May 24 '17 at 05:20
-
The linked topic should completely answer your question. If it doesn't you need to show us the code you're trying to run. It's likely the problem is timing: you need to delay your code via setTimeout, for example. – wOxxOm May 24 '17 at 06:05