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
Replace server response
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?
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?
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Replace server response
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.
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.
Re: Replace server response
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.
I will be grateful for any help.
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Replace server response
You need to use TResponseFilterBrowserFrm.CopyScript and modify the line with :
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 :
Code: Select all
Move(data_in^, data_out^, data_out_written);
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.
Re: Replace server response
Thanks a lot, salvadordf
Re: Replace server response

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);
Code: Select all
["res","refer"] replace ["res","test"]]U or ["res","test"
Copy script
Replace logo
Replace Text

- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Replace server response
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
How to do? I sometimes get extra characters in the modified code.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);
I'm sorry but I'm extremely busy these days and this seems to be a text encoding issue.dilfich wrote: Fri Feb 15, 2019 7:31 pmOr add an example in the Demo ResponseFilterBrowser, many will certainly help.Code: Select all
["res","refer"] replace ["res","test"]]U or ["res","test"
Copy script
Replace logo
Replace Text
![]()
Re: Replace server response
I think the problem is length.
Like there is no mistakes
Problem
Need to know a new the size of the and where the point to?
Like there is no mistakes
Code: Select all
Repl:= System.AnsiStrings.StringReplace(Repl, 'refer', 'reser', [rfReplaceAll, rfIgnoreCase]);
Code: Select all
Repl:= System.AnsiStrings.StringReplace(Repl, 'refer', 'test', [rfReplaceAll, rfIgnoreCase]);
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Replace server response
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 :
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.
Re: Replace server response
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. 
