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



Call Delphi procedure

;函數

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;code

// 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;orm

// 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;htm

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

相關文章
相關標籤/搜索