yoy.be "Why-o-Why"

WebBrowser: save HTML page data

2006-05-08 17:00  i598  delphi  [permalink]

Though this works great:

delphi.about.com: How to save a web page as HTML or MHT

it operates much like 'view source' and shows the original HTML used to first render the page. If scripting or the containing application uses the MSHTML interfaces to alter the display and/or content (e.g. with innerHTML or insertAdjacentHTML) this doesn't end up in the written data. In those cases I tend to use this code:

var
  w:WideString;
  w1:word;
  f:TFileStream;
begin
  if SaveDialog1.Execute then
   begin
    w:=((Web1.Document as IHTMLDocument2).body as IHTMLElement).parentElement.outerHTML;
    f:=TFileStream.Create(SaveDialog1.FileName,fmCreate);
    w1:=$FEFF;
    f.Write(w1,2);
    f.Write(w[1],Length(w)*2);
    f.Free;
   end;
end;

The prefix $FEFF is used by Unicode to determine the direction of MSB/LSB's in the file containing UTF-16 data.


twitter reddit linkedin facebook