3

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.

dda
  • 6,030
  • 2
  • 25
  • 34
actual
  • 2,370
  • 1
  • 21
  • 32

1 Answers1

4

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.
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • There is no guarantee about the executing order of dynamic inserted script tag. So this method may fail in some time. – tsh Aug 07 '20 at 09:50