0

enter image description here

In Delphi , With below code I can send data to Javascript and it work well , but how can send data from Javascript to Delphi ?

first , I Use below code in Javascript but did not work :

JS_DELPHI._geta() ;

even below code did not work :

TMyExtension._geta() ;

I think my code in Delphi have not some essential code.

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.Controls.Presentation, FMX.StdCtrls, ceffmx, FMX.Edit ,   ceflib ,
   uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes, uCEFListValue, uCEFBrowser, uCEFFrame, uCEFRequest,
  uCEFv8Context, uCEFv8Exception, uCEFv8StackTrace, uCEFDomNode, uCEFProcessMessage, uCEFApplicationCore;

type
  TForm1 = class(TForm)
    ChromiumFMX1: TChromiumFMX;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;


   type
//这里建议用class  不建议用class(TThread)  不然有些地方要报错
TMyExtension = class(TThread) // or just class, (extension code execute in thread)
  public
  class function _geta:string;
end;

TCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
protected
    procedure OnWebKitInitialized; override;
end;


var
  Form1: TForm1;
  d : Integer   ;
  m : TMyExtension ;
  CefRenderProcessHandler : TCustomRenderProcessHandler ;

implementation

{$R *.fmx}
{$R *.LgXhdpiPh.fmx ANDROID}
{$R *.XLgXhdpiTb.fmx ANDROID}
{$R *.SSW3.fmx ANDROID}

procedure TForm1.Button1Click(Sender: TObject);
begin
ChromiumFMX1.Load('http://localhost/index.html');

 Edit1.Text := '555 ';

  ShowMessage('hiiiiiiiiiiiiiiiii');
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
ChromiumFMX1.Browser.MainFrame.ExecuteJavaScript(' d1();',ChromiumFMX1.browser.MainFrame.GetURL, 0);
end;


class function TMyExtension._geta: string;
begin
ShowMessage('dddddddddddddddddddddddd');
  Result:='salam';
end;

procedure TCustomRenderProcessHandler.OnWebKitInitialized;

begin
    {$IFDEF DELPHI14_UP}
    TCefRTTIExtension.Register('JS_DELPHI', TMyExtension);
  {$ENDIF}
end;



initialization
  CefRemoteDebuggingPort := 9000;
  CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
  CefBrowserProcessHandler := TCefBrowserProcessHandlerOwn.Create;
end.

end.
2
  • 1
    This question was answered here : stackoverflow.com/questions/64303829/… Commented Jul 17, 2022 at 19:41
  • If you need a quick solution use console messages to send information from JavaScript to Delphi. Call "console.log()" from JavaScript and use the TChromium.OnConsoleMessage event to get the information. All the code is in the DOMVisitor demo. Commented Jul 18, 2022 at 9:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.