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.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

How to read variable created in global context?

Post Reply
michal@dorfin.waw.pl
Posts: 41
Joined: Sun Feb 05, 2017 8:53 am

How to read variable created in global context?

Post 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 ?
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to read variable created in global context?

Post 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.
michal@dorfin.waw.pl
Posts: 41
Joined: Sun Feb 05, 2017 8:53 am

Re: How to read variable created in global context?

Post 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.
Post Reply