Solved

Internet explorer source

Posted on 2001-06-29
11
198 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now