How do inject a script into a website *before* the first script on this website is able to run?
Manuel Reimer
manuel.reimer at gmx.de
Tue Aug 15 16:00:13 UTC 2017
Hello,
my new code:
var script = document.createElement("script");
document.documentElement.appendChild(script);
var request = new XMLHttpRequest();
request.open('GET', browser.extension.getURL("pagescript.js"), false);
request.send(null);
script.textContent = request.responseText;
My code still runs too late. If I place code as <script> into <body> it
is still executed before my extension is able to implement the
javascript the page javascript is intended to use.
But: It actually works if I place my whole code directly inside my
content-script. I don't really like this. Seems like the XMLHttpRequest,
even if I tell it to be non-async, still causes stuff to get async
somehow. Is there any way, I can use from a WebExtension, to get content
of an extension-internal file in *really* async way? Would be so much
nicer to do some code-separation...
On 08/15/2017 05:26 PM, Makyen wrote:
> 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
> <https://stackoverflow.com/questions/38577656/how-can-i-make-a-firefox-add-on-contentscript-inject-and-run-a-script-before-oth>,
> which is specifically about this issue.
>
>
>
> On 8/15/2017 5:52 AM, Manuel Reimer wrote:
>> Hello,
>>
>> 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
>> to do heavy exchange with page javascript which is difficult or
>> 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
>>
>> Manuel
>> _______________________________________________
>> Dev-addons mailing list
>> Dev-addons at mozilla.org
>> https://mail.mozilla.org/listinfo/dev-addons
>>
>
>
>
>
> _______________________________________________
> Dev-addons mailing list
> Dev-addons at mozilla.org
> https://mail.mozilla.org/listinfo/dev-addons
>
More information about the Dev-addons
mailing list