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.

Call Delphi function

Post Reply
giovani.erthal
Posts: 12
Joined: Mon Nov 18, 2019 9:23 pm

Call Delphi function

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

Re: Call Delphi function

Post 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.
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: Call Delphi function

Post 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);
giovani.erthal
Posts: 12
Joined: Mon Nov 18, 2019 9:23 pm

Re: Call Delphi function

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