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?
vBulletin? v3.8.7, Copyright ?2000-2014, vBulletin Solutions, Inc.