打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Replace html in active TWebbrowser document (IHTMLTxtRange)

View Full Version : Replace html in active TWebbrowser document (IHTMLTxtRange)


PasTra
12-17-2007, 08:00 AM
Hi I have a question:

How can I navigate to www.google.com (TWebBrowser), when loaded remove the google image (http://www.google.com/intl/en_ALL/images/logo.gif) from the document.?

What I have tryid to modify the following code, but I dont see how to call some procedure like "ie. replace".

This is a examble of how to APPEND html to a running instance of Twebbrowser.


procedure AppendToWB(WB: tEmbeddedWB; const html: widestring) ;
var
Range: IHTMLTxtRange;
begin
Range := ((WB.Document AS IHTMLDocument2).body AS IHTMLBodyElement).createTextRange;
Range.Collapse(False) ;
range.
Range.PasteHTML(html) ;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
s:= 'Test';
AppendToWB(EmbeddedWB1,s) ;
end;


Can anyone give me some guidance on how to Modify/remove tags at "view time"? ie.: delete img tag from google so only THAT image isn't displayed, or to remove <Iframe>...</Iframe> tags.

Thanks in advance.

Warm holiday wishes to all and a happy new year!


Regards PasTra

PasTra
12-17-2007, 08:03 AM
EDIT: I mistakenly wrote TWebbrowser when I meant: "TembeddedWebbrowser" or "TembeddedWB".

Not that It really matters ;D

Regards PasTra

MrBaseball34
12-17-2007, 10:33 AM
try this:


procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
Doc: IHTMLDocument2;
ElementCollection: IHTMLElementCollection;
Images: IHTMLElementCollection;
Element: IHTMLElement;
LogoImage: IHTMLDOMNode;
i, j: Integer;
Attribs: IHTMLAttributeCollection;
DOMAttribute: IHTMLDOMAttribute;
Dispatch: IDispatch;
oVar: OleVariant;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
ElementCollection := Doc.body.all as IHTMLElementCollection;
Images := ElementCollection.tags('IMG') as IHTMLElementCollection;
if Images <> nil then
begin
for i:=0 to Images.length-1 do
begin
Element := Images.item(i,0) as IHTMLElement;
LogoImage := Element as IHTMLDomNode;
if LogoImage <> nil then
begin
Attribs := LogoImage.attributes as IHTMLAttributeCollection;
if Attribs <> nil then
begin
for j:=0 to Attribs.length-1 do
begin
oVar := j;
Dispatch := Attribs.item(oVar);
if ASSIGNED(Dispatch) then
begin
DOMAttribute := Dispatch as IHTMLDOMAttribute;
if ASSIGNED(DOMAttribute) and (DOMAttribute.nodename = 'src') then
begin
LogoImage.parentNode.removeChild(LogoImage);
end;
end;
end;
end;
end;
end;
end;
end;


MrBaseball34
Hook'Em Horns!
http://www.austinmetrobaseball.com/texas.gif
2005 College Football National Champions
2005 College Baseball National Champions

MrBaseball34
12-17-2007, 10:35 AM
oops, change the last if to this:


if ASSIGNED(DOMAttribute) and (DOMAttribute.nodename = 'src') and
(Pos('images/logo.gif', DOMAttribute.nodeValue) > 0) then
begin
LogoImage.parentNode.removeChild(LogoImage);
end;



MrBaseball34
Hook'Em Horns!
http://www.austinmetrobaseball.com/texas.gif
2005 College Football National Champions
2005 College Baseball National Champions

PasTra
12-17-2007, 07:14 PM
Thank you, this works almost perfect for images.
(The images show up while loading page, but i guess that can't be changed since we need source before modifying.)

Is it possible to modify this example to remove an iframe?

Thanks



Regards PasTra

MrBaseball34
12-18-2007, 03:42 AM
Change IMG to IFRAME in the code:
tags('IMG')

MrBaseball34
Hook'Em Horns!
http://www.austinmetrobaseball.com/texas.gif
2005 College Football National Champions
2005 College Baseball National Champions

MrBaseball34
12-18-2007, 03:44 AM
CLearere:

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
Doc: IHTMLDocument2;
ElementCollection: IHTMLElementCollection;
Frames: IHTMLElementCollection;
Element: IHTMLElement;
Frame: IHTMLDOMNode;
i: Integer;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
ElementCollection := Doc.body.all as IHTMLElementCollection;
Frames := ElementCollection.tags('IFRAME') as IHTMLElementCollection;
if Frames <> nil then
begin
for i:=0 to Frames.length-1 do
begin
Element := Frames.item(i,0) as IHTMLElement;
Frame := Element as IHTMLDomNode;
if Frame <> nil then
begin
Frame.parentNode.removeChild(Frame);
end;
end;
end;
end;


MrBaseball34
Hook'Em Horns!
http://www.austinmetrobaseball.com/texas.gif
2005 College Football National Champions
2005 College Baseball National Champions

PasTra
12-18-2007, 08:25 AM
Once again, thanks for the help.

All perfect now

Regards PasTra

OmidFatehi
05-27-2013, 01:24 PM
try this:


procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
Doc: IHTMLDocument2;
ElementCollection: IHTMLElementCollection;
Images: IHTMLElementCollection;
Element: IHTMLElement;
LogoImage: IHTMLDOMNode;
i, j: Integer;
Attribs: IHTMLAttributeCollection;
DOMAttribute: IHTMLDOMAttribute;
Dispatch: IDispatch;
oVar: OleVariant;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
ElementCollection := Doc.body.all as IHTMLElementCollection;
Images := ElementCollection.tags('IMG') as IHTMLElementCollection;
if Images <> nil then
begin
for i:=0 to Images.length-1 do
begin
Element := Images.item(i,0) as IHTMLElement;
LogoImage := Element as IHTMLDomNode;
if LogoImage <> nil then
begin
Attribs := LogoImage.attributes as IHTMLAttributeCollection;
if Attribs <> nil then
begin
for j:=0 to Attribs.length-1 do
begin
oVar := j;
Dispatch := Attribs.item(oVar);
if ASSIGNED(Dispatch) then
begin
DOMAttribute := Dispatch as IHTMLDOMAttribute;
if ASSIGNED(DOMAttribute) and (DOMAttribute.nodename = 'src') then
begin
LogoImage.parentNode.removeChild(LogoImage);
end;
end;
end;
end;
end;
end;
end;
end;


MrBaseball34
Hook'Em Horns!
http://www.austinmetrobaseball.com/texas.gif
2005 College Football National Champions
2005 College Baseball National Champions


Is there a way to remove the certain texts?

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
删除文件夹(含子目录)
Delphi TWebBrowser[6] 获取网页所有链接(元素)、下拉菜单及GetElementByID返回值的有效性判定方法
XLSReadWriteII5使用示例
Delphi UrlDownloadToFile 实现文件下载
delphi 如何得到 WinRar 处理解压缩文件的返回值 ?
使用代码创建临时表(delphi)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服