Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

can't access dom at content_scripts files #512

Closed
HomyeeKing opened this issue Mar 9, 2021 · 4 comments
Closed

can't access dom at content_scripts files #512

HomyeeKing opened this issue Mar 9, 2021 · 4 comments
Labels
bug Something on the site is broken!

Comments

@HomyeeKing
Copy link

excute console.log(document.body)
image

but we can access the document 🤷

excute console.log(document)
image

version: ALL NEW

@HomyeeKing HomyeeKing added the bug Something on the site is broken! label Mar 9, 2021
@tophf
Copy link

tophf commented Mar 9, 2021

This repository is about the documentation, not about code support.

@HomyeeKing
Copy link
Author

but the document says that we can access DOM in content_scripts and the fact is not as expected
I think I need an explaination from the official member

@tophf
Copy link

tophf commented Mar 10, 2021

Content scripts can access DOM and they do it in lots of extensions, including the ones I write. Your code is probably incorrect, for example you've declared the script with "run_at": "document_start" so it runs before anything is added to DOM.

I guess the documentation could elaborate on document_start. Currently it says:

Scripts are injected after any files from css, but before any other DOM is constructed or any other script is run

Many people arguably may not know what "DOM" or "DOM is constructed" means so the documentation could say:

Scripts are injected after any files from css, but before any other DOM is constructed or any other script is run. It means that only document.documentElement exists for the root node of the page (it maps to <html> in an HTML document) but there's still no document.head nor document.body. In such cases the typical solution is to use MutationObserver, periodically check DOM inside setInterval, or listen to events.

@dotproto, mentioning you so this is not lost: something to consider when you upgrade this documentation article.

@HomyeeKing
Copy link
Author

thanks a lot!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something on the site is broken!
Projects
None yet
Development

No branches or pull requests

2 participants