【问题标题】:How do I save the contents of TWebBrowser, including user-entered form values?如何保存 TWebBrowser 的内容,包括用户输入的表单值?
【发布时间】:2012-12-20 09:22:49
【问题描述】:

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

【问题讨论】:

  • 如果可能的话,Delphi 应用程序甚至可以在用户提交 HTML 表单之前检索表单字段值。包括密码等敏感数据。
  • @mjn,好吧,但这是可能的。即使是从屏蔽的输入字段(如密码),您也可以很容易地获取用户输入的原始值。不知道如何保存到文档,但我认为您必须在保存时自行修改。
  • 如果这适用于密码类型输入字段,我不会信任任何使用嵌入式 Web 浏览器与 Internet 站点通信的应用程序,例如进行 OAuth 登录。

标签: forms delphi save twebbrowser


【解决方案1】:

当然可以!

小型演示应用程序,制作一个新的 vcl 表单应用程序,在您的表单上放置 TWebBrowserTButtonTMemo 并使用此代码(不要忘记为表单绑定 OnCreateOnClick 按钮)

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.

输出:

编辑

正如mjn所指出的,password类型输入的值不会显示。 不过,您仍然可以获得它们的价值:

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

创建时:

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

点击:

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

【讨论】:

  • 只是好奇:这甚至适用于密码类型输入字段吗?
  • @mjn:看我的编辑,这是可能的,但它需要了解 Html 文档,在这种情况下没有通用解决方案......
  • @Artik:不客气!你可以通过接受这个答案来感谢我;)meta.stackexchange.com/a/5235
  • @DavidHeffernan,这很糟糕,因为我的外部应用程序可以从其他 IE 应用程序读取密码......虽然没有经过测试。但是我可以通过窗口句柄从我自己的应用程序中防御性地访问外国 IE DOM,所以 mjn 在这里有一点。
  • @kobik 一旦你在你的机器上安装了恶意软件,你就会被淹没。如果您的进程可以读取密码,那么恶意软件就可以注入您的进程并读取它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 2021-06-11
相关资源
最近更新 更多