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.

Set Cookie

marcoscdoni
Posts: 25
Joined: Thu Sep 21, 2017 4:33 pm

Set Cookie

Post by marcoscdoni »

Hello, first of all I would like to say that your work is amazing! Congratulations!



To assign a cookie in DCEF3 I use the following code:

TCefFastTask.New(TID_IO, procedure
begin
CookieManager.SetCookie(url, name, value, domain, '/', false, false, false, creationDate, accessDate, expirationDate);
end
);


How would it be possible to do the same using CEF4Delphi?

Thank you!
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Set Cookie

Post by salvadordf »

Hi,

I've never done that but in theory it should be done the same way.

CEF4Delphi is based in DCEF3 and many classes are identical.
marcoscdoni
Posts: 25
Joined: Thu Sep 21, 2017 4:33 pm

Re: Set Cookie

Post by marcoscdoni »

It works correctly. Just need to pass last callback parameter a nil. Thank!
rgdawson
Posts: 22
Joined: Tue Apr 10, 2018 11:05 pm

Re: Set Cookie

Post by rgdawson »

I find that this works for 32-bit, but does nothing when I compile my application in 64-bit. In 64-bit, the anonymous task does not get executed. No idea why at this point.

R Greg Dawson
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Set Cookie

Post by salvadordf »

While I find a way to test this in 64 bits (Lazarus or some friend with a 64 bit Delphi compiler) I added a SetCookie example to the CookieVisitor demo in the context menu.

The CEF3 code comments say that you can call SetCookie in any thread and the example in CookieVisitor do that successfully.

Please, use SetCookie without a task while I try to find a way to see why that task is not working.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Set Cookie

Post by salvadordf »

Please, try this code. I tested it in 64 bits with Lazarus and it works.

Add a custom procedure that calls SeCookie like this :

Code: Select all

procedure SetCookieTask;
var
  TempManager : ICefCookieManager;
begin
  TempManager := TCefCookieManagerRef.Global(nil);
  TempManager.SetCookie('https://www.example.com',
                                 'example_cookie_name',
                                 '1234',
                                 '',
                                 '/',
                                 True,
                                 True,
                                 False,
                                 now,
                                 now,
                                 now,
                                 nil);
end;
Then create a TCefFastTask with that procedure :

Code: Select all

procedure PostCookieTask;
var
  TempTask : TCefFastTask;
begin
  TempTask := TCefFastTask.Create(SetCookieTask);
  CefPostTask(TID_IO, TempTask);
end;        
PS : To test this code in Lazarus just add a '@' before SetCookieTask when you create TCefFastTask
dilfich
Posts: 368
Joined: Thu Nov 30, 2017 1:17 am

Re: Set Cookie

Post by dilfich »

And how can you put a cookie site which is in the frame? It is possible through JS, but it is not quite convenient.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Set Cookie

Post by salvadordf »

Sorry for the late answer.

The CookieManager is the only way to set cookies with CEF3 if you don't want to use JavaScript :
https://magpcss.org/ceforum/apidocs3/pr ... nager.html

You can also use JavaScript if it's more convenient :
https://www.w3schools.com/js/js_cookies.asp
https://developer.mozilla.org/en-US/doc ... ent/cookie
dilfich
Posts: 368
Joined: Thu Nov 30, 2017 1:17 am

Re: Set Cookie

Post by dilfich »

About this question, in the demo you have shown the work of CookieManager and it works. But how to use it to set cookies to another site in the frame?
Or is it only through JavaScript..
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Set Cookie

Post by salvadordf »

The CookieVisitor demo creates a cookie for the "example.com" domain while visiting the "google.com" website.
You can set cookies for any domain whether it's in a frame or it's not even loaded.

Modify the cookie domain in the TCookieVisitorFrm.Chromium1ContextMenuCommand procedure to test your cookie.
Post Reply