简体   繁体   中英

Chrome extension inject script before page load

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?? PS - I cannot contact the 3rd party website developer.

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.

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

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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