Jump to content
sail2000

how can i handle the javascript result?

Recommended Posts

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?

ScreenSnap_20221226153853267.thumb.png.3d0a0a526bfb02a4c28bd0d105eef22b.png

test_javascript.zip

Share this post


Link to post

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

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
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!

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×