Page 1 of 2

Return function JavaScrit in Delphi

Posted: Thu Apr 09, 2020 6:13 pm
by EdsonAlves
Hello,

What would be the best using CEF4 to capture the return of a javascript function executed on Chromium:

For example:

function sum (a, b) {
return a + b
}

what better way to get that return in Delphi?

Re: Return function JavaScrit in Delphi

Posted: Fri Apr 10, 2020 6:39 am
by salvadordf
Hi,

Register a JavaScript extension to call custom functions that execute Delphi code. That code can send a process message to send any information to the browser process.

The JSExtension demo shows how to do all that. Read the code comments for all the details.

Re: Return function JavaScrit in Delphi

Posted: Sat Apr 11, 2020 1:13 am
by zavet
you can use Chromium1ConsoleMessage

To output a message to the console on JS:
Form1.Chromium1.Browser.MainFrame.ExecuteJavaScript('var body =(myText);console.log("text="+body);', 'about:blank', 0);

Re: Return function JavaScrit in Delphi

Posted: Sat Apr 11, 2020 9:40 pm
by EdsonAlves
zavet wrote: Sat Apr 11, 2020 1:13 am you can use Chromium1ConsoleMessage

To output a message to the console on JS:
Form1.Chromium1.Browser.MainFrame.ExecuteJavaScript('var body =(myText);console.log("text="+body);', 'about:blank', 0);
Yes, but I would not like to use the console, because it is running all the time in the application, I wanted something only when it was really the return of a js function

Re: Return function JavaScrit in Delphi

Posted: Sun Apr 12, 2020 12:06 pm
by ForumReader
You can simply call JS: Alert( needed_result )
And intercept (and supress) this result in Jsdialog event :)

Re: Return function JavaScrit in Delphi

Posted: Sun Apr 12, 2020 12:59 pm
by salvadordf
ForumReader wrote: Sun Apr 12, 2020 12:06 pm You can simply call JS: Alert( needed_result )
And intercept (and supress) this result in Jsdialog event :)
Nice trick! :D

Re: Return function JavaScrit in Delphi

Posted: Mon Apr 13, 2020 4:43 pm
by shobits1
salvadordf wrote: Sun Apr 12, 2020 12:59 pm
ForumReader wrote: Sun Apr 12, 2020 12:06 pm You can simply call JS: Alert( needed_result )
And intercept (and supress) this result in Jsdialog event :)
Nice trick! :D
what's the text size limit ?? if you know :)

Re: Return function JavaScrit in Delphi

Posted: Mon Apr 13, 2020 4:58 pm
by salvadordf
shobits1 wrote: Mon Apr 13, 2020 4:43 pm
salvadordf wrote: Sun Apr 12, 2020 12:59 pm
ForumReader wrote: Sun Apr 12, 2020 12:06 pm You can simply call JS: Alert( needed_result )
And intercept (and supress) this result in Jsdialog event :)
Nice trick! :D
what's the text size limit ?? if you know :)
CEF4Delphi has the same limits as the Delphi strings (up to 2GB if you use unicode strings) but I don't know if CEF or Chromium has some other limit.

Re: Return function JavaScrit in Delphi

Posted: Mon Apr 13, 2020 5:15 pm
by shobits1
OK, thanks

I'll test it when I have time, it may make my code simpler instead of using callbacks from jsExtension, although I don't know how much it'll affect the CEF performance.

Re: Return function JavaScrit in Delphi

Posted: Tue Apr 14, 2020 5:36 pm
by shobits1
Limits for JS alert text is 10240byte

Code: Select all

let s = ''
for( i = 0; i < 1000*1000; i++){
    s += '.';
}
// in browser console
// s.length = 1,000,000

alert(s);

// delphi received  messageText  length = 10,240

Limit for JS prompt

Code: Select all

let s = '';
let s100 = '';
for( i = 0; i < 100; i++){
    s100 += '.';
}

for( i = 0; i < 1000*1000; i++){
    s += s100;
}

// in browser console
// s.length = 100,000,000

prompt(s, s);

// delphi received  messageText        length = 10,240
//                  defaultPromptText  length = 100,000,000
so alert has a limit to about 10KByte, prompt has the same limit on message(messageText), but no limit on value (defaultPromptText) tested up to 200,000,000 in length.