How to load HTML string into Chromium browser in "Delphi 10 Seattle"

1.4k Views Asked by At

The following code is written for loading the HTML string (as Stream) into TWebBrowser:

procedure TfrmTestDialog.LoadDocumentFromStream(const Stream: TStream);
var  
        PersistStreamInit: IPersistStreamInit;
        StreamAdapter: IStream;
begin  
  if not Assigned(dWebBrowser.Document) then Exit;
  // Get IPersistStreamInit interface on document object 
  if dWebBrowser.Document.QueryInterface(IPersistStreamInit, 
    PersistStreamInit) = S_OK then
  begin
    // Clear document
    if PersistStreamInit.InitNew = S_OK then
    begin
      // Get IStream interface on stream
      StreamAdapter:= TStreamAdapter.Create(Stream);
      // Load data from Stream into WebBrowser
      PersistStreamInit.Load(StreamAdapter);
    end;
  end;
end;

But i need to achieve this in TChromium browser.

Anyone please help me on this...

1

There are 1 best solutions below

0
Salvador Díaz Fau On

You can convert the stream to a string using TStringStream :

function StreamToString(Stream: TStream): String;
begin
    with TStringStream.Create('') do
    try
        CopyFrom(Stream, Stream.Size - Stream.Position);
        Result := DataString;
    finally
        Free;
    end;
end;

That function is available here : http://embarcadero.newsgroups.archived.at/public.delphi.vcl.components.using/200907/0907292775.html

Then call TChromium.LoadString to load the resulting string as you can see in the MiniBrowser demo : https://github.com/salvadordf/CEF4Delphi/blob/7798f97872e4ca6f5246e3fdda04feeb990f88c7/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas#L725

MyChromiumcomponent.LoadString(MyString);