How do inject a script into a website *before* the first script on this website is able to run?
wp.makyen at gmail.com
Tue Aug 15 15:26:13 UTC 2017
I'm not sure what you were doing prior to Firefox 55 which allowed you
to access an existing document.head when your content script was
injected at "document_start". It's normal, and has been such going back
to Firefox versions in the 40's (I don't remember where I first tested)
and Chrome before WebExtensions, that content scripts declared in
manifest.json using a content_scripts entry with "run_at" =
"document_start" are injected prior to the <head> and <body> being
inserted into the DOM (i.e. when document.head === null && document.body
=== null). This is normal, expected and desired behavior. Is this not
what you are experiencing? were experiencing?
If your intent is to /insert/ a script into the page context, that's
easily done at that time (i.e. at "document_start") by appending a
<script> element to the document.documentElement. That's the normal way
to do so at that time. If you are wanting to wait until the <head>
and/or <body> are created, then you can do so by using a
MutationObserver, as has already been mentioned. However, you need to
insert the code as text, not as a <script src="someFile">. If the
contents of the <script> is text, then the code is executed immediately.
If it's a <script src="someFile">, then it's non-blocking and the order
of execution is not guaranteed. Given that such referenced file is
sourced from within your extension (i.e. on local disk with no network
delay), it's likely that it will execute first, but it's not guaranteed.
For more detailed information, you can see this Stack Overflow question
which is specifically about this issue.
On 8/15/2017 5:52 AM, Manuel Reimer wrote:
> before Firefox 55 I was able to do the following:
> Register my content script to run at "document_start".
> Then I was able to access "document.head" from there and inject my
> <script> as the very first script to load.
> I need my script to run right in website context as my script is meant
> impossible with a content script directly.
> Since Firefox 55 it seems to be impossible to do so. "document_start"
> seems to fire before "<head>" even exists.
> I tried to fix this with the "DOMContentLoaded" but this now is too
> late. Scripts embedded into <body> already ran at this point...
> Then I tried a "MutationObserver" to maybe wait for the point when
> <head> is inserted, but for me the MutationObserver is never fired.
> Can someone please point me into the right direction?
> Thanks in advance
> Dev-addons mailing list
> Dev-addons at mozilla.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Dev-addons