Page 1 of 1

Changing the content of a JavaScript file before it runs.

Posted: Sat Oct 26, 2019 7:18 pm
by Paulo França
Hi.
I'm automating a web-based application that loads some JavaScript files from the server.
One of them I need to intercept as soon as it gets loaded, but before it runs (the JS code gets executed by the main document itself, not by my application).

I'd like to be able to retrieve, edit and replace the JavaScript doc entirely, as pure text.
Is there a way (without DOM Visitor) to do this? Perhaps an event?

Thanks in advance.

Re: Changing the content of a JavaScript file before it runs.

Posted: Sun Oct 27, 2019 7:51 am
by salvadordf
Hi,

The ResponseFilterBrowser demo shows how to intercept and replace the contents of a resource while it's being downloaded.

That demo can intercept a JavaScript file or replace the logo at briskbard.com. It uses a "filter" (TCustomResponseFilter) that receives the data in the TCustomResponseFilter.OnFilter event.

Notice that the filter only works with the downloaded resources and not with the resources read from the cache. You may have to modify the HTTP headers to always download the resource you need to filter.

Read the code comments in the demo and this page for more information :
https://magpcss.org/ceforum/apidocs3/pr ... ilter.html

Re: Changing the content of a JavaScript file before it runs.

Posted: Sun Oct 27, 2019 9:47 am
by Paulo França
That sounds awesome! Thanks (so far). I'm going to study that code right now.
As for the cache issue, it's not a problem, as my app already clears the cache (deletes the whole folder tree) before the CEF is initialized.