How to embed a Delphi VCL form in an HTML page using NPAPI?

There are known ways to write ActiveX plugins with Delphi, but ActiveX itself creates many restrictions in browsers other than IE. So, I was thinking - how to compile the plugin in NPAPI format compatible with Chrome / Firefox?

The intent of the plugin is to allow embedding the VCL form in the HTML page and to be able to bidirectionally communicate with this form using JavaScript. For instance. clicking the button on the form will trigger the JavaScript function on the page, and the JavaScript functions on the page can send events to the VCL form. How can this be achieved?

0
source share
1 answer

Here is a list of existing NPAPI shells for Delphi in the Mozilla browser: https://www.mozdev.org/bugs/show_bug.cgi?id=8708

The last entry (NPAPI plugin with scripting support + demonstration by Yuri Sidorov) offers exactly what is needed.

This VCL Form project can be compiled into an NPAPI compatible DLL. Manifest.json also needs to be added. After that, the plugin can be installed in Chrome, as usual.

The following HTML embeds the VCL form that is stored in the plugin:

<EMBED id="embed1" TYPE="application/x-delphi-demo-plugin" ALIGN=CENTER WIDTH=400 HEIGHT=300> <script> var embed1 = document.getElementById('embed1'); </script> <input type=button value="Show Value" onclick='alert("Value=" + embed1.value);'> 

And this is how a form can change the HTML page around it:

 with Plugin.GetBrowserWindowObject do GetObject('document')['bgColor'] := clRed; 

PS The only fix that should be applied for modern versions of Delphi is change string and PChar to AnsiString and PAnsiChar throughout NPPlugin.pas . Or the connection with the embedded form is broken.

+1
source

Source: https://habr.com/ru/post/911789/


All Articles