0

Here is my problem: I am making an extension for an existing website.

the site loads two javascript files when the page is loaded:

foo.js
bar.js

foo.js defines variable X and assigns it a value of 1.

bar.js then goes on to reference variable X, and uses that value in it's own script.

I want to assign my own value to variable X.

My problem is that the extension can only run at document_start or document_end. if I try to change the value of variable X by running the script at document_start, the site loads foo.js and overwrites my variable back to 1 when it's loading all of the site resources. If I change variable X in document_end it won't help because bar.js already used the value from variable X way before my extension was executed.

Suppose I ran my extension at document_start. If I could allow foo.js to load, but STOP bar.js from loading, assign my custom value to variable X, AND ONLY THEN request bar.js again and have it excecute it's code, my problem would be solved. Is this possible?

Omega P
  • 197
  • 1
  • 20
user2936448
  • 335
  • 4
  • 16
  • Why can't you modify either one of the js files? – Charles McKelvey Aug 23 '16 at 02:36
  • ... because I do not own the website. I am making an extension which can only change the page after the fact. The files have to do with audio that is being played on the page. The audio elements are defined strictly in javascript, so I can't select them as if they were an HTML element. They are defined in an anonymous function where I have no access to the variables whatsoever – user2936448 Aug 23 '16 at 02:39
  • Seems like promise comes into the place to work with. – Rafi Ud Daula Refat Aug 23 '16 at 02:40
  • Do you want your script run after foo.js and bar,js was loaded? – Omega P Aug 23 '16 at 02:41
  • @OmegaP please read the post. – user2936448 Aug 23 '16 at 02:45
  • Have you tried setting the second script tags to defer, you may be able to poll the value of x and get it before the second script runs – Blindman67 Aug 23 '16 at 02:54
  • So this is a chrome extension? Then you have a bigger problem: extension code can not access javascript objects that belong to webpage scripts: https://developer.chrome.com/extensions/content_scripts#execution-environment – artem Aug 23 '16 at 02:57
  • Try `$.holdReady()`, document [at here](https://api.jquery.com/jquery.holdready/) – Omega P Aug 23 '16 at 02:59
  • @artem That is not a problem. I am able to run my own js in the scope of the page by using document.createElement('script') and assigning its textContent in the scope of my chrome extension. Once I append it to the page, the JavaScript is executed in the scope of the webpage. This is not the issue. – user2936448 Aug 23 '16 at 03:00
  • Another way to stop code is to force it to throw an error by changing something it assumes can not change. Then you can run it at your leisure when what ever you need done is done. – Blindman67 Aug 23 '16 at 03:10
  • 2
    if foo and bar are also loaded by script tags, you could add your script on document_start, watch for DOM mutations that insert script tags for foo and bar, attach your onload event handler on these tags http://stackoverflow.com/questions/8882502/how-to-track-dom-change-in-chrome-extension – artem Aug 23 '16 at 03:13
  • 1
    Is it possible to add your script file in between foo.js reference and bar.js reference in document_start ? – Jithu Rajan Aug 23 '16 at 03:23

0 Answers0