delphi ScriptGate 調用JS

在 FireMonkey 使用 TWebBrowser 調用 Javascript函數並獲取返回值以及 JavaScript 中調 Delphi 的函數/過程,廣泛都在使用老掉牙的URL重定的方法,還要改 FMX 的源碼,至關繁瑣。html

如今使用 ScriptGate 可輕易解決這個問題,ScriptGate  支持 Windows, macOS, Android, iOS,很是好用,強烈推薦。

項目地址:https://bitbucket.org/freeonterminate/scriptgate

 用法以下:

HTML / JavaScript:ide

<html> <header> <script type="text/JavaScript"> function helloJS() { alert("Hello, JavaScript!"); return "Hello !!"; } </script> </head> 函數

<body> <br><br> <a href="delphi:HelloDelphi()">Call Delphi procedure</a> <body> </html>;code

Delphi: procedure TForm1.FormCreate(Sender: TObject); begin // Binding ScriptGate to WebBrowser and setting the scheme delphi // The scheme is also specified on the JavaScript side // Same as file :, JavaScript: etc. ScriptGate := TScriptGate.Create(Self, WebBrowser1, 'delphi'); end;orm

// Call helloJS () JavaScript. // You can also retrieve the return value using an anonymous function. procedure TForm1.Button1Click(Sender: TObject); begin FScriptGate.CallScript( 'helloJS()', procedure(const iResult: String) begin ShowMessage(iResult); // Show return value end ); end;htm

// Execute arbitrary JavaScript // You can also retrieve the return value using an anonymous function. procedure TForm1.Button1Click(Sender: TObject); begin FScriptGate.Eval( 'document.getElementsByTagName("html")[0].outerHTML', procedure(const iResult: String) begin ShowMessage(iResult); // Show return value end ); end;ip

// It is a method published in JavaScript and is called from JavaScript. procedure TForm1.HelloDelphi; begin ShowMessage('Hello, Delphi!'); end;ci

相關文章
相關標籤/搜索