Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Changing the content of a JavaScript file before it runs.

Post Reply
User avatar
Paulo França
Posts: 17
Joined: Mon Jul 15, 2019 2:22 am
Location: Brazil
Contact:

Changing the content of a JavaScript file before it runs.

Post 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.
User avatar
salvadordf
Posts: 4052
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

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

Post 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
User avatar
Paulo França
Posts: 17
Joined: Mon Jul 15, 2019 2:22 am
Location: Brazil
Contact:

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

Post 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.
Post Reply