如何保存TWebBrowser的内容,包括用户输入的表单值? [英] How do I save the contents of TWebBrowser, including user-entered form values?

查看:165
本文介绍了如何保存TWebBrowser的内容,包括用户输入的表单值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将加载在Webbrowser(在Delphi中)的整个文档保存为具有新值的普通HTML文件(我的意思是用户在html表单中输入的值为本文档)?
我需要这个阅读这个HTML文档,下次使用应用程序时的所有值。

解决方案

是可能的!



小型演示应用程序,制作一个新的vcl表单应用程序,删除一个 TWebBrowser ,一个 TButton 和您的表单上的 TMemo 并使用此代码(不要忘记绑定 OnCreate 表单和 OnClick 为Button)

  

使用
Windows,消息,SysUtils,变体,类,图形,控件,表单,
对话框,OleCtrls,SHDocVw,StdCtrls,mshtml,ActiveX;

type
TForm1 = class(TForm)
WebBrowser1:TWebBrowser;
Button1:TButton;
Memo1:TMemo;
procedure FormCreate(Sender:TObject);
procedure Button1Click(Sender:TObject);
private
{私人声明}
public
{公开声明}
end;

var
Form1:TForm1;

实现

{$ R * .dfm}

//代码从about.com
过程WBLoadHTML(WebBrowser:TWebBrowser ; HTMLCode:字符串);
var
sl:TStringList;
ms:TMemoryStream;
begin
WebBrowser.Navigate('about:blank');
而WebBrowser.ReadyState< READYSTATE_INTERACTIVE do
Application.ProcessMessages;

如果分配(WebBrowser.Document)然后
开始
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;
结束
finally
sl.Free;
结束
结束
结束

procedure TForm1.Button1Click(Sender:TObject);

var
文件:IHtmlDocument2;

begin
Doc:= WebBrowser1.Document as IHtmlDocument2;
Memo1.Lines.Text:= Doc.body.innerHTML;
结束

procedure TForm1.FormCreate(Sender:TObject);

var
Html:String;
begin
Html:='更改输入值,然后按Button1更改DOM< br />< input id =myinputtype =textvalue =orgval>< / input>';
WBLoadHTML(WebBrowser1,Html);
结束

结束。

输出:





编辑 / p>

As mjn 指出,密码类型输入的值将不会显示。
您仍然可以通过以下方式获取其价值:



将这两行添加到Button1.Click并更改html



OnCreate:

  Html:=更改输入值,然后按Button1至更改的DOM< br />< ; input id =myinputtype =passwordvalue =orgval>< / input>'; 

OnClick:

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


is it possible to save entire document loaded in Webbrowser (in Delphi) as a ordinary HTML file with new values (I mean values entered by user in html's forms this document)? I need this for reading this HTML document with all values next time when application will be used.

解决方案

Sure this is possible!

Small demo App, make a new vcl forms application, drop a TWebBrowser, a TButton and a TMemo on your form and use this code (don't forget to bind 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.

Output:

EDIT

As mjn pointed out, the values of password type inputs will not be shown. You can still can get their value though:

add these 2 lines to Button1.Click and change 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]))

这篇关于如何保存TWebBrowser的内容,包括用户输入的表单值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆