最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to overwrite a function in the web page with Chrome extension? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Feb 19, 2012 at 7:01 wong2 asked Feb 19, 2012 at 6:55 wong2wong2 35.8k51 gold badges137 silver badges182 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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:

document.head.innerHTML += '<script>Q.foo = function(){alert("over written");}</script>';

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.

Q.prototype.foo = function(){
  alert("over written");
}
发布评论

评论列表(0)

  1. 暂无评论