Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1112
  • Last Modified:

Change html of twebbrowser

hi experts, there is any way to change the html when i use the webbrowser? for example i want put a javascript to execute when i use the  webbroswer, or edit html before to view it , another ex: instead seeing the google logo, i want see another logo..

can i change the html before see it, and if i can ,anyone can give me some examples?
0
xapsx
Asked:
xapsx
  • 3
  • 3
  • 3
1 Solution
 
geobulCommented:
Hi,

Some functions that may help you:

uses ActiveX, MSHTML; // or MSHTML_TLB depending on your Delphi version. (Import Microsoft HTML Object Library to your project)

// get the html code in a string:
function WBGetDocumentHTML(WebBrowser : TWebBrowser) : String;
var
  DOM : variant;
begin
  result := '';
  DOM := WebBrowser1.Document;
  if WebBrowser1.LocationURL <> '' then begin
    result := DOM.Body.OuterHTML;
  end;
end;

// replace the html code with a new one:
procedure WBSetDocumentHTML(WebBrowser: TWebBrowser; HTMLCode: string);
var
  sl: TStringList;
  ms: TMemoryStream;
begin
  WebBrowser.Navigate('about:blank');
  while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
   Application.ProcessMessages;

  if Assigned(WebBrowser.Document) then
  begin
    sl := TStringList.Create;
    try
      ms := TMemoryStream.Create;
      try
        sl.Text := HTMLCode;
        sl.SaveToStream(ms);
        ms.Seek(0, 0);
        (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
      finally
        ms.Free;
      end;
    finally
      sl.Free;
    end;
  end;
end;

Now all you need is to:
1. get the current HTML:
2. Modify it
3. Set it back

var
  htm: string;
begin
  htm := WBGetDocumentHTML(WebBrowser1);
  htm := .....
  WBSetDocumentHTML(WebBrowser1, htm);
end;

Regards, Geo
0
 
xapsxAuthor Commented:
i've to modify it , but how?

htm := ?

sorry.
0
 
geobulCommented:
By adding, deleting, modifying the HTML code. It is just a string. You can do whatever you want.

htm := '<html><body>Hello World!!!</body></html>'; // replace the entire page woth a new one

or:
----
procedure TForm1.Button1Click(Sender: TObject);
begin
  WebBrowser1.Navigate('www.google.com');
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  htm: string;
begin
  if WebBrowser1.LocationURL = 'http://www.google.com/' then begin
    htm := WBGetDocumentHTML(WebBrowser1);
    htm := StringReplace(htm, 'Google', 'Geoobul', [rfReplaceAll]); // replace just one string with another
    WBSetDocumentHTML(WebBrowser1, htm);
  end;
end;
----

You have to know what the actual web page contains and what you want to change in it.

Regards, Geo
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
xapsxAuthor Commented:
i've tried this with your examples but it give me some problems..

this is my unit:

---
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActiveX, MSHTML, OleCtrls, SHDocVw, StdCtrls;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    procedure WebBrowser1DocumentComplete(ASender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// get the html code in a string:
function WBGetDocumentHTML(WebBrowser : TWebBrowser) : String;
var
  DOM : variant;
begin
  result := '';
  DOM := form1.WebBrowser1.Document;
  if form1.WebBrowser1.LocationURL <> '' then begin
    result := DOM.Body.OuterHTML;
  end;
end;

  procedure WBSetDocumentHTML(WebBrowser: TWebBrowser; HTMLCode: string);
var
  sl: TStringList;
  ms: TMemoryStream;
begin
  WebBrowser.Navigate('www.google.com');
  while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
   Application.ProcessMessages;

  if Assigned(WebBrowser.Document) then
  begin
    sl := TStringList.Create;
    try
      ms := TMemoryStream.Create;
      try
        sl.Text := HTMLCode;
        sl.SaveToStream(ms);
        ms.Seek(0, 0);
        (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
      finally
        ms.Free;
      end;
    finally
      sl.Free;
    end;
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
//var

 // htm: string;
begin
//  htm := WBGetDocumentHTML(WebBrowser1);
 // htm := '<html><body>Hello World!!!</body></html>'; // replace the entire page woth a new one
 // WBSetDocumentHTML(WebBrowser1, htm);
end;

procedure TForm1.Button1Click(Sender: TObject);

begin
  WebBrowser1.Navigate('www.google.com');
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  htm: string;
begin
  if WebBrowser1.LocationURL = 'http://www.google.com/' then begin
    htm := WBGetDocumentHTML(WebBrowser1);
    htm := StringReplace(htm, 'Google', 'Geoobul', [rfReplaceAll]); // replace just one string with another
    WBSetDocumentHTML(WebBrowser1, htm);
  end;
end;
end.

-----

the hello word is working, instead the stringreplace make a Stack OverFlow

---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EStackOverflow with message 'Stack overflow'.
---------------------------
Break   Continue   Help  
---------------------------


sorry, for this trouble, still thanks!
0
 
geobulCommented:
The problem is in WBSetDocumentHTML:

WebBrowser.Navigate('www.google.com');

should always be:

WebBrowser.Navigate('about:blank');

Regards, Geo
0
 
Eddie ShipmanAll-around developerCommented:
The only problem with this technique, geobul, is that all relative links will be appended
by 'about:blank'

What I mean is, if you have an image on the page with the HTML:

<img src="images/myimage.gif" width="100" height="100" />

After doing what you are descibing, the src of that image will be:
<img src="about:blank/images/myimage.gif" width="100" height="100" />

You can change the source in TWebbrowser but you must first modify all URLs to
be absolute, i.e.
<img src="http://www.mysite.com/images/myimage.gif" width="100" height="100" />



This is fine if he wants to replace the entire page, however, he only wants to change parts
of it.

I am working on a solution to replace relativelinks with absolute ones that will make this work
for any page.

0
 
Eddie ShipmanAll-around developerCommented:
This technique was used to stop all javscript errors on a page.

Now to ADD a javascript to a page that doesn't have any scripts,
I'll have to look at that one.

// Memo1 has soruce to the javascript we want to infuse into this page.
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
 ovScript: OleVariant;
begin
  // Only loading the first script.
  ovScript :=
     WebBrowser1.OleObject.Document.all.tags('SCRIPT').item(0);
  Memo1.Lines.Add(ovScript.text);
  ovScript.Text := Memo1.Lines.Text;
  ovScript :=
      WebBrowser1.OleObject.Document.all.tags('SCRIPT').item(0);
  // reload the script so we can make sure we got it.
  Memo1.Lines.Clear;
  Memo1.Lines.Add(ovScript.text);
end;

0
 
xapsxAuthor Commented:
hi eddie thanks for your interesting, ive some problem with your code, this give me only a script into a memo1, and if i write something , it make me a javascript error? can u help me more? thanks! ( ive to replace the string ovscript := ? )

geobul, really thanks for your help


thanks guys
0
 
Eddie ShipmanAll-around developerCommented:
<< Memo1 has soruce to the javascript we want to infuse into this page >>
You MUST put the javascript you want to put into your page in the memo FIRST.
then it gets put into the page.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now