如何用Chrome扩展覆盖网页中的函数?
假设有一个网站有一个全局命名空间Q = {}
,并且它下面有一个函数:Q.foo
我想在我的chrome 扩展,因此当网页调用 Q.foo
时,它会执行我喜欢的操作。
我尝试写:
Q.foo = function(){
alert("over written");
}
带有内容脚本。但它不起作用......
谢谢。
Suppose there is a web site has a global namespace Q = {}
, and there is a function under it: Q.foo
I'd like to overwrite this function in my chrome extension, so when the web page calls Q.foo
, it would do what I like.
I tried to write:
Q.foo = function(){
alert("over written");
}
with content script. But it doesn't work....
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要问题是 Chrome 扩展存在于一个单独的环境中,该环境的创建是为了扩展开发人员无法破坏现有页面的 JavaScript,反之亦然。
然而,极客们可以这样做:
基本上,它的作用是将一个脚本标签附加到 dom 中,然后立即在页面的上下文中对其进行评估。
The main problem iis that a chrome extension exists in a separated enviornment which was created so that extension developers cant screw with the existing page's javascript and vice versa.
However geeky people can do this:
Basically what this does is that it appends a script tag into the dom which is then instantly eval'd in the context of the page.