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.

Replace server response

Post Reply
User avatar
xpert13
Posts: 39
Joined: Wed May 31, 2017 5:26 pm

Replace server response

Post by xpert13 »

Hello.

Some site has inline JS code in the page response. I want to change those code before browser execute JS. How can I do it?
User avatar
salvadordf
Posts: 4040
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Replace server response

Post by salvadordf »

Hi,

Right now the official CEF forum is down and I can't search a way to change the response headers but you can use the code in the ResponseFilterBrowser demo to change the javascript code that checks the response to ignore that value.
User avatar
xpert13
Posts: 39
Joined: Wed May 31, 2017 5:26 pm

Re: Replace server response

Post by xpert13 »

Thank you. It seems to be that I looking for. But I not completly understand how it works. Your have 2 functions: first one read complete resonse, second one is replacing complete resonse by own. But I don't understand how to read responce and replace just part of it.

I will be grateful for any help.
User avatar
salvadordf
Posts: 4040
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Replace server response

Post by salvadordf »

You need to use TResponseFilterBrowserFrm.CopyScript and modify the line with :

Code: Select all

Move(data_in^, data_out^, data_out_written);
That code copies the input buffer in "data_in" to the output buffer in "data_out" unchanged.

You need to search and replace the string in the "data_in" buffer and then write that buffer to the "data_out" buffer.
Use the "data_in" pointer like a PChar with a "data_in_size" size and use the search and replace functions in Delphi.

Things to keep in mind :
  • The web server sends encoded text. See how the TResponseFilterBrowserFrm.GetResponseEncoding and TResponseFilterBrowserFrm.StreamCopyCompleteMsg functions use the content type to decode the text.
  • The TResponseFilterBrowserFrm.Filter_OnFilter function can be called several times with several chunks of data if the resource is too big. Read the code comments in uResponseFilterBrowser.pas.
User avatar
xpert13
Posts: 39
Joined: Wed May 31, 2017 5:26 pm

Re: Replace server response

Post by xpert13 »

Thanks a lot, salvadordf
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: Replace server response

Post by dilfich »

:?

Code: Select all

 var
  Repl: AnsiString;
......
  Repl:= PAnsiChar(data_in);
  Repl:= System.AnsiStrings.StringReplace(Repl, 'refer', 'test', [rfReplaceAll, rfIgnoreCase]);
  data_in:= PAnsiChar(Repl);

 Move(data_in^, data_out^, data_out_written);
How to do? I sometimes get extra characters in the modified code.

Code: Select all

["res","refer"] replace ["res","test"]]U or ["res","test"
Or add an example in the Demo ResponseFilterBrowser, many will certainly help.
Copy script
Replace logo
Replace Text
:roll:
User avatar
salvadordf
Posts: 4040
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Replace server response

Post by salvadordf »

dilfich wrote: Fri Feb 15, 2019 7:31 pm :?

Code: Select all

 var
  Repl: AnsiString;
......
  Repl:= PAnsiChar(data_in);
  Repl:= System.AnsiStrings.StringReplace(Repl, 'refer', 'test', [rfReplaceAll, rfIgnoreCase]);
  data_in:= PAnsiChar(Repl);

 Move(data_in^, data_out^, data_out_written);
How to do? I sometimes get extra characters in the modified code.
Perhaps the server is sending text encoded in UTF8 or something else that "System.AnsiStrings.StringReplace" can't handle correctly.
dilfich wrote: Fri Feb 15, 2019 7:31 pm

Code: Select all

["res","refer"] replace ["res","test"]]U or ["res","test"
Or add an example in the Demo ResponseFilterBrowser, many will certainly help.
Copy script
Replace logo
Replace Text
:roll:
I'm sorry but I'm extremely busy these days and this seems to be a text encoding issue.
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: Replace server response

Post by dilfich »

I think the problem is length.
Like there is no mistakes

Code: Select all

Repl:= System.AnsiStrings.StringReplace(Repl, 'refer', 'reser', [rfReplaceAll, rfIgnoreCase]);
Problem

Code: Select all

Repl:= System.AnsiStrings.StringReplace(Repl, 'refer', 'test', [rfReplaceAll, rfIgnoreCase]);
Need to know a new the size of the and where the point to?
User avatar
salvadordf
Posts: 4040
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Replace server response

Post by salvadordf »

The TResponseFilterBrowserFrm.ReplaceLogo function in the ResponseFilterBrowser demo handles buffers with different sizes.
The original logo is much smaller than the new logo but the demo keeps sending data.

If the new data buffer is smaller than the old buffer then you need to :
  • Write the modified data in the buffer.
  • Set the data_in_read and data_out_written parameters accordingly. They will be smaller than data_in_size.
  • Return RESPONSE_FILTER_DONE.
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: Replace server response

Post by dilfich »

I can not understand how to upgrade the size. ReplaceLogo not suitable, there is a file changing and not the text, the html code is not changed. I ask specifically about the text. How will you have the time, please add ResponseFilterBrowser is also an example of modification of the text received in the response from the server. :?
Post Reply