sail2000 1 Posted December 26, 2022 I use delphi 11.2 in my ANDROID app, i must handling a javascript function's result, it's convert javasciprt to pascal is very hard to me. my way: run javascript in TWebrowser and get result, js result is OK, but, how can i get return value from TWebrowser? My thoughts: 1,execute js throught some library eg: ScriptGate? ScriptGate run javascript Error in delphi 11 . 2,modify TWebrowserm, add javascript callback feature? test_javascript.zip Share this post Link to post
C2H 1 Posted December 26, 2022 You can get the WebBrowser's document content with this code : sValue := (WebBrowser.DefaultInterface.Document as IHTMLDocument3).documentElement.outerText; Share this post Link to post
sail2000 1 Posted December 27, 2022 11 hours ago, C2H said: You can get the WebBrowser's document content with this code : sValue := (WebBrowser.DefaultInterface.Document as IHTMLDocument3).documentElement.outerText; seems IHTMLDocument3 inteface work on windows only. my code will run on android. thanks. Share this post Link to post
Dave Nottage 557 Posted December 27, 2022 This could be one way: unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.WebBrowser, FMX.Controls.Presentation, FMX.StdCtrls, FMX.Edit, Androidapi.JNIBridge, Androidapi.JNI.WebKit, Androidapi.JNI.JavaTypes; type TJavaScriptResultEvent = procedure(Sender: TObject; const JavaScriptResult: string) of object; TJavaScriptValueCallback = class(TJavaLocal, JValueCallback) private FOnResult: TJavaScriptResultEvent; public { JValueCallback } procedure onReceiveValue(value: JObject); cdecl; public property OnResult: TJavaScriptResultEvent read FOnResult write FOnResult; end; TForm1 = class(TForm) WebBrowser: TWebBrowser; Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); private FJavaScriptValueCallback: TJavaScriptValueCallback; procedure NewDateResultHandler(Sender: TObject; const AJavaScriptResult: string); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; end; var Form1: TForm1; implementation {$R *.fmx} uses Androidapi.Helpers; { TJavaScriptValueCallback } procedure TJavaScriptValueCallback.onReceiveValue(value: JObject); begin if Assigned(FOnResult) then FOnResult(Self, JStringToString(TJString.Wrap(value)).DeQuotedString('"')); end; { TForm1 } constructor TForm1.Create(AOwner: TComponent); begin inherited; FJavaScriptValueCallback := TJavaScriptValueCallback.Create; end; destructor TForm1.Destroy; begin FJavaScriptValueCallback.Free; inherited; end; procedure TForm1.NewDateResultHandler(Sender: TObject; const AJavaScriptResult: string); begin Edit1.Text := AJavaScriptResult; end; procedure TForm1.Button1Click(Sender: TObject); var LWebView: JWebView; begin if Supports(WebBrowser, JWebView, LWebView) then begin FJavaScriptValueCallback.OnResult := NewDateResultHandler; LWebView.evaluateJavascript(StringToJString('new Date()'), FJavaScriptValueCallback); end; end; end. Share this post Link to post
sail2000 1 Posted December 27, 2022 2 hours ago, Dave Nottage said: This could be one way: unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.WebBrowser, FMX.Controls.Presentation, FMX.StdCtrls, FMX.Edit, Androidapi.JNIBridge, Androidapi.JNI.WebKit, Androidapi.JNI.JavaTypes; type TJavaScriptResultEvent = procedure(Sender: TObject; const JavaScriptResult: string) of object; TJavaScriptValueCallback = class(TJavaLocal, JValueCallback) private FOnResult: TJavaScriptResultEvent; public { JValueCallback } procedure onReceiveValue(value: JObject); cdecl; public property OnResult: TJavaScriptResultEvent read FOnResult write FOnResult; end; TForm1 = class(TForm) WebBrowser: TWebBrowser; Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); private FJavaScriptValueCallback: TJavaScriptValueCallback; procedure NewDateResultHandler(Sender: TObject; const AJavaScriptResult: string); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; end; var Form1: TForm1; implementation {$R *.fmx} uses Androidapi.Helpers; { TJavaScriptValueCallback } procedure TJavaScriptValueCallback.onReceiveValue(value: JObject); begin if Assigned(FOnResult) then FOnResult(Self, JStringToString(TJString.Wrap(value)).DeQuotedString('"')); end; { TForm1 } constructor TForm1.Create(AOwner: TComponent); begin inherited; FJavaScriptValueCallback := TJavaScriptValueCallback.Create; end; destructor TForm1.Destroy; begin FJavaScriptValueCallback.Free; inherited; end; procedure TForm1.NewDateResultHandler(Sender: TObject; const AJavaScriptResult: string); begin Edit1.Text := AJavaScriptResult; end; procedure TForm1.Button1Click(Sender: TObject); var LWebView: JWebView; begin if Supports(WebBrowser, JWebView, LWebView) then begin FJavaScriptValueCallback.OnResult := NewDateResultHandler; LWebView.evaluateJavascript(StringToJString('new Date()'), FJavaScriptValueCallback); end; end; end. code is perfect. Thank you so much! 1 Share this post Link to post