2

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 !

6
  • What do you mean by " get the console output"? Do you want the TWebBrowser to display the JS console or do you want to capture (in Delphi code) what output the console script produces? Commented Jun 9, 2016 at 16:08
  • 1
    In the latter case, you should not even be using a TWebBrowser to run the javascript at all. Run the javascript directly, such as with Windows Script Host (the IActiveScript interface), cscript.exe, or other scripting engine. It is wasteful to use a visual HTML component to perform non-visual script work by itself. Commented Jun 9, 2016 at 16:31
  • @MartynA It's like i said, i'd like to get the console output using my Delphi App. Commented Jun 9, 2016 at 17:18
  • @RemyLebeau I need to run the script in a webpage, that's why i use TWebbrowser. The case in the question is just an example. Commented Jun 9, 2016 at 17:22
  • @delphirules your example is navigating the browser to a javascript: url that has an embedded script. You are not calling a script on a HTML page, so TWebBrowser is the wrong tool to use in your example. If you are doing something different in your real code, you should have shown that instead. Commented Jun 9, 2016 at 17:44

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much Remy ! I lost the count about how many times you helped me not only here but in lot of other sources like the Embarcadero forums. Please take my warm THANK YOU VERY MUCH for such a great work you provide to the Delphi community. You rock !
Yes, I'm obliged to Remy, too, for editing my answer here (and for many other things,too!).
I tried to compile the above code (from James) but got an error on line 152 on @CGID_MSHTML about difference between formal and actual parameters. Compiling with Delphi 10.3.3. Rio.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.