actual
actual

Reputation: 2370

Is it possible to run a script in context of a webpage, before any of the webpage's scripts run, using a chrome extension?

I have tried "run_at": "document_start", but there is no DOM when the extension gets control, so I can not insert the script into head. Also, tried to use DOMNodeInserted, but it is not fired for head. if I try to insert the script on first DOMNodeInserted event, it gets executed after the webpage's scripts.

Upvotes: 3

Views: 235

Answers (1)

Rob W
Rob W

Reputation: 348972

The root element always exist. You can just append the <script> to the <html> element:

var s = document.createElement('script');
s.textContent = 'console.log("Test");';
document.documentElement.appendChild(s);
s.parentNode.removeChild(s); // Just to clean-up.

Upvotes: 4

Related Questions