1

For integration purpose I use javascript file from other provider. I add it to my site in next way

    var other = document.createElement('script');
    other.type = 'text/javascript'; 
    other.async = true;
    other.src = SOME_URL_ON_OTHER_DOMAIN;
    (document.getElementsByTagName('body')[0]).appendChild(other);

Now I would like to change it content. Could I do this myself on browser side without changing on javascript provider side? It would be fine it will be not cross browser solution.

Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
  • 3
    Is there anything specific that you want to change in its content? Maybe a variable value or a function's behavior? – LcSalazar Sep 09 '14 at 21:19
  • @LcSalazar I need to change content of some functions. Would be nice to work with it as with strings. – Stan Kurilin Sep 09 '14 at 21:20
  • 1
    No, you cannot change the content of an arbitrary script that you don't control. And you can access the content of the script on a different domain due to SOP. Monkeypatching the objects that the script created is possible, though (especially if they're global). – Bergi Sep 09 '14 at 21:21
  • You can redefine variables, functions etc, that exist in the remote script. For example if there's a function named "myFunction" you can do something like window["myFunction"] = function(){} – Mike Willis Sep 09 '14 at 21:22
  • For anyone coming across here https://stackoverflow.com/q/22141205/1214236 might also be relevant. – Alowaniak Jul 01 '23 at 10:33

1 Answers1

2

You'll have to watch and wait for the remote file to be loaded. Once it's loaded you can redefine functions. For example something like this should get you started:

redefineRemoteFunctions = setInterval( function() {
    if ( typeof window["remoteFunction"] == "function" ) {
        // looks like the remote library has been loaded, we can now redefine the functions
        window["remoteFunction"] = function( ) {
            // do whatever you want here
        };
        clearInterval(redefineRemoteFunctions);
    }
}, 100 );
Mike Willis
  • 1,493
  • 15
  • 30