procedure TMainFrm.GetFramesLink(CheckDoc: IHTMLDocument2; First: boolean); var i : integer; Document : IHTMLDocument2; Frames : IHTMLFramesCollection2; Element : IHTMLElement; ElementName : OLEVariant; FrameWindow : IHTMLWindow2; FrameDocument : IHTMLDocument2; Dispatch : IDispatch; FrameCount : Integer; AnchorString : string ; begin if First then begin //Get the main document Document:= FInternetExplorer.Document as IHTMLDocument2; if not Assigned(Document) then exit; //After we retrieve the document, we get the number of frames Frames:=Document.Get_Frames; //Test if there has frames if not assigned(Frames) then exit; for i:=0 to Document.all.length-1 do begin Element:=Document.all.item(i,0) as IHTMLElement; if not assigned(Element) then continue; if Element.tagName = 'FRAME' then begin ElementName:=Element.getAttribute('Name',0); inc(FrameCount); FrameList.Items.add(ElementName); try Dispatch:=Frames.item(ElementName); Dispatch.QueryInterface(IHTMLWindow2,FrameWindow); if not assigned(FrameWindow) then continue; FrameDocument:=FrameWindow.document; if assigned(FrameDocument) then GetFramesLink(FrameDocument,false); except on EOLEException do begin FrameList.Items.Delete(FrameList.Count-1); dec(FrameCount); continue; end; end; end //if=Frame else if Element.tagName = 'A' then begin AnchorString := Element.innerText; if AnchorString = '' then AnchorString := '(Empty Name)'; // We know that the element is an IHTMLAnchorElement since the tagName // is 'A'. AnchorString := AnchorString + ' - ' + (Element as IHTMLAnchorElement).href; lstbxLinks.Lines.Add(AnchorString); end; end; //For i Schleife end else begin if not assigned(CheckDoc) then exit; Frames:=CheckDoc.get_frames; if not assigned(Frames) then exit; for i:=0 to CheckDoc.all.length-1 do begin Element:=CheckDoc.all.item(i,0) as IHTMLElement; if not Assigned(Element) then continue; if Element.tagName = 'FRAME' then begin ElementName:=Element.getAttribute('Name',0); inc(FrameCount); FrameList.Items.Add(ElementName); try Dispatch:=Frames.item(ElementName); Dispatch.QueryInterface(IHTMLWindow2,FrameWindow); if not assigned(FrameWindow) then continue; FrameDocument:=FrameWindow.document; if assigned(FrameDocument) then GetFramesLink(FrameDocument,false); except on EOLEException do begin FrameList.Items.Delete(FrameList.Count-1); dec(FrameCount); continue; end; end; end //if=FRAME else if Element.tagName = 'A' then begin AnchorString := Element.innerText; if AnchorString = '' then AnchorString := '(Empty Name)'; // We know that the element is an IHTMLAnchorElement since the tagName // is 'A'. AnchorString := AnchorString + ' - ' + (Element as IHTMLAnchorElement).href; lstbxLinks.Lines.Add(AnchorString); end; end; //For i Loop end; end;
procedure TMainFrm.GetAllUrl(Sender: TObject; var pDisp, URL: OleVariant); var Doc : IHTMLDocument2; ElementCollection : IHTMLElementCollection; HtmlElement : IHTMLElement; I : Integer; AnchorString : string;begin lstbxLinks.Clear; // We will process the document at this time. Trying to do // so earlier won't work because it hasn't fully loaded. Doc := FInternetExplorer.Document as IHTMLDocument2; if Doc = nil then raise Exception.Create('Couldn''t convert the ' + 'FInternetExplorer.Document to an IHTMLDocument2'); // First, grab all the elements on the web page ElementCollection := Doc.all; //GetFramesLink(Doc,true); for I := 0 to ElementCollection.length - 1 do begin // Get the current element HtmlElement := ElementCollection.item(I, '') as IHTMLElement; // Next, check to see if it is a link (tagName will be A). // You could easily find other tags (such as TABLE, FONT, etc.) if HTMLElement.tagName = 'A' then begin // Now grab the innerText for this particular link. The innerText is // all text that is inside of the particular tag. For example, // this would give us "Go To Borland" from the HTML: // <a href="http://www.borland.com"><b>Go To Borland</b></a>. // If you want "<b>Go To Borland</b>" use innerHTML. AnchorString := HtmlElement.innerText; if AnchorString = '' then AnchorString := '(Empty Name)'; // We know that the element is an IHTMLAnchorElement since the tagName // is 'A'. AnchorString := AnchorString + ' - ' + (HtmlElement as IHTMLAnchorElement).href; lstbxLinks.Lines.Add(AnchorString); end; end; if I>0 then FInternetExplorer.OnDocumentComplete:=nil; end;
var i : integer;
Document : IHTMLDocument2;
Frames : IHTMLFramesCollection2;
Element : IHTMLElement;
ElementName : OLEVariant;
FrameWindow : IHTMLWindow2;
FrameDocument : IHTMLDocument2;
Dispatch : IDispatch;
FrameCount : Integer;
AnchorString : string ;
begin
if First then
begin
//Get the main document
Document:= FInternetExplorer.Document as IHTMLDocument2;
if not Assigned(Document) then
exit;
//After we retrieve the document, we get the number of frames
Frames:=Document.Get_Frames;
//Test if there has frames
if not assigned(Frames) then
exit;
for i:=0 to Document.all.length-1 do
begin
Element:=Document.all.item(i,0) as IHTMLElement;
if not assigned(Element) then
continue;
if Element.tagName = 'FRAME' then
begin
ElementName:=Element.getAttribute('Name',0);
inc(FrameCount);
FrameList.Items.add(ElementName);
try
Dispatch:=Frames.item(ElementName);
Dispatch.QueryInterface(IHTMLWindow2,FrameWindow);
if not assigned(FrameWindow) then
continue;
FrameDocument:=FrameWindow.document;
if assigned(FrameDocument) then
GetFramesLink(FrameDocument,false);
except
on EOLEException do
begin
FrameList.Items.Delete(FrameList.Count-1);
dec(FrameCount);
continue;
end;
end;
end //if=Frame
else
if Element.tagName = 'A' then
begin
AnchorString := Element.innerText;
if AnchorString = '' then
AnchorString := '(Empty Name)';
// We know that the element is an IHTMLAnchorElement since the tagName
// is 'A'.
AnchorString := AnchorString + ' - ' +
(Element as IHTMLAnchorElement).href;
lstbxLinks.Lines.Add(AnchorString);
end;
end; //For i Schleife
end
else
begin
if not assigned(CheckDoc) then
exit;
Frames:=CheckDoc.get_frames;
if not assigned(Frames) then
exit;
for i:=0 to CheckDoc.all.length-1 do
begin
Element:=CheckDoc.all.item(i,0) as IHTMLElement;
if not Assigned(Element) then
continue;
if Element.tagName = 'FRAME' then
begin
ElementName:=Element.getAttribute('Name',0);
inc(FrameCount);
FrameList.Items.Add(ElementName);
try
Dispatch:=Frames.item(ElementName);
Dispatch.QueryInterface(IHTMLWindow2,FrameWindow);
if not assigned(FrameWindow) then
continue;
FrameDocument:=FrameWindow.document;
if assigned(FrameDocument) then
GetFramesLink(FrameDocument,false);
except
on EOLEException do
begin
FrameList.Items.Delete(FrameList.Count-1);
dec(FrameCount);
continue;
end;
end;
end //if=FRAME
else
if Element.tagName = 'A' then
begin
AnchorString := Element.innerText;
if AnchorString = '' then
AnchorString := '(Empty Name)';
// We know that the element is an IHTMLAnchorElement since the tagName
// is 'A'.
AnchorString := AnchorString + ' - ' +
(Element as IHTMLAnchorElement).href;
lstbxLinks.Lines.Add(AnchorString);
end;
end; //For i Loop
end;
end;
URL: OleVariant);
var
Doc : IHTMLDocument2;
ElementCollection : IHTMLElementCollection;
HtmlElement : IHTMLElement;
I : Integer;
AnchorString : string;begin
lstbxLinks.Clear;
// We will process the document at this time. Trying to do
// so earlier won't work because it hasn't fully loaded.
Doc := FInternetExplorer.Document as IHTMLDocument2;
if Doc = nil then
raise Exception.Create('Couldn''t convert the ' +
'FInternetExplorer.Document to an IHTMLDocument2');
// First, grab all the elements on the web page
ElementCollection := Doc.all; //GetFramesLink(Doc,true);
for I := 0 to ElementCollection.length - 1 do
begin
// Get the current element
HtmlElement := ElementCollection.item(I, '') as IHTMLElement;
// Next, check to see if it is a link (tagName will be A).
// You could easily find other tags (such as TABLE, FONT, etc.)
if HTMLElement.tagName = 'A' then
begin
// Now grab the innerText for this particular link. The innerText is
// all text that is inside of the particular tag. For example,
// this would give us "Go To Borland" from the HTML:
// <a href="http://www.borland.com"><b>Go To Borland</b></a>.
// If you want "<b>Go To Borland</b>" use innerHTML.
AnchorString := HtmlElement.innerText;
if AnchorString = '' then
AnchorString := '(Empty Name)';
// We know that the element is an IHTMLAnchorElement since the tagName
// is 'A'.
AnchorString := AnchorString + ' - ' +
(HtmlElement as IHTMLAnchorElement).href;
lstbxLinks.Lines.Add(AnchorString);
end;
end;
if I>0 then FInternetExplorer.OnDocumentComplete:=nil;
end;