Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Jan : Re: Get and CHANGE Body tag
| Subject: | Re: Get and CHANGE Body tag |
| Posted by: | "Ralf Junker - http://www.yunqa.de/delphi/" (delphi.at.yunqa.dot...@) |
| Date: | Tue, 17 Jan 2006 12:24:09 |
Hello Eddie,
this is the idea: If you can not manipulate the <BODY> tag inside TWebBrowser,
you can do so outside TWebBrowser provided that you feed you HTML data to
TWebBrowser manually.
Below is an example which demonstrates how to do this with the help of my
DIHtmlParser HTML processing suite available from
http://www.yunqa.de/delphi/htmlparser/
Now all you need to do to add the OnMouseUp event to the <BODY> tag is to pass
your HTML through the Insert_Body_OnMouseUp function before showing it in
TWebBrowser. I have commented plenty to help to understand what's going on. You
can easily add more attributes if you require.
Regards,
Ralf
=====================================================
{ DIHtmlParser example showing how to insert an OnMouseUp event into the HTML
document's <BODY> start tag.
Visit the DIHtmlParser homepage for latest information and updates:
http://www.yunqa.de/delphi/
Copyright (c) 2006 Ralf Junker, The Delphi Inspiration <delphi@yunqa.de>
------------------------------------------------------------------------------ }
program Insert_Body_Event;
{$APPTYPE Console}
{$I DI.inc}
uses
DIUtils, DIHtmlMisc, DIHtmlParser, DIHtmlWriterPlugin;
//------------------------------------------------------------------------------
{ This function inserts an OnMouseUp="" attribute to any existing <BODY> tag. }
function Insert_Body_OnMouseUp(const AHtml: AnsiString): AnsiString;
var
HtmlParser: TDIHtmlParser;
HtmlWriter: TDIHtmlWriterPlugin;
begin
{ Create the HTML parser. }
HtmlParser := TDIHtmlParser.Create(nil);
{ Set up individual HTML tag filtering. }
HtmlParser.HtmlTagFilters := TDITagFilters.Create;
{ Since we need to manipulate the <BODY> tag, show it to us. }
HtmlParser.HtmlTagFilters.SetStart(TAG_BODY_ID, fiShow);
{ Create the HTML writer. }
HtmlWriter := TDIHtmlWriterPlugin.Create(nil);
{ Attach the writer to the parser. This causes the parser to feed all HTML
to the writer which takes care of writing syntactically correct HTML. }
HtmlWriter.HtmlParser := HtmlParser;
{ The writer must write all HTML contents ... }
HtmlWriter.SetAllFilters(fiShow);
{ ... except for those handled otherwise. This obviously applies to the
<BODY> tag where we need to add another attribute to. See below for
how this is done. }
HtmlWriter.FilterHtmlTags.SetStartEnd(fiShowHidden);
try
{ Set the parser's HTML source. }
HtmlParser.SourceBufferAsStrA := AHtml;
{ Loop through the HTML. The filtering is set up in way that the following
loop is executed for <BODY> tags only. All other HTML data is
automatically passed on to the writer internally. }
while HtmlParser.ParseNextPiece do
begin
{ Now for the <BODY> tag: Force the OnMouseUp attribute with an empty
string contents. }
HtmlParser.HtmlTag.ForceAttrib(
ATTRIB_ONMOUSEUP_ID,
ATTRIB_ONMOUSEUP,
'""');
{ Write the modified <BODY> tag as this is not done automatically
according to above's filter settings. }
HtmlWriter.WriteCurrentHtmlTagStart;
end;
{ Retriev the data. }
Result := HtmlWriter.Writer.DataToStrA;
finally
{ Cleanup. }
HtmlWriter.Free;
HtmlParser.HtmlTagFilters.Free;
HtmlParser.Free;
end;
end;
//------------------------------------------------------------------------------
const
HTML_DATA =
'<HEAD>' + CRLF +
' <!--Head Content-->' + CRLF +
'</HEAD>' + CRLF +
'<BODY bgcolor="white" text="black">' + CRLF +
' <!--Body Content-->' + CRLF +
'</BODY>';
begin
{ Do this once only in your application. }
RegisterHtmlTags; RegisterHtmlAttribs; RegisterHtmlDecodingEntities;
WriteLn('Original HTML:');
WriteLn;
WriteLn(HTML_DATA);
WriteLn;
WriteLn('Modified HTML:');
WriteLn;
WriteLn(Insert_Body_OnMouseUp(HTML_DATA));
WriteLn;
WriteLn;
WriteLn('Done - Press ENTER to exit.');
ReadLn;
end.
=====================================================
Eddie Shipman <mrbaseball34@No_Spam_gmail.com> wrote:
>Not sure what you mean, Ralf, could you show me?
---
The Delphi Inspiration
http://www.yunqa.de/delphi/