0

I need to add Google ads to a page using the DOM API in any language (Delphi, C++, C#). I can add the Adsense code in a Web page using the DOM, but how can I subsequently trigger it to run?

The ad code usually runs in the document.load event. How do I attach my function to that event? (I also can use document download event but this event isn't compatible with DOM.)

I need to attach my function to document.load and I need use DOM functionality for parsing HTML code before running the Adsense Javascript code.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
relativ
  • 665
  • 1
  • 7
  • 18
  • 1
    Have you checked e.g. [this](http://stackoverflow.com/questions/153748/how-to-inject-javascript-in-webbrowser-control) post. It seems you're going to do something similar. – TLama Dec 12 '11 at 14:29
  • Thanks TLama, you are a so good programmer, I was solve my problem same way as you show me :) doc.onreadystatechange := (TEventObject.Create(DocReadyStateChangeProc) as IDispatch); – relativ Dec 12 '11 at 15:17
  • @TLama, nice link :) have you ever implemented `WebBrowser1.Document.InvokeScript`? – kobik Dec 12 '11 at 17:17
  • @kobik, no experience, but maybe when I'll have some time I'll try it :) Anyway, I'm still wondering why there are so many people who downvote this kind of "_malware creation_" questions. It seems to be malicious I know, but note what you can do e.g. with `HWND_BROADCAST` etc. – TLama Dec 13 '11 at 02:23
  • 1
    @TLama, I agree with you about the downvotes... maybe its the site policy. dunno. I'm not into politics. btw, the implementation of `InvokeScript` can be found in `EwbTools.pas` (TEmbeddedWB) method `WBExecScript`. (kinda messy though. could be optimized) :) – kobik Dec 13 '11 at 13:13

3 Answers3

1

using delphi you can do this:

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.Navigate('http://stackoverflow.com');
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; 
  const pDisp: IDispatch; var URL: OleVariant);
var
  Script: WideString;
begin
  Script := 'document.onload=alert("boo!");';
  if (pDisp as IWebBrowser) <> nil then
    ((pDisp as IWebBrowser).Document as IHTMLDocument2).parentWindow.execScript(Script, 'JScript');
end;

Here is also some additional info about sinking events with TWebBrowser. which may be useful in your case. note that the:

TEventObject = class(TDebugInterfacedObject, IDispatch) should be declared as: TEventObject = class(TInterfacedObject, IDispatch)

kobik
  • 21,001
  • 4
  • 61
  • 121
0

It sounds more like a JavaScript question.

All this will be much easier to make if you use some framework like jQuery: it will let you access easily to the DOM from your javascript code.

See for instance http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery for an official tutorial to easily manipulate the DOM of your page.

Here is an extract from this page:

We start with an empty html page:

 <html>                                                                  
 <head>                                                                  
 <script type="text/javascript" src="jquery.js"></script>          
 <script type="text/javascript">                                         
   // we will add our javascript code here                                     
 </script>                                                               
 </head>                                                                 
 <body>                                                                  
   <!-- we will add our HTML content here -->                                        
 </body>                                                                 
 </html>

This page just loads the jquery.js library (make sure the URL points to where you stored your copy of jquery! This example assumes that you store it in the same directory as this example file). Two comments indicate where we will expand this template with code.

As almost everything we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is ready.

To do this, we register a ready event for the document.

 $(document).ready(function() {
   // do stuff when DOM is ready
 });

Putting an alert into that function does not make much sense, as an alert does not require the DOM to be loaded. So lets try something a little more sophisticated: Show an alert when clicking a link.

Add the following to the <body>:

 <a href="">Link</a>

Now update the $(document).ready handler:

 $(document).ready(function() {
   $("a").click(function() {
     alert("Hello world!");
   });
 });

Then you just add this JavaScript in the Delphi, C++ or C# code which generates it, as plain text. You'll have to insert the Google AdSense code to the main page in the $(document).ready handler.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • It's not possible :( I can't access to DOM before document complate! in system programming, maybe I can Access but I have no event like before document.load, If I have like that event, yes I can use your sample but the html document coming to my hand with ready(state is already ready) already – relativ Dec 12 '11 at 12:58
  • So then focus directly on elements. See for instance [`IHTMLElement2::onreadystatechange`](http://msdn.microsoft.com/en-us/library/aa704023%28v=VS.85%29.aspx) – TLama Dec 12 '11 at 14:22
  • Thanks TLama, you are a so good programmer, I was solve my problem same way as you show me :) doc.onreadystatechange := (TEventObject.Create(DocReadyStateChangeProc) as IDispatch); – relativ Dec 12 '11 at 15:27
0

If you need to access the DOM from the outside (not inside like with jQuery) then you could easily do it with Delphi. But you should first install the excellent embedded web browser component TEmbeddedWB from bsalsa which makes it really easy to work with the browser.

Drop the TEmbeddedWB component onto your form and add a handler for e.g. OnDocumentComplete which is raised when the document's DOM is completely accessible:

procedure TForm1.EmbeddedWB1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
  // check that the event is raised for the outermost browser (not frames or iframes)
  if (pDisp as IUnknown) = (EmbeddedWB1.ControlInterface as IUnknown) then
  begin
    // do your work here
    // example: show a message 
    EmbeddedWB1.ExecScript('eval("alert(''Hello'')")', '');
  end;
end;

Start a navigation to test this:

EmbeddedWB1.Navigate('http://www.google.com');
Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
  • I am using EmbeddedWB core library already :) but I can't use the VCL component (TEmbeddedWB). I need to find active internet explorer windows for manipulate the html codes in the active internet explorer. – relativ Dec 12 '11 at 15:16