Page 1 of 1

Call Delphi function

Posted: Tue Mar 10, 2020 6:08 pm
by giovani.erthal
Is it possible to call a Delphi function with JavaScript?

With a quick search, I found that for security reasons, it is not possible to control the mouse with JavaScript. So I need an "alternative solution".

The alternative solution would be to execute the function Chromium1.Browser.Host.SendMouseClickEvent (I also need to pass the coordinates as a parameter), but I don't know how to call this function through JS code.

I can do this by sending messages to the console (and filtering the messages in the OnConsoleMessage event), but that doesn't seem like a good solution.

Code: Select all

console.log ('SendMouseClickEvent 10 10');
Looking quickly at the demos, I didn't find what I'm looking for.

Re: Call Delphi function

Posted: Tue Mar 10, 2020 6:20 pm
by salvadordf
Hi,

You need to use a JavaScript extension. JavaScript extensions allow you to add custom JavaScript functions that execute Delphi code.

There are 2 demos inside the "demos\Delphi_VCL\JavaScript" directory that show you how to use them : JSExtension and JSRTTIExtension.

Read the code comments in the JSExtension demo for all the details.

Re: Call Delphi function

Posted: Wed Mar 11, 2020 9:29 am
by dilfich
giovani.erthal wrote: Tue Mar 10, 2020 6:08 pm

Code: Select all

console.log ('SendMouseClickEvent 10 10');
You can immediately script and click, once the coordinates are there.

Code: Select all

function click(x, y)
{
    var ev = new MouseEvent('click', {
        'view': window,
        'bubbles': true,
        'cancelable': true,
        'screenX': x,
        'screenY': y
    });

    var el = document.elementFromPoint(x, y);
    el.dispatchEvent(ev);
}

click(10, 10);

Re: Call Delphi function

Posted: Wed Mar 11, 2020 11:57 am
by giovani.erthal
salvadordf wrote: Tue Mar 10, 2020 6:20 pm Hi,

You need to use a JavaScript extension. JavaScript extensions allow you to add custom JavaScript functions that execute Delphi code.

There are 2 demos inside the "demos\Delphi_VCL\JavaScript" directory that show you how to use them : JSExtension and JSRTTIExtension.

Read the code comments in the JSExtension demo for all the details.
Sorry, I had not verified this demo.

That's exactly what I need!
dilfich wrote: Wed Mar 11, 2020 9:29 am
giovani.erthal wrote: Tue Mar 10, 2020 6:08 pm

Code: Select all

console.log ('SendMouseClickEvent 10 10');
You can immediately script and click, once the coordinates are there.

Code: Select all

function click(x, y)
{
    var ev = new MouseEvent('click', {
        'view': window,
        'bubbles': true,
        'cancelable': true,
        'screenX': x,
        'screenY': y
    });

    var el = document.elementFromPoint(x, y);
    el.dispatchEvent(ev);
}

click(10, 10);
I tried, this was my first alternative.

However, the object I need to click on is a <canvas>, and even passing the coordinates, it doesn't work. So I need this function.

Thanks!