Solved

Internet explorer source

Posted on 2001-06-29
11
201 Views
Last Modified: 2010-04-04
Hi,
I need code to save the source from the currently active internet explorer page, to a file.

thank you
// peymanz
0
Comment
Question by:peymanz
11 Comments
 
LVL 6

Expert Comment

by:Jaymol
ID: 6237960
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
 
LVL 6

Expert Comment

by:Jaymol
ID: 6237964
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
 
LVL 2

Author Comment

by:peymanz
ID: 6238350
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 6

Expert Comment

by:Jaymol
ID: 6238360
If you alter the above code to suit your needs, it will do what you asked for.

John.
0
 
LVL 2

Author Comment

by:peymanz
ID: 6238416
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
 
LVL 2

Author Comment

by:peymanz
ID: 6238417
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
 
LVL 1

Expert Comment

by:MoonCalf
ID: 6238427
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
 
LVL 2

Author Comment

by:peymanz
ID: 6238460
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
 
LVL 6

Expert Comment

by:Jaymol
ID: 6238466
Okay.
0
 
LVL 2

Accepted Solution

by:
SChertkov earned 160 total points
ID: 6238592
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
 
LVL 2

Author Comment

by:peymanz
ID: 6765834
Sorry, I had forgot to give the points.

// regards
peymanz
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 68
code issue 8 153
Delphi Seattle: Project Options : Version info for all platforms, all releases? 2 100
Run video youtube webbrowse 10 65
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question