在网页的任何脚本运行之前,是否可以在网页上下文中使用chrome扩展来运行脚本? [英] Is it possible to run a script in context of a webpage, before any of the webpage's scripts run, using a chrome extension?

查看:93
本文介绍了在网页的任何脚本运行之前,是否可以在网页上下文中使用chrome扩展来运行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过run_at:document_start,但是当扩展名获得控制权时没有DOM,所以我不能插入脚本转换为头部。此外,试图使用 DOMNodeInserted ,但它不会被解雇为头部。如果我尝试在第一个 DOMNodeInserted 事件中插入脚本,它会在网页的脚本之后执行。

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.

推荐答案

根元素始终存在。您可以将< script> 附加到< html> 元素:

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.

这篇关于在网页的任何脚本运行之前,是否可以在网页上下文中使用chrome扩展来运行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆