是否可以在使用chrome扩展程序运行任何网页脚本之前,在网页上下文中运行脚本?

时间:2012-11-24 11:11:20

标签: google-chrome-extension content-script

我尝试了"run_at": "document_start",但是当扩展程序获得控制权时没有DOM,因此我无法将script插入head。此外,尝试使用DOMNodeInserted,但head没有触发它。如果我尝试在第一个script事件上插入DOMNodeInserted,则会在网页脚本之后执行。

1 个答案:

答案 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.