Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
global variant in delphi function registered in js
global variant in delphi function registered in js
why js cannot obtain new value of global variant?
click button2 always show '' even button1 is clicked;
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.ComCtrls,Generics.Collections,
System.StrUtils, Masks,
uCEFWindowParent, uCEFChromium, uCEFConstants, uCEFApplication, uCEFInterfaces,
uCEFTypes, uCEFProcessMessage, uCEFMiscFunctions, uCEFSchemeRegistrar, uCEFRenderProcessHandler,
uCEFv8Handler, uCEFDomVisitor, uCEFDomNode, uCEFTask;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
CEFWindowParent1: TCEFWindowParent;
Chromium1: TChromium;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
myvariant:string;
procedure CreateGlobalCEFApp; //initialize
implementation
uses unit2, uCEFStringMultimap, DateUtils; // TCefStringMultimapOwn : uCEFStringMultimap
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
myvariant:='Unita1';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//
Chromium1.Browser.MainFrame.ExecuteJavaScript('TMyExtension.StringNameToVariant("1")', Chromium1.browser.MainFrame.GetURL (), 0 );
end;
procedure GlobalCEFApp_OnWebKitInitialized;
begin
// Registering the extension. Read this document for more details :
// https://bitbucket.org/chromiumembedded/ ... gration.md
TCefRTTIExtension.Register('MyJavaExtension', TMyExtension);
end;
procedure CreateGlobalCEFApp; //use , uCEFApplication
begin
GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.OnWebKitInitialized := GlobalCEFApp_OnWebKitInitialized;
end;
end.
----------
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TMyExtension = class
class function StringNameToVariant(const AStringName : string): string;
end;
implementation
uses unit1;
class function TMyExtension.StringNameToVariant(const AStringName : string): string;
begin
Result:=myvariant;
showmessage(myvariant); //not 'Unita1' but ''(beginning value)
end;
end.
click button2 always show '' even button1 is clicked;
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.ComCtrls,Generics.Collections,
System.StrUtils, Masks,
uCEFWindowParent, uCEFChromium, uCEFConstants, uCEFApplication, uCEFInterfaces,
uCEFTypes, uCEFProcessMessage, uCEFMiscFunctions, uCEFSchemeRegistrar, uCEFRenderProcessHandler,
uCEFv8Handler, uCEFDomVisitor, uCEFDomNode, uCEFTask;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
CEFWindowParent1: TCEFWindowParent;
Chromium1: TChromium;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
myvariant:string;
procedure CreateGlobalCEFApp; //initialize
implementation
uses unit2, uCEFStringMultimap, DateUtils; // TCefStringMultimapOwn : uCEFStringMultimap
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
myvariant:='Unita1';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//
Chromium1.Browser.MainFrame.ExecuteJavaScript('TMyExtension.StringNameToVariant("1")', Chromium1.browser.MainFrame.GetURL (), 0 );
end;
procedure GlobalCEFApp_OnWebKitInitialized;
begin
// Registering the extension. Read this document for more details :
// https://bitbucket.org/chromiumembedded/ ... gration.md
TCefRTTIExtension.Register('MyJavaExtension', TMyExtension);
end;
procedure CreateGlobalCEFApp; //use , uCEFApplication
begin
GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.OnWebKitInitialized := GlobalCEFApp_OnWebKitInitialized;
end;
end.
----------
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TMyExtension = class
class function StringNameToVariant(const AStringName : string): string;
end;
implementation
uses unit1;
class function TMyExtension.StringNameToVariant(const AStringName : string): string;
begin
Result:=myvariant;
showmessage(myvariant); //not 'Unita1' but ''(beginning value)
end;
end.
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: global variant in delphi function registered in js
Hi,
myvariant is declared in Unit1 which is used in the browser process but JavaScript and the extension is executed in the render process.
Read this for more information about the multi-process architecture in Chromium :
http://www.chromium.org/developers/desi ... chitecture
https://bitbucket.org/chromiumembedded/ ... ntegration
myvariant is declared in Unit1 which is used in the browser process but JavaScript and the extension is executed in the render process.
Read this for more information about the multi-process architecture in Chromium :
http://www.chromium.org/developers/desi ... chitecture
https://bitbucket.org/chromiumembedded/ ... ntegration
Re: global variant in delphi function registered in js
Thank you very much!
If it is possible to use global variant in is situation? use message?(but variant name may be different)
If it is possible to use global variant in is situation? use message?(but variant name may be different)
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: global variant in delphi function registered in js
It's not possible to use a variable declared in a different process. You will read different values.coater wrote: Fri May 10, 2019 2:48 am Thank you very much!
If it is possible to use global variant in is situation? use message?(but variant name may be different)
If you need to share some information you can :
- Set a value in TCefProcessMessageRef.ArgumentList and send that message to another process. Several demos use those arguments to pass information between processes but there's a limit of a few KB in size https://github.com/salvadordf/CEF4Delph ... l.pas#L379
- Use a database protected by a mutex.
- Use WebSockets or any other way to send custom IPC messages
Re: global variant in delphi function registered in js
Thank you very much for your kind help!
If I use message to obtain value of the variant, How can I wait and continue until the message was send back(so I can obtain the value of variant)?
keen for your help, thank you!
If I use message to obtain value of the variant, How can I wait and continue until the message was send back(so I can obtain the value of variant)?
keen for your help, thank you!
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: global variant in delphi function registered in js
If you use a message then you need to split your work in several pieces. Each piece would do a part of the work, send a message to the other process and the other process would reply with another message or executing some other JavaScript function.
If your app can't split the work in several phases easily then use a database protected by a mutex.
If your app can't split the work in several phases easily then use a database protected by a mutex.
Re: global variant in delphi function registered in js
Thank you very much!
Hope there would be some examples in the future, thank you!
Hope there would be some examples in the future, thank you!
Re: global variant in delphi function registered in js
1. glbal variant:
var
hMutex:THandle; //
2.
procedure TFrm.FormCreate(Sender: TObject);
begin
hMutex:=CreateMutex(nil,false,'Myjs'); //success
end;
3.
javaextension:
var i:integer;
begin
Result:=inttostr(hMutex); //show '0' after run 'alert(MyJavaExtension.StrV('txt'))'
end;
var
hMutex:THandle; //
2.
procedure TFrm.FormCreate(Sender: TObject);
begin
hMutex:=CreateMutex(nil,false,'Myjs'); //success
end;
3.
javaextension:
4.class function TMyJavaExtension.StrV(const AStringName : string): string;class function TMyJavaExtension.FileExistsByJs(const Afile : string): boolean;
var i:integer;
begin
Result:=false;
showmessage(inttostr( WaitForSingleObject(hMutex,INFINITE)));// no message, ----but old cef4 showed message
if WaitForSingleObject(hMutex,INFINITE)=WAIT_OBJECT_0 then
begin
Result:=True;
ReleaseMutex(hMutex);
end else Result:=False;
end;
var i:integer;
begin
Result:=inttostr(hMutex); //show '0' after run 'alert(MyJavaExtension.StrV('txt'))'
end;
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: global variant in delphi function registered in js
Use process messages to send information between the browser and render processes.
The DOMVisitor, JSRTTIExtension and JSExtension demos show you how to send those messages.
The DOMVisitor, JSRTTIExtension and JSExtension demos show you how to send those messages.
Re: global variant in delphi function registered in js

I think out another way:
Before the use of the function:
var a js variant and set it's value befor using the function.

Why new cef4 does't show dialog by showmessage() in jsextention procedure? but old can