Internet explorer source

Hi,
I need code to save the source from the currently active internet explorer page, to a file.

thank you
// peymanz
LVL 2
peymanzAsked:
Who is Participating?
 
SChertkovConnect With a Mentor Commented:
You must use IPersistFile in DocumentComplete event

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  pf: IPersistFile;
begin
  if pDisp = WebBrowser1.ControlInterface then
    begin
      if Succeeded(WebBrowser1.Document.QueryInterface(IPersistFile, pf)) then
        OleCheck(pf.Save('1.htm', True));
    end;
end;
0
 
JaymolCommented:
Here's an example of something I did recently....

Unit source code....

unit htmlsource01;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Psock, NMHttp, StdCtrls;

type
  TForm1 = class(TForm)
    txtURL: TEdit;
    Memo1: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    NMHTTP1: TNMHTTP;
    procedure txtURLKeyPress(Sender: TObject; var Key: Char);
    procedure Memo1Change(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure NMHTTP1Connect(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
     Form1                    : TForm1;
     JustChanged     :     Boolean;

implementation

{$R *.DFM}

procedure TForm1.txtURLKeyPress(Sender: TObject; var Key: Char);
begin
     If Key=#13 then begin
          NMHTTP1.Disconnect;
          txtURL.Enabled:=False;
          JustChanged:=True;
          Key:=#0;
          txtURL.SelectAll;
          NMHTTP1.InputFileMode := FALSE;
          NMHTTP1.OutputFileMode := FALSE;
          NMHTTP1.ReportLevel := Status_Basic;
          NMHTTP1.Get(txtURL.Text);
          Memo1.Text := NMHTTP1.Body;
     end;
end;

procedure TForm1.Memo1Change(Sender: TObject);
var
     TmpI     :     Integer;
     TmpS     :     String;
begin
     If JustChanged then begin
          JustChanged:=False;
          For TmpI:=1 to Length(Memo1.Text) do begin
               If Memo1.Text[TmpI]=#10 then
                    TmpS:=TmpS+#13;
               TmpS:=TmpS+Memo1.Text[TmpI];
          end;
          Memo1.Text:=TmpS;
     end;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
     txtURL.Width:=Form1.ClientWidth-24;
     Memo1.Width:=Form1.ClientWidth-24;
     Memo1.Height:=Form1.ClientHeight-80;
end;

procedure TForm1.NMHTTP1Connect(Sender: TObject);
begin
     txtURL.Enabled:=True;
end;

end.



Form description....

object Form1: TForm1
  Left = 192
  Top = 103
  BorderStyle = bsSingle
  Caption = 'HTML Source Viewer'
  ClientHeight = 453
  ClientWidth = 661
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnResize = FormResize
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 12
    Top = 8
    Width = 56
    Height = 13
    Caption = 'Enter URL :'
  end
  object Label2: TLabel
    Left = 12
    Top = 52
    Width = 101
    Height = 13
    Caption = 'HTML Source Code :'
  end
  object txtURL: TEdit
    Left = 12
    Top = 24
    Width = 637
    Height = 21
    TabOrder = 0
    OnKeyPress = txtURLKeyPress
  end
  object Memo1: TMemo
    Left = 12
    Top = 68
    Width = 637
    Height = 373
    Font.Charset = ANSI_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Courier New'
    Font.Style = []
    ParentFont = False
    ScrollBars = ssBoth
    TabOrder = 1
    WordWrap = False
    OnChange = Memo1Change
  end
  object NMHTTP1: TNMHTTP
    Port = 0
    ReportLevel = 0
    OnConnect = NMHTTP1Connect
    Body = 'Default.htm'
    Header = 'Head.txt'
    InputFileMode = False
    OutputFileMode = False
    ProxyPort = 0
    Left = 16
    Top = 72
  end
end

0
 
JaymolCommented:
This bit is not too comprehensive because it only works with IE...

function Get_URL: String;
var
     Client: TDDEClientConv;
begin
     Result:='';
     Client:=TDDEClientConv.Create(nil);
     With Client do begin
          SetLink('IExplore', 'WWW_GetWindowInfo');
          Result:=StrPas(RequestData('0xFFFFFFFF'));
          CloseLink;
     end;
     Client.Free;
end;

but there was a more comprehensive version done on EE recently.....worth searching for.

John.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
peymanzAuthor Commented:
The problem is that I want to get the source of the page. Getting the url and sending the request does not work, since the page will be refreshed. I want the equivalent to
view->source menu.

// peymanz
0
 
JaymolCommented:
If you alter the above code to suit your needs, it will do what you asked for.

John.
0
 
peymanzAuthor Commented:
How if I understand right the first code gets the url from the edit box and requests the page. Which I can not do since the page has already been requested once from the browser and if It is requested once more the contents will change.

And the second code returns the current url of IE. It is the same problem there I can not send a new requests to the server.

Note that the page has already been requested once (from IE) and I want to get the source which is stored locally.

// peymanz
0
 
peymanzAuthor Commented:
How if I understand right the first code gets the url from the edit box and requests the page. Which I can not do since the page has already been requested once from the browser and if It is requested once more the contents will change.

And the second code returns the current url of IE. It is the same problem there I can not send a new requests to the server.

Note that the page has already been requested once (from IE) and I want to get the source which is stored locally.

// peymanz
0
 
MoonCalfCommented:
I tried Jaymol's code and it worked.

I used the second function to get the url and then first lot of code to display the source.

MoonCalf.
0
 
peymanzAuthor Commented:
Sorry but as you say the first lot of code gets the page from the server before it displays it. The source is already on the computer. I do not want to get it from the server again.

// peymanz
0
 
JaymolCommented:
Okay.
0
 
peymanzAuthor Commented:
Sorry, I had forgot to give the points.

// regards
peymanz
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.