简体   繁体   English

如何保存TWebBrowser的内容,包括用户输入的表单值?

[英]How do I save the contents of TWebBrowser, including user-entered form values?

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)? 是否可以将Webbrowser(在Delphi中)中加载的整个文档保存为具有新值的普通HTML文件(我的意思是用户在html的表单中输入的值本文档)? I need this for reading this HTML document with all values next time when application will be used. 我需要这个,以便在下次使用应用程序时读取包含所有值的HTML文档。

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) 小型演示应用程序,创建一个新的vcl表单应用程序,在表单上删除一个TWebBrowser ,一个TButton和一个TMemo并使用此代码(不要忘记绑定OnCreate for Form和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. 正如mjn指出的那样, 密码类型输入的值将不会显示。 You can still can get their value though: 你仍然可以获得他们的价值:

add these 2 lines to Button1.Click and change html 将这两行添加到Button1.Click并更改html

OnCreate: 在OnCreate:

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

OnClick: 的OnClick:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Cookie保存和检索表单值(包括下拉菜单)? - How do I save and retrieve form values including dropdowns using cookies? 在表单中发生错误后,如何获取在Rails中输入的值以保留在表单中? - How do I get values entered in my Rails to form to remain after an error occurs in the form? Twitter Bootstrap表格。 如何捕获在表单中输入的值? - Twitter Bootstrap Form. How do I capture values entered in the form? 当用户离开页面时,如何跟踪何时在表单中输入什么文本? - How do I keep track of when what is the text entered in the form when the user navigates away from the page? javascript:如何提取在此表单中输入的值? - javascript: how can I extract the values entered in this form? 除了用户输入的值在 1 - 100 之间(例如年龄框),我如何才能制作我的表单 - How can I make my form only except values that the user entered that are between 1 - 100 (e.g like for the age box) 如何根据在访问权限中输入的员工ID自动填写表格? - How do I autofill a form based on the employee ID entered in access? 如何访问.php文件中以html格式输入的信息? - How do I access information entered in a form on html on the .php file? 如何捕获输入到HTML表单字段中的数据? - How do I capture data entered into the field of an HTML form? 如何防止将无效字符输入表单? - How do I prevent invalid characters from being entered into a form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM