1
我有以下三種功能來獲取和設置HTML,第一項功能,HTML DOM,第二項功能,原有的HTML頁面,自上次功能注入的TWebBrowser一個新的代碼,但我想要做的方式,需要。有可能改寫文檔屬性,並通過TWebBrowser重新注入一個頁面的HTML?
注入新代碼後運行成功,但只是在視覺上,當我用鼠標右鍵單擊和可視化源代碼開放的記事本和,而不是DOM代碼,我看到頁面的代碼。
有沒有辦法改寫原有的HTML?
獲取HTML源代碼(DOM)。
function GetHTML(WebBrowser : TWebBrowser) : String;
var
HTMLElement : IHTMLElement;
begin
Result := '';
if Assigned(WebBrowser.Document) then begin
HTMLElement := (WebBrowser.Document as IHTMLDocument2).body;
if Assigned(HTMLElement) then begin
while HTMLElement.parentElement <> nil do begin
HTMLElement := HTMLElement.parentElement;
end;
Result := HTMLElement.outerHTML;
end else begin
Result := (WebBrowser.Document as IHTMLDocument2).all.toString;
end;
end;
end;
獲取HTML源代碼(「原始HTML」)。
function GetWebBrowserHTML(Const WebBrowser : TWebBrowser) : String;
var
LStream : TStringStream;
Stream : IStream;
LPersistStreamInit : IPersistStreamInit;
begin
if not Assigned(WebBrowser.Document) then exit;
LStream := TStringStream.Create('');
try
LPersistStreamInit := WebBrowser.Document as IPersistStreamInit;
Stream := TStreamAdapter.Create(LStream, soReference);
LPersistStreamInit.Save(Stream, true);
Result := LStream.DataString;
finally LStream.Free();
end;
end;
重寫HTML源代碼(只在視覺上明顯)。
procedure WBAppendHTML(WB : SHDocVw.TWebbrowser;const HTML : string);
var
Doc : MSHTML.IHTMLDocument2;
BodyElem : MSHTML.IHTMLBodyElement;
Range : MSHTML.IHTMLTxtRange;
begin
if not SysUtils.Supports(WB.Document, MSHTML.IHTMLDocument2, Doc) then begin
Exit;
end;
if not SysUtils.Supports(Doc.body, MSHTML.IHTMLBodyElement, BodyElem) then
begin
Exit;
end;
Range := BodyElem.createTextRange;
Range.collapse(False);
Range.pasteHTML(HTML);
end;