Chrome 扩展程序在页面加载之前注入脚本
我正在第三方网站上开发 Chrome 应用程序。
我试图更改的文档具有以下页面格式:
<head>
.
.meta data here
.
</head>
<body>
<script type="text/javascript">
function a1(){
..contents of function a1
}
</script>
.
.other contents of body
.
<script type="text/javascript">
a1(); // <-- I don't want this to be executed
</script>
</body>
我的问题是我希望加载整个页面,除了函数调用 a1();
所以我想到了一个简单的解决方案:在函数定义之前即; function a1(){..},我想将 a1 创建为一个不执行任何操作的常量函数,因此使 a1() 调用无用。
问题是,如果我在js中将函数定义为常量,其中run_at document_start,执行环境是不同的,因此它不会影响页面。
因此,在同一执行环境中运行的替代方法是使用innerHTML+=“...”注入代码
另一种替代方法是使用“createElement”构造脚本元素,并且src是必须在执行之前加载的外部js文件。
这两种替代方案都不起作用,因为解析树不是在 run_at document_start 脚本中创建的。
所以,我想到了另一个解决方案。我添加了 DOMModifySubTree 事件的侦听器,希望改变解析序列(我知道,这听起来很有趣:))以便不调用该函数。没有帮助。
所以,我的问题是如何防止调用 a1() 函数? PS - 我无法联系第 3 方网站开发商。
I am developing a Chrome application on a 3rd party website.
The document that I am trying to alter has the following page format:
<head>
.
.meta data here
.
</head>
<body>
<script type="text/javascript">
function a1(){
..contents of function a1
}
</script>
.
.other contents of body
.
<script type="text/javascript">
a1(); // <-- I don't want this to be executed
</script>
</body>
My problem is I want the entire page to be loaded, except the function call a1();
So I thought of a simple solution: BEFORE the function definition ie; function a1(){..}, I want to create a1 as a CONSTANT function which does nothing, therefore rendering the a1() call useless.
Problem is that if I define the function to be constant in my js which run_at document_start, the execution environment is different, hence it wont affect the page.
So the alternate to run in the same execution environment is by INJECT the code using innerHTML+=" ... "
Another alternate is to construct a script element using "createElement" and the src is an external js file which has to be loaded before execution.
Both the alternatives does not work, as the parse tree isn't created in the run_at document_start script.
So, I thought of an other solution. I added the listeners for DOMModifySubTree event, hoping to alter the parse sequence(I know, it sounds funny :)) so that the function isn't called. Doesn't help.
So, my question is how do I prevent the call to a1() function??
P.S - I cannot contact the 3rd party website developer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Greasemonkey 就是您要找的。
它是一个 Firefox 扩展,可以在您选择的网页中注入您自己的脚本。
访问 wiki.greasespot.net 开始编写脚本。
幸运的是,Chrome 原生支持 Greasemonkey 脚本。有关详细信息,请参阅此页面。
Greasemonkey is what you're looking for.
It's a Firefox extension that injects your own scripts in webpages of your choice.
Visit wiki.greasespot.net to get started on writing scripts.
Fontunately, Chrome natively supports Greasemonkey scripts. See this page for more info.
抱歉回复晚了。我最初为跳过函数调用所做的事情是在文档开始时运行一个脚本并将其注入到“文档”中(因为 JS 在单独的环境中运行)。在注入的脚本中,我使用
这种方式,当再次声明该函数时,它不会被覆盖,因此该函数不会被执行(即,它正在执行我们的虚拟函数),从而基本上破坏了代码。 :)
Sorry for the late late reply. What I had originally done to skip the function call is that I ran a script at document start and injected it into the "document" (as JS runs in separate envi). In that injected script I used
This way, when the function is being declared again, it is NOT overwritten, hence the function is not executed (ie; it is executing our dummy function), thus essentially breaking the code. :)