简体   繁体   English

Chrome扩展程序在页面加载之前注入脚本

[英]Chrome extension inject script before page load

I am developing a Chrome application on a 3rd party website. 我正在第三方网站上开发Chrome应用程序。

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(); 我的问题是我希望加载整个页面,但函数调用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. 函数a1(){..},我想将a1创建为不执行任何操作的CONSTANT函数,因此使a1()调用无效。

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. 问题是,如果我在run_at document_start的js中将函数定义为常量,则执行环境会有所不同,因此不会影响页面。

So the alternate to run in the same execution environment is by INJECT the code using innerHTML+=" ... " 因此,要在同一执行环境中运行的替代方法是使用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. 另一种选择是使用“ createElement”构造一个脚本元素,而src是一个外部js文件,必须在执行之前加载它。

Both the alternatives does not work, as the parse tree isn't created in the run_at document_start script. 这两种选择都不起作用,因为没有在run_at document_start脚本中创建解析树。

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. 我为DOMModifySubTree事件添加了侦听器,希望更改解析顺序(我知道,这听起来很有趣:)),以便不调用该函数。 Doesn't help. 无济于事。

So, my question is how do I prevent the call to a1() function?? 所以,我的问题是如何防止调用a1()函数? PS - I cannot contact the 3rd party website developer. PS-我无法联系第三方网站开发人员。

Greasemonkey is what you're looking for. Greasemonkey是您要找的东西。
It's a Firefox extension that injects your own scripts in webpages of your choice. 这是一个Firefox扩展程序, 可以在您选择的网页中插入自己的脚本
Visit wiki.greasespot.net to get started on writing scripts. 访问wiki.greasespot.net以开始编写脚本。

Fontunately, Chrome natively supports Greasemonkey scripts. 值得一提的是,Chrome本身支持Greasemonkey脚本。 See this page for more info. 请参阅此页面以获取更多信息。

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). 我最初跳过该函数调用所做的是,我在文档开始时运行了一个脚本,并将其注入到“文档”中(因为JS在单独的envi中运行)。 In that injected script I used 在我使用的注入脚本中

const a1=function(){};

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. 这样,当再次声明该函数时,该函数不会被覆盖,因此该函数不会执行(即,它正在执行我们的伪函数),从而实质上破坏了代码。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM