0

I am working on a web script in Chrome with an extention. I found this:

"content_scripts": [{
    "js": [.....],
    "matches": ["http://*/*", "https://*/*"],
    "run_at": "document_start"
}]

In my Js script, there are just two lines of code:

window.test = 'test';
console.log(window.test);

And I tried to open my web page in this condition:

  1. Without opening the chrome devtool, I opened a new tab and go to my web page
  2. I checked the console.log in my code is displayed in my console. But, When I continue to enter window.test or test, i found the result of both of them are undefined.

P.S.

  1. I tried to change the 'document_start' to 'document_end', and these two global variables are defined, there is no bug.
  2. For the content scripts, I tried to write a new vide content script or with the content 'console.log...', but this way didn't work, so it is not the problem of the content of content script

I need to ask your ideas, if you want, you can try your chrome and your extension.

Leyla Lee
  • 466
  • 5
  • 19
  • 1
    Content scripts run in isolated world with no access to page variables or custom DOM properties you create in JS code. See [Building a Chrome Extension - Inject code in a page using a Content script](https://stackoverflow.com/q/9515704) Also, when using the devtools console UI make sure to choose the correct context above the console: `top` for the webpage, `` for the content script. – wOxxOm Sep 19 '16 at 16:26
  • @wOxxOm Thank you so much, it is very helpful ! I tried to study a lot of scipts, but nothing worrks, because I forgot the easiest thing, thank your for your mention ! – Leyla Lee Sep 20 '16 at 08:16

1 Answers1

1

Thank you for your answer! @wOxxOm As you said in the comment, I will make this as the answer to let others to prevent this point which is easy but very difficult to find.

When using the devtools console UI make sure to choose the correct context above the console: top for the webpage,'your extension' for the content script.

Leyla Lee
  • 466
  • 5
  • 19