Scriptgate is a library that implements the mutual invocation of JavaScript and Delphi code on Twebbrowser, specifically here: https://bitbucket.org/freeonterminate/scriptgate
With Scriptgate, we can easily implement JavaScript on the use of Delphi, currently support the latest Delphi Tokyo 10.2.3 version, note that the FMX type of control, the author said that support Windows,macos, Android and iOS.
What am I going to do?
For example, you can call the following JavaScript methods from Delphi.
function hellojs () { alert ( "Hello,javascript! "); } </script>
procedure Tform1.button1click (sender:tobject); begin fscriptgate.callscript ('hellojs ()'); End;
See, in Delphi, call JavaScript's hellojs.
Instead, look at the Delphi method called from JavaScript:
procedure Tform1.hellodelphi () begin showmessage ('Hello, delphi! ' ); End;
<body> <a href= "Delphi:Hellodelphi ()" >call delphi</a></body>
Note here that the "Delphi:" String, is the Tscriptgate constructor specified by the scheme, do not understand, then continue to look down.
Try using Scriptgate
Preparatory work
First, download the code from BitBucket.
Next, in the new "multi-Device Application", select "Empty Application".
* You can, of course, merge it into an existing project, but only for the Firemonkey app project.
Add the downloaded and unzipped code directory to the search path.
If Android is included in the development goal, add Sgwebclient.jar to the Android library.
At this point, the preparation is complete.
Establish Tscriptgate
Here we use Scriptgate with Webbrowser1,tform1.
UnitUnit1;Interfaceuses {...}SG. Scriptgate; //manually referencing this unittypeTForm1=class(tform) Webbrowser1:twebbrowser; Layout1:tlayout; Button1:tbutton; procedureformcreate (Sender:tobject); procedureButton1Click (Sender:tobject); Private varfscriptgate:tscriptgate;//Add a Fscriptgate object Public procedureHellodelphi;//Add a Delphi method that is ready to be called in JS End;
Using Form1 's OnCreate event, a Tscriptgate instance object is built first Fscriptgate
Implementation procedure tform1.formcreate (sender:tobject); begin fscriptgate:= tscriptgate. Create ' Delphi ' ); End;
Tscriptgate.create Prototypes:
Constructor Create ( const ireceiver:tobject; Const Iwebbrowser:twebbrowser; Const reintroduce;
Ireceiver is an instance of receiving a response from JavaScript.
Here TForm1 is specified, but it can be any instance of the class.
The method of the instance specified here can be called by JavaScript.
Such as:
type classpublic procedure Foo; procedure Bar (const msg:string); End;
If you specify an instance of a class named Treceiver , you can call the bar method from JavaScript.
It is important to note that the access level of the methods exposed to JavaScript must be public or higher.
It uses Rtti for method lookups.
Specifies an instance of the Twebbrowser associated with the tscriptgate.
You can invoke the JavaScript method that specifies WebBrowser loading.
Call the first example to append the Ischeme Scriptgate method to the "Delphi:" Parameter.
This has the same meaning as the "javascript:" specified when using JavaScript or "file:" when specifying local files.
No matter what you specify here, you must also specify the same string on the JavaScript side.
Note that the string passed to the parameter does not require a colon ":".
Then write a method that calls JavaScript and a method called from JavaScript.
// call Hellojs () in JavaScript. // You can also use anonymous functions to retrieve the return value.
Procedure Tform1.button1click (sender:tobject); begin Fscriptgate.callscript ( ' Hellojs () ', procedure ( Const iresult:string) begin ShowMessage (iresult); End ); end;
//Allow JavaScript calls methods, and the methods in JavaScript are called Hellodelphi.
Procedure Tform1.hellodelphi;begin showmessage (' Hello, delphi! '); End
The HTML that is loaded into WebBrowser1 is shown below.
<HTML> <Header> <Scripttype= "Text/javascript"> functionHellojs () {alert ("Hello, javascript!."); return "Hello !!"; } </Script> </Head> <Body> <BR><BR> <ahref= "Delphi:hellodelphi ()">Call Delphi procedure</a> <Body> </HTML>;
The following is a complete example written by the author, demonstrating how Delphi and JavaScript call each other and how parameters are passed when called.
UnitUnit1;Interfaceusessystem.sysutils, System.types, System.uitypes, system.classes, System.variants, FMX. Types, FMX. Controls, FMX. Forms, FMX. Graphics, FMX. Dialogs, FMX. Controls.presentation, Fmx.stdctrls, FMX. Layouts, FMX. WebBrowser, SG. Scriptgate;typeTForm1=class(tform) Webbrowser1:twebbrowser; Layout1:tlayout; Button1:tbutton; Layout2:tlayout; Button2:tbutton; procedureformcreate (Sender:tobject); procedureButton1Click (Sender:tobject); procedureButton2click (Sender:tobject); Private varfscriptgate:tscriptgate; Public procedureHellodelphi (Constistr:string); procedureADD (Constmsg:string;ConstA, B:integer); End;varForm1:tform1;Implementation{$R *.FMX}Constsamplehtml=''+''+'<script type= "Text/javascript" >'+'function Hellojs (msg, MSG2) {alert (msg + MSG2); return "Hello Delphi! I am JavaScript! ";}'+//Call from Delphi '</script>'+''+'<body>'+'<br><br>'+//Call Delphi Method '<a href= "Yourorgscheme:hellodelphi (' Call by JS ')" >call Delphi noparam procedure</a>'+'<br><br>'+'<a href= "Yourorgscheme:add (' calc:30 + + = '," +) ">call Delphi procedure</a>'+'<body>'+'';proceduretform1.formcreate (sender:tobject);beginwebbrowser1.loadfromstrings (samplehtml,'/'); //The method of the object specified by the first argument is //called from JavaScript.Fscriptgate: = Tscriptgate.Create(Self, WebBrowser1,'Yourorgscheme');End;procedureTform1.button1click (sender:tobject);beginFscriptgate.callscript ('Hellojs ("Hello JS!", "I am Delphi!")', procedure(Constiresult:string)beginShowMessage (Iresult); End );End;procedureTform1.button2click (sender:tobject);beginFscriptgate.eval ('document.getelementsbytagname ("html") [0].outerhtml', procedure(Constiresult:string)beginShowMessage (Iresult); End );End;procedureTform1.add (Constmsg:string;ConstA, B:integer);beginShowMessage (MSG+ (A +B). ToString);End;procedureTform1.hellodelphi (Constistr:string);beginShowMessage ('Hello!'+iStr);End;End.
Open Source Project Scriptgate,delphi the artifact that calls each other with JavaScript