Page 1 of 1

How to read variable created in global context?

Posted: Wed Jan 24, 2018 6:16 pm
by michal@dorfin.waw.pl
I'm creating a variable like this:
var globalContext : ICefV8Value = nil; // global variable

procedure GlobalCEFApp_OnContextCreated(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context);
var
str : ICefV8Value;
begin
globalContext:=context.Global;
str:=TCefv8ValueRef.NewString('Hello world!');
globalContext.SetValueByKey('myval',str,V8_PROPERTY_ATTRIBUTE_NONE);
end;


When I open DevTools and in Console check "myval" variable I got "Hello world!" string.
That means that "myval" was created succesfully.

Than I try to read this value inside Delphi app this way:

function myFunc;
var myval : ICEFv8Value;
begin
myval:=globalContext.GetValueByKey('myval');
// the "myval" is nil !
end;


Unfortunaelly I got "nil". Of course "myFunc" is fired after the variable "myval" is created (in Console I can read it).
What is wrong ?

Re: How to read variable created in global context?

Posted: Wed Jan 24, 2018 7:53 pm
by salvadordf
CEF3 executes GlobalCEFApp.OnContextCreated in the render process and the globalContext global variable is assigned in that process.
If you try to use the globalContext variable from the browser process you will have problems.
You can't access variables or components from different processes.

It's recommended that you create local variables to copy interfaces because they will be released when they are out of scope. Global variables with interfaces might give you problems if you don't set them to nil before the application closes.

If you need to send information from javascript to delphi you can use the JSExtension demo.
Run that demo, right click over a web page and select "Visit DOM in JavaScript".
You will see a new window with the HTML code sent from JavaScript.

Re: How to read variable created in global context?

Posted: Wed Jan 24, 2018 8:10 pm
by michal@dorfin.waw.pl
I thought that I one can define own variable - than it can be accessed inside the code in easy way.
Thank you.