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

不羁岁月 提交于 2020-01-20 06:58:28

问题


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.


回答1:


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.


来源:https://stackoverflow.com/questions/13540636/is-it-possible-to-run-a-script-in-context-of-a-webpage-before-any-of-the-webpag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!