我尝试了"run_at": "document_start"
,但是当扩展程序获得控制权时没有DOM,因此我无法将script
插入head
。此外,尝试使用DOMNodeInserted
,但head
没有触发它。如果我尝试在第一个script
事件上插入DOMNodeInserted
,则会在网页脚本之后执行。
答案 0 :(得分:4)
根元素始终存在。您只需将<script>
附加到<html>
元素:
var s = document.createElement('script');
s.textContent = 'console.log("Test");';
document.documentElement.appendChild(s);
s.parentNode.removeChild(s); // Just to clean-up.