TWebbrowser Çerçeve Kaynağını Kaydetme
// Uses'a ActiveX, MSHTML_TLB, ComCtrls, ComObj ekleyelim.
function GetBrowserForFrame(Doc: IHTMLDocument2; nFrame: Integer): IWebBrowser2;
//Thanks to Rik Barker
//returns an interface to the frame's browser
var
pContainer: IOLEContainer;
enumerator: ActiveX.IEnumUnknown;
nFetched: PLongInt;
unkFrame: IUnknown;
hr: HRESULT;
begin
Result := nil;
nFetched := nil;
// Cast the page as an OLE container
pContainer := Doc as IOleContainer;
// Get an enumerator for the frames on the page
hr := pContainer.EnumObjects(OLECONTF_EMBEDDINGS or OLECONTF_OTHERS, enumerator);
if hr <> S_OK then
begin
pContainer._Release;
Exit;
end;
// Now skip to the frame we're interested in
enumerator.Skip(nFrame);
// and get the frame as IUnknown
enumerator.Next(1,unkFrame, nFetched);
// Now QI the frame for a WebBrowser Interface - I'm not entirely
// sure this is necessary, but COM never ceases to surprise me
unkframe.QueryInterface(IID_IWebBrowser2, Result);
end;
function GetFrameSource(WebDoc: iHTMLDocument2): string;
//returns frame HTML and scripts as a text string
var
re: integer;
HTMLel: iHTMLElement;
HTMLcol: iHTMLElementCollection;
HTMLlen: Integer;
ScriptEL: IHTMLScriptElement;
begin
Result := '';
if Assigned(WebDoc) then
begin
HTMLcol := WebDoc.Get_all;
HTMLlen := HTMLcol.Length;
for re := 0 to HTMLlen - 1 do
begin
HTMLel := HTMLcol.Item(re, 0) as iHTMLElement;
if HTMLEl.tagName = 'HTML' then
Result := Result + HTMLEl.outerHTML;
end;
end;
end;
function WB_SaveFrameToFile(HTMLDocument: IHTMLDocument2;
const FileName: TFileName): Boolean;
// Save IHTMLDocument2 to a file
var
PersistFile: IPersistFile;
begin
PersistFile := HTMLDocument as IPersistFile;
PersistFile.Save(StringToOleStr(FileName), System.True);
end;
function SaveWBFrames(WebBrowser1: TWebBrowser): string;
// return the source for all frames in the browser
var
Webdoc, HTMLDoc: ihtmldocument2;
framesCol: iHTMLFramesCollection2;
FramesLen: integer;
pickFrame: olevariant;
p: integer;
begin
try
WebDoc := WebBrowser1.Document as IHTMLDocument2;
Result := GetFrameSource(WebDoc);
// §§§ Hier kann Result in eine Datei gespeichert werden §§§§ oder mit
// WB_SaveFrameToFile(WebDoc,'c:\MainPage.html');
//Handle multiple or single frames
FramesCol := WebDoc.Get_frames;
FramesLen := FramesCol.Get_length;
if FramesLen > 0 then
for p := 0 to FramesLen - 1 do
begin
pickframe := p;
HTMLDoc := WebBrowser1.Document as iHTMLDocument2;
WebDoc := GetBrowserForFrame(HTMLDoc, pickframe).document as iHTMLDocument2;
if WebDoc <> nil then
begin
Result := GetFrameSource(WebDoc);
WB_SaveFrameToFile(WebDoc, 'c:\Frame' + IntToStr(p) + '.html');
// ShowMessage(HTMLDoc.Get_parentWindow.Get_name);
// ShowMessage(HTMLDoc.Get_parentWindow.Parent.Get_document.nameProp);
end;
end;
except
Result := 'No Source Available';
end;
end;
Kullanımı:
procedure TForm1.Button1Click(Sender: TObject);
begin
SaveWBFrames(Webbrowser1);
end;
Alternatif Yöntem:
//Uses' a ActiveX; ekleyelim.
function TForm1.GetFrame(FrameNo: Integer): IWebbrowser2;
var
OleContainer: IOleContainer;
enum: IEnumUnknown;
unk: IUnknown;
Fetched: PLongint;
begin
while Webbrowser1.ReadyState <> READYSTATE_COMPLETE do
Application.ProcessMessages;
if Assigned(Webbrowser1.document) then
begin
Fetched := nil;
OleContainer := Webbrowser1.Document as IOleContainer;
OleContainer.EnumObjects(OLECONTF_EMBEDDINGS, Enum);
Enum.Skip(FrameNo);
Enum.Next(1, Unk, Fetched);
Result := Unk as IWebbrowser2;
end
else
Result := nil;
end;
Örnek sayfayı yükleyelim.
procedure TForm1.Button1Click(Sender: TObject);
begin
Webbrowser1.Navigate('http://www.warebizprogramming.com/tutorials/html/framesEx1.htm');
end;
// Tüm kareleri tek dosyaya kaydedelim.
procedure TForm1.Button2Click(Sender: TObject);
var
IpStream: IPersistStreamInit;
AStream: TMemoryStream;
iw: IWebbrowser2;
i: Integer;
sl: TStringList;
begin
for i := 0 to Webbrowser1.OleObject.Document.frames.Length - 1 do
begin
iw := GetFrame(i);
AStream := TMemoryStream.Create;
try
IpStream := iw.document as IPersistStreamInit;
if Succeeded(IpStream.save(TStreamadapter.Create(AStream), True)) then
begin
AStream.Seek(0, 0);
sl := TStringList.Create;
sl.LoadFromStream(AStream);
sl.SaveToFile('c:\frame' + IntToStr(i) + '.txt');
// memo1.Lines.LoadFromStream(AStream);
sl.Free;
end;
except
end;
AStream.Free;
end;
end;
Yorumlarınız bizim için değerlidir. Konu altına yorum bırakabilirsiniz. Teşekkürler.
Daha fazla Delphi Çözümleri İçin tıklayın DELPHİ BLOG
Daha fazla Delphi Kaynak Kod ve Proje Örnekleri için tıklayın DELPHİ KAYNAK KODLARI