在 FireMonkey 使用 TWebBrowser 调用 Javascript函数并获取返回值以及 JavaScript 中调 Delphi 的函数/过程,普遍都在使用老掉牙的URL重定的方法,还要改 FMX 的源码,相当繁琐。
现在使用 ScriptGate 可轻易解决这个问题,ScriptGate 支持 Windows, macOS, Android, iOS,非常好用,强烈推荐。
项目地址:https://bitbucket.org/freeonterminate/scriptgate
用法如下:
HTML / JavaScript:
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;
// 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;
// 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;
// It is a method published in JavaScript and is called from JavaScript.
procedure TForm1.HelloDelphi;
begin
ShowMessage('Hello, Delphi!');
end;
|
请发表评论