Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Return function JavaScrit in Delphi

EdsonAlves
Posts: 7
Joined: Sun Jan 28, 2018 12:38 pm

Return function JavaScrit in Delphi

Post 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?
User avatar
salvadordf
Posts: 4575
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Return function JavaScrit in Delphi

Post 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.
zavet
Posts: 4
Joined: Sat Apr 04, 2020 2:09 am

Re: Return function JavaScrit in Delphi

Post 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);
EdsonAlves
Posts: 7
Joined: Sun Jan 28, 2018 12:38 pm

Re: Return function JavaScrit in Delphi

Post 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
ForumReader
Posts: 8
Joined: Wed Mar 18, 2020 4:18 am

Re: Return function JavaScrit in Delphi

Post by ForumReader »

You can simply call JS: Alert( needed_result )
And intercept (and supress) this result in Jsdialog event :)
User avatar
salvadordf
Posts: 4575
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Return function JavaScrit in Delphi

Post 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
shobits1
Posts: 25
Joined: Wed Mar 04, 2020 9:16 pm

Re: Return function JavaScrit in Delphi

Post 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 :)
User avatar
salvadordf
Posts: 4575
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Return function JavaScrit in Delphi

Post 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.
shobits1
Posts: 25
Joined: Wed Mar 04, 2020 9:16 pm

Re: Return function JavaScrit in Delphi

Post 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.
shobits1
Posts: 25
Joined: Wed Mar 04, 2020 9:16 pm

Re: Return function JavaScrit in Delphi

Post 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.
Post Reply