How to save TWebBrowser content, including user-entered form values?

Is it possible to save the whole document loaded in Webbrowser (in Delphi) as a regular HTML file with new values ​​(I mean the values ​​entered by the user in the html format of this document)? I need this to read this HTML document with all the values ​​the next time the application is used.

+6
source share
1 answer

Of course it is possible!

A small demo application, create a new application for vcl forms, leave TWebBrowser , a TButton and a TMemo in your form and use this code (do not forget to link OnCreate for the form and OnClick for the button)

 interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, OleCtrls, SHDocVw, StdCtrls,mshtml, ActiveX; type TForm1 = class(TForm) WebBrowser1: TWebBrowser; Button1: TButton; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} //code snagged from about.com procedure WBLoadHTML(WebBrowser: TWebBrowser; HTMLCode: string) ; var sl: TStringList; ms: TMemoryStream; begin WebBrowser.Navigate('about:blank') ; while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do Application.ProcessMessages; if Assigned(WebBrowser.Document) then begin sl := TStringList.Create; try ms := TMemoryStream.Create; try sl.Text := HTMLCode; sl.SaveToStream(ms) ; ms.Seek(0, 0) ; (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ; finally ms.Free; end; finally sl.Free; end; end; end; procedure TForm1.Button1Click(Sender: TObject); var Doc : IHtmlDocument2; begin Doc := WebBrowser1.Document as IHtmlDocument2; Memo1.Lines.Text := Doc.body.innerHTML; end; procedure TForm1.FormCreate(Sender: TObject); var Html : String; begin Html := 'change value of input and press Button1 to changed DOM<br/><input id="myinput" type="text" value="orgval"></input>'; WBLoadHTML(WebBrowser1, Html); end; end. 

Conclusion:

enter image description here

EDIT

As indicated by mjn , password type values ​​are not displayed. You can still get your value:

add these 2 lines to Button1.Click and modify the html

OnCreate:

 Html := 'change value of input and press Button1 to changed DOM<br/><input id="myinput" type="password" value="orgval"></input>'; 

OnClick:

 El := (Doc as IHtmlDocument3).getElementById('myinput') as IHtmlInputElement; Memo1.Lines.Add(Format('value of password field = %s', [El.value])) 
+7
source

All Articles