Skip to content Skip to sidebar Skip to footer

Chrome Extension Run At Doc Start

I have a script that I want to be injected into the document BEFORE load. I.E; it should function as if MYSCRIPT HERE . . I have made

Solution 1:

You should be able to inject script into the page's JavaScript context by adding a script tag via your content script. In other words, your script could be injected at document_idle, and execute something like:

var s = document.createElement('script');
s.textContent = 'const a = function () {};';
document.documentElement.appendChild(s);

That script tag would be executed in the context of the page, not in the context of your script, and should allow you to achieve the result you're looking for.

Documentation for content scripts in general is available at https://developer.chrome.com/extensions/content_scripts


Solution 2:

Chrome probably has a bug in the current build as PAEz pointed out. But yes, a lot of insight on how things can be injected into "document" using chrome extension. Thanks Mike for the post. Will keep that in mind the next time I'm injecting using greesemonkey or something :)


Post a Comment for "Chrome Extension Run At Doc Start"