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.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

How to edit source HTML code before render

User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to edit source HTML code before render

Post by salvadordf »

If you can't see the Delphi code line where the error has occurred then it could be a CEF error or perhaps there is a problem replacing the resource in Delphi. If Delphi writes out of the allocated memory to data_out or sets data_out_written with an incorrect value, CEF could crash.

I would try to make it work step by step.

Try to make a simple replacement like changing a character in some text file. If that works correctly then try to add or remove a few text lines.
Student
Posts: 73
Joined: Tue Aug 07, 2018 9:20 am

Re: How to edit source HTML code before render

Post by Student »

Text is replaced, tried length longer less to do. White page is no been.

Code: Select all

	if (data_out_written > 0) then
            begin
              SetString(mystring, PAnsichar(data_in), data_out_written);
              if pos('The power of 10 applications', mystring) <> 0 then
              begin
                mystring := StringReplace(mystring,
                  'The power of 10 applications', 'HELLO WORLD!',
                  [rfReplaceAll, rfIgnoreCase]);
                data_out_written:=length(mystring);
                Move(PAnsichar(mystring)^, data_out^, data_out_written);
              end
              else
                Move(data_in^, data_out^, data_out_written);
            end;
Student
Posts: 73
Joined: Tue Aug 07, 2018 9:20 am

Re: How to edit source HTML code before render

Post by Student »

if singleprocess:=true; then after closing app AV, this in demo unchanged.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to edit source HTML code before render

Post by salvadordf »

Why is it comparing data_out_written with 0 in the first line ?

These are the parameters in the OnFilter event :
  • data_in : Pointer to the input data. You can only copy the contents from the address pointed by this parameter.
  • data_in_size : Input data size. This value can't be modified.
  • data_in_read : Amount of input data read in the OnFilter event. The app should set the data_in_read value but it can't be greater than data_in_size.
  • data_out : Pointer to the output data. You can write your data in the address pointed by this parameter but you can't reallocate this pointer.
  • data_out_size : Output data size. This value can't be modified.
  • data_out_written : Amount of data written in the address pointed by data_out. data_out_written can't be greater than data_out_size. If no output data was written then set data_in_read := data_in_size. If you need to write more data and this is the last input data chunk then set data_out_written := data_out_size; and aResult := RESPONSE_FILTER_NEED_MORE_DATA;
  • aResult : This parameter can have 3 values :
    • RESPONSE_FILTER_NEED_MORE_DATA : Use this value when you still have pending data to write.
    • RESPONSE_FILTER_DONE : Use this value when you are finished writing data.
    • RESPONSE_FILTER_ERROR : Use this value to indicate an error.
Perhaps I missed setting data_in_read := data_in_size in the demo when there was no more output data written. I'll take a look at this demo as soon as I can but please, check that your app sets the right values in all the parameters, specially in data_out_written and data_in_read.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to edit source HTML code before render

Post by salvadordf »

Student wrote: Wed Aug 08, 2018 1:45 pm if singleprocess:=true; then after closing app AV, this in demo unchanged.
That's normal. The single process mode is not supported. It generates multiple errors and it's only useful for debugging.
Student
Posts: 73
Joined: Tue Aug 07, 2018 9:20 am

Re: How to edit source HTML code before render

Post by Student »

Ok, thanks you, I'll recheck code. Most likely that the missed, if replace text size > data_out_size then not display page, not decided yet.
Student
Posts: 73
Joined: Tue Aug 07, 2018 9:20 am

Re: How to edit source HTML code before render

Post by Student »

So far so made, works while without bugs. Replaced data_out_written := min(data_in_size, data_out_size); on data_out_written := data_out_size; to large chunk write in data_out.

Code: Select all

if (data_out <> nil) then
        begin
          data_out_written := data_out_size;
          if FStream.Position <> FStream.Size then
          begin
            FStream.read(data_out^, data_out_written);
            data_in_read := 0;
            aResult := RESPONSE_FILTER_NEED_MORE_DATA;
          end
          else
          begin
            data_in_read := data_in_size;
            data_out_written := 0;
            FRscCompleted := PostMessage(Handle, STREAM_COPY_COMPLETE, 0, 0);
            aResult := RESPONSE_FILTER_DONE;
          end;
        end;
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to edit source HTML code before render

Post by salvadordf »

I uploaded a new version of the ResponseFilterBrowser demo with a new option to replace a resource.

I also replaced the BriskBard logo with a much larger image of Jupiter and added more code comments.

This demo uses TChromium.ReloadIgnoreCache and the user can't modify the URL. If you need to add this code to your app then you will probably need to set some custom HTTP headers to always download all resources ignoring the cache.
Student
Posts: 73
Joined: Tue Aug 07, 2018 9:20 am

Re: How to edit source HTML code before render

Post by Student »

Thank you for a new demo. And if there will be a lot of replaced resources during page loading, the demo only guarantees the replacement of one resource. And that nothing passed by the filter, I must correct in Chromium1GetResourceResponseFilter?
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to edit source HTML code before render

Post by salvadordf »

This demo only creates one ICefResponseFilter called FFilter. If you need to filter or replace several resources you will have to create several ICefResponseFilter and assign the OnFilter event in all of them.

If you reuse the same procedure for all OnFilter events then you will have to check the "Sender" parameter to know what resource needs to be filtered or replaced.

The TChromium.OnGetResourceResponseFilter event is called once for each resource and you would have to recognize each of them to assign one of your ICefResponseFilter. If you don't recognize the resource then set the ICefResponseFilter parameter to nil. This demo uses the IsMyResource function to recognize the resources.
Post Reply