Lazarus

Programming => Networking and Web Programming => Topic started by: Nicole on June 16, 2025, 05:32:02 pm

Title: CEF component - the first step
Post by: Nicole on June 16, 2025, 05:32:02 pm
I read a lot and found hundreds of links. They shoot me, but do not help me.

in this tread
https://forum.lazarus.freepascal.org/index.php?topic=70684.0
it is spoken about "demo minibrowser", - whcih i cannot find.

At them moment I have installed CEF from the OPm, created a button doing:
Code: Pascal  [Select][+][-]
  1.  if not ChromiumWindow1.Initialized then
  2.    ChromiumWindow1.CreateBrowser;
  3.  
  4.  if Assigned(ChromiumWindow1.ChromiumBrowser) then
  5.    ChromiumWindow1.LoadURL('https://www.google.com');  

all four lines are jumped to, but I cannot see anything. Not even a window, where a browser could be.

I am sure, I miss the most important thing. Please can anybody tell me, what it is?
Those tutorials offer loads of options and frameworks and things.
I think, it can be done without it. In my old Delphi version, there just was a click and the browser was there. No other stuff needed. I am sure, there is a way Lazarus does this too.

Title: Re: CEF component - the first step
Post by: n7800 on June 16, 2025, 08:38:09 pm
it is spoken about "demo minibrowser", - whcih i cannot find.

The demo projects are in the "CEF4Delphi" folder that you downloaded from OPM. You can also download them directly from the original repository CEF4Delphi (https://github.com/salvadordf/CEF4Delphi), here is the direct link (https://github.com/salvadordf/CEF4Delphi/tree/master/demos/Lazarus_Windows/MiniBrowser) to "MiniBrowser".

The version in the repository may differ slightly from the version in OPM.
Title: Re: CEF component - the first step
Post by: Nicole on June 17, 2025, 10:33:45 am
Thank you so much. I found it all.
Even the explanation I understood partly.

The demos I started claimed dlls. These dll I found, but - they are that huge! All in all more than 1 GB.
Is there a small count of binaries, that I can copy? Or must I give my VM more space? (I want to avoid that).

Title: Re: CEF component - the first step
Post by: loaded on June 17, 2025, 10:49:28 am
Cef needs at least these folders and files to run. (~306 MB)

Folders;
-locales
-swiftshader

Files;
-snapshot_blob.bin
-v8_context_snapshot.bin
-icudtl.dat
-vk_swiftshader_icd.json
-cef_sandbox.lib
-libcef.lib
-chrome_100_percent.pak
-chrome_200_percent.pak
-resources.pak
-chrome_elf.dll
-d3dcompiler_47.dll
-libcef.dll
-libEGL.dll
-libGLESv2.dll
-vk_swiftshader.dll
-vulkan-1.dll
Title: Re: CEF component - the first step
Post by: Nicole on June 17, 2025, 03:48:38 pm
300 MB would just be one third and great.

However, I am not sure, if you are talking about the same folders as those which I have downloaded.
I make a snapshot of what I have. I got these by scrolling down for the binaries and downloading those for windows and unzipping

Title: Re: CEF component - the first step
Post by: loaded on June 17, 2025, 08:02:30 pm
First of all, the CEF4 version I used was 98.2, and the current version is 137.0.17. So a lot of water has passed under the bridge. The CEF4 you downloaded is a complete project file. I downloaded it and took a look at it. You need to distribute the attached folders with your application exe at runtime. You may need to do some trial and error, or if you contact the developer of this project, salvadordf (who is a member of this forum), he will help you.
Title: Re: CEF component - the first step
Post by: n7800 on June 18, 2025, 12:37:13 am
300 MB would just be one third and great.

However, I am not sure, if you are talking about the same folders as those which I have downloaded.
I make a snapshot of what I have. I got these by scrolling down for the binaries and downloading those for windows and unzipping

As you can see, you have "Release" and "Debug" subfolders that contain the corresponding copies of the DLL. To distribute your application, you only need "Release", and for debugging - "Debug" (theoretically, you can also develop with Release).

In fact, I had to write a function in my application so that depending on the build mode, the files from the right subfolder are used. It's a bit complicated ("$IFDEF", setting up build modes), but it makes debugging easier later.

Note that the DLL version (release/debug) and the build mode of your project (which can also have "release"/"debug") are different things.
Title: Re: CEF component - the first step
Post by: Nicole on June 18, 2025, 08:55:54 am
oups! I only understood half of what you said.

My situation: I only develop for myself. What I want: small files. As I err all day long I need many backups.
What do you advice me, how to work?

I never cared about those 2 kinds.

About the dlls: Those Demos give error-messages: .... missing. My plan was, that I will us this error messages to copy the needed dlls one by one.

And another question: My CEF is in the wrong directory. Is there a smart way to change the directory? And: Was it the best way to install from the OPM? Or shall I copy the files from the git? (I am at Windows 7 at the moment)
Title: Re: CEF component - the first step
Post by: Thaddy on June 18, 2025, 11:42:18 am
Well, your files can be made small, but the dependencies will make the whole big.
And in the case of CEF there is no way around that.
Title: Re: CEF component - the first step
Post by: salvadordf on June 18, 2025, 12:40:53 pm
Read this Wiki page :
https://github.com/salvadordf/CEF4Delphi/wiki/Before-you-run-the-demos

It has all the information needed to run the demos and instructions to copy the CEF binaries correctly.

Windows 7 is not supported by Chromium anymore. Read this :
https://github.com/salvadordf/CEF4Delphi/wiki/Frequently-asked-questions
Title: Re: CEF component - the first step
Post by: Nicole on June 18, 2025, 05:18:10 pm
I am puzzled. I clicked link and link and link, - CEF DOES work with Win 7 still or doesn't it?

If the version is from 2022, I can live with it.
Title: Re: CEF component - the first step
Post by: salvadordf on June 18, 2025, 05:55:04 pm
Please, read the last link.

Google decided to drop Windows 7 support in 2022. The last CEF version that worked in Windows 7 was CEF 109.

CEF 110 and newer require Windows 10 or 11.
Title: Re: CEF component - the first step
Post by: wp on June 18, 2025, 05:58:08 pm
Thank you so much. I found it all.
Even the explanation I understood partly.

The demos I started claimed dlls. These dll I found, but - they are that huge! All in all more than 1 GB.
Is there a small count of binaries, that I can copy? Or must I give my VM more space? (I want to avoid that).
It it really necessary to have this dinosaur in your project? Of course it depends on your requirements, but if you just want to display some HTML pages and do not want to execute JavaScript, maybe the "HTMLViewer" (in OPM) in sufficient. (I am not mentioning the IpHTMLPanel which comes with Lazarus and is probably too buggy).
Title: Re: CEF component - the first step
Post by: Nicole on June 18, 2025, 06:15:16 pm
What I want to display is yahoo.com, the financial sites.
Not sure, if the small components do so.

However I am stuck:
It tried the "mini browser" demo and it gave me error messages, which dlls are missing. Well, there is a download link for them. Unfortunately, half of them are still missing.  :'(

All those browsers are that resource consuming! And whenever I tried a slime one (e.g. Pale Moon), - it did not really work in the sense, that sites were just part displayed.

Those slim components, do you know, if they display yahoo-charts?
Title: Re: CEF component - the first step
Post by: wp on June 18, 2025, 08:21:11 pm
What I want to display is yahoo.com, the financial sites.
Not sure, if the small components do so.
No chance with THTMLViewer here...
Title: Re: CEF component - the first step
Post by: loaded on June 19, 2025, 10:15:22 am
It it really necessary to have this dinosaur in your project? ....

Hey Master Wp, It's a bit harsh to call a masterpiece like CEF4 a dinosaur. 🤔
I think it's a great piece of work, yes it's a bit difficult to use at first but once you get used to it it's addictive and works flawlessly with all old and new technology sites.😉
By the way, I would like to thank my brother salvadordf for this unique work. Best regards.
Title: Re: CEF component - the first step
Post by: Thaddy on June 19, 2025, 10:54:49 am
I think wp was merely referring to the "excessive" size, when counting in all dependencies, not to deprecated code.
To display tickers from yahoo financial, you only need fcl-web/net and process json.
You definitely don't want to use the behemoth - which may be a better wording - that is CEF4 for that.
You only need 20-30 lines of code.

For multi-featured full browser processing CEF4 is a great choice but not fit for this particular very simple purpose.
Title: Re: CEF component - the first step
Post by: Thaddy on June 19, 2025, 11:19:34 am
@Nicole

Now I contradict myself for the number of lines...just a bit.... but here is a full example to retrieve the EUR/USD exchange rate from Yahoo finance.
I recommend getting a real API key from yahoo, but here I also implemented a - free - fallback.
Note do not try the fallback for real-time results:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. {$ifdef windows}{$apptype console}{$endif}
  3.  
  4. uses
  5.   Classes,
  6.   SysUtils,
  7.   fphttpclient,
  8.   fpjson,
  9.   jsonparser,
  10.   openssl,
  11.   opensslsockets;
  12.  
  13. const
  14.   YAHOO_API_KEY = 'your_api_key_here'; // Replace with actual key
  15.   YAHOO_FINANCE_API = 'https://query1.finance.yahoo.com/v7/finance/quote?symbols=EURUSD=X';
  16.   FALLBACK_API = 'https://api.exchangerate-api.com/v4/latest/EUR';
  17.  
  18. function GetYahooExchangeRate: Double;
  19. var
  20.   Client: TFPHttpClient;
  21.   Response: String;
  22.   JSONData: TJSONData;
  23.   QuotesArray: TJSONArray;
  24. begin
  25.   Result := 0;
  26.   Client := TFPHttpClient.Create(nil);
  27.   try
  28.     // Configure Yahoo Finance API request
  29.     Client.RequestHeaders.Clear;
  30.     Client.AddHeader('Accept', 'application/json');
  31.     Client.AddHeader('x-api-key', YAHOO_API_KEY); // API key header
  32.    
  33.     try
  34.       // Make authenticated request
  35.       Response := Client.Get(YAHOO_FINANCE_API);
  36.      
  37.       JSONData := GetJSON(Response);
  38.       try
  39.         // Parse the updated Yahoo Finance API response format
  40.         QuotesArray := (JSONData as TJSONObject).Get('quoteResponse',
  41.                        TJSONObject(nil)).Get('result', TJSONArray(nil));
  42.         if (QuotesArray <> nil) and (QuotesArray.Count > 0) then
  43.           Result := (QuotesArray.Items[0] as TJSONObject).Get('regularMarketPrice', 0.0);
  44.       finally
  45.         JSONData.Free;
  46.       end;
  47.     except
  48.       on E: Exception do
  49.         Writeln('Yahoo API Error: ', E.Message);
  50.     end;
  51.   finally
  52.     Client.Free;
  53.   end;
  54. end;
  55.  
  56. function GetFallbackExchangeRate: Double;
  57. var
  58.   Client: TFPHttpClient;
  59.   Response: String;
  60.   JSONData: TJSONData;
  61. begin
  62.   Result := 0;
  63.   Client := TFPHttpClient.Create(nil);
  64.   try
  65.     Client.RequestHeaders.Clear;
  66.     Client.AddHeader('User-Agent', 'CDDatabase/1.0');
  67.    
  68.     try
  69.       Response := Client.Get(FALLBACK_API);
  70.      
  71.       JSONData := GetJSON(Response);
  72.       try
  73.         Result := (JSONData as TJSONObject).Get('rates',
  74.                   TJSONObject(nil)).Get('USD', 0.0);
  75.       finally
  76.         JSONData.Free;
  77.       end;
  78.     except
  79.       on E: Exception do
  80.         Writeln('Fallback API Error: ', E.Message);
  81.     end;
  82.   finally
  83.     Client.Free;
  84.   end;
  85. end;
  86.  
  87. var
  88.   ExchangeRate: Double;
  89. begin
  90.   Writeln('Attempting to fetch EUR/USD rate...');
  91.  
  92.   // Try Yahoo with API key first
  93.   ExchangeRate := GetYahooExchangeRate;
  94.  
  95.   // If Yahoo fails, use fallback
  96.   if ExchangeRate = 0 then
  97.   begin
  98.     Writeln('Yahoo API failed, using fallback...');
  99.     ExchangeRate := GetFallbackExchangeRate;
  100.   end;
  101.  
  102.   if ExchangeRate > 0 then
  103.     Writeln(Format('Current EUR/USD: %.4f', [ExchangeRate]))
  104.   else
  105.     Writeln('All API attempts failed');
  106. end.
For real-time results you will need to pay!!! and get the professional API.
That is normal, but this also works.

I used a mix and match of three sources, my own, yahoo and deepseek (for the fallback.)

I would personally add some more robustness too. But this is already quite complete.
If you want to use the above for the real Yahoo API, I guess you just need to add the real key.
If you already have a key - like I have - I can amend the example to be a bit shorter if you want.
There is a place holder, though.
And you do not need CEF4 for this.

I wrote this from my professional background in finance software, just to test if I could still do it.
(I am a pensioner)

I can reduce this to ~40 lines and there is very little overhead. Mostly OpenSSL.

Plz note that if you do not have an API key, an exception is raised and handled.
This is intentional. (one always gets stupid remarks or questions in such cases)
Title: Re: CEF component - the first step
Post by: Nicole on June 19, 2025, 09:05:31 pm
thank you so much for the unit!
This is very useful to me.

At the moment I collect such data requests.

Do you need a FOREX request from Frankfurt?
If yes, I can post it.

Further I have an Alpha Vantage (thank you WP for your help!) and at the moment I work on MarketStack.
And there is one half-baked for stock-key-data, like ISIN, CPID ... no quotes, but all keys of the world.
TinyPortal © 2005-2018