Search code examples
delphimshtmltwebbrowser

How to get a JS script console output in TWebbrowser?


I want to run a javascript code in TWebbrowser and get the console output of it. For instance, if i run this code in the console :

var a = 2; var b = 3; var c = a +b ; console.log('The result is '+ c); 

I get this output on console :

The result is 5

The code i use to run a JS script with TWebbrowser is this :

twebbrowser1.navigate('javascript:var a = 2; var b = 3; var c = a +b ; console.log('The result is '+ c);');

It works, but i don't know how to get the console output. Is there a way to do it ?

Thanks in advance !


Solution

  • According to MSDN, it looks like your (Delphi) app would need to implement the IDeveloperConsoleMessageReceiver interface.

    Then you can connect an object instance of your IDeveloperConsoleMessageReceiver to the browser's current Document. Query the TWebBrowser.Document property for its IOleCommandTarget interface and then call its Exec() method to issue a IDM_ADDCONSOLEMESSAGERECEIVER command.

    Try that, and if you get stuck, try asking again.