1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2025-06-22 22:17:48 +02:00

Added SetCookie example to the CookieVisitor demo

This commit is contained in:
Salvador Díaz Fau
2018-10-21 20:32:35 +02:00
parent 74c545a5fb
commit b5920d1aac

View File

@ -50,13 +50,15 @@ uses
Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
{$ENDIF} {$ENDIF}
uCEFChromium, uCEFWindowParent, uCEFInterfaces, uCEFApplication, uCEFTypes, uCEFConstants, uCEFChromium, uCEFWindowParent, uCEFInterfaces, uCEFApplication, uCEFTypes, uCEFConstants,
uCEFCookieManager, uCEFCookieVisitor; uCEFCookieManager, uCEFCookieVisitor, uCEFWinControl;
const const
MINIBROWSER_SHOWCOOKIES = WM_APP + $101; MINIBROWSER_SHOWCOOKIES = WM_APP + $101;
MINIBROWSER_SETCOOKIERSLT = WM_APP + $102;
MINIBROWSER_CONTEXTMENU_DELETECOOKIES = MENU_ID_USER_FIRST + 1; MINIBROWSER_CONTEXTMENU_DELETECOOKIES = MENU_ID_USER_FIRST + 1;
MINIBROWSER_CONTEXTMENU_GETCOOKIES = MENU_ID_USER_FIRST + 2; MINIBROWSER_CONTEXTMENU_GETCOOKIES = MENU_ID_USER_FIRST + 2;
MINIBROWSER_CONTEXTMENU_SETCOOKIE = MENU_ID_USER_FIRST + 3;
type type
TCookieVisitorFrm = class(TForm) TCookieVisitorFrm = class(TForm)
@ -102,6 +104,7 @@ type
procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED; procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED;
procedure BrowserDestroyMsg(var aMessage : TMessage); message CEF_DESTROY; procedure BrowserDestroyMsg(var aMessage : TMessage); message CEF_DESTROY;
procedure ShowCookiesMsg(var aMessage : TMessage); message MINIBROWSER_SHOWCOOKIES; procedure ShowCookiesMsg(var aMessage : TMessage); message MINIBROWSER_SHOWCOOKIES;
procedure SetCookieRsltMsg(var aMessage : TMessage); message MINIBROWSER_SETCOOKIERSLT;
protected protected
FText : string; FText : string;
@ -196,6 +199,14 @@ begin
SimpleTextViewerFrm.ShowModal; SimpleTextViewerFrm.ShowModal;
end; end;
procedure TCookieVisitorFrm.SetCookieRsltMsg(var aMessage : TMessage);
begin
if (aMessage.wParam = 0) then
showmessage('There was a problem setting the cookie')
else
showmessage('Cookie set successfully !');
end;
procedure TCookieVisitorFrm.Timer1Timer(Sender: TObject); procedure TCookieVisitorFrm.Timer1Timer(Sender: TObject);
begin begin
Timer1.Enabled := False; Timer1.Enabled := False;
@ -227,6 +238,7 @@ begin
model.AddItem(MINIBROWSER_CONTEXTMENU_DELETECOOKIES, 'Delete cookies'); model.AddItem(MINIBROWSER_CONTEXTMENU_DELETECOOKIES, 'Delete cookies');
model.AddSeparator; model.AddSeparator;
model.AddItem(MINIBROWSER_CONTEXTMENU_GETCOOKIES, 'Visit cookies'); model.AddItem(MINIBROWSER_CONTEXTMENU_GETCOOKIES, 'Visit cookies');
model.AddItem(MINIBROWSER_CONTEXTMENU_SETCOOKIE, 'Set cookie');
end; end;
procedure TCookieVisitorFrm.Chromium1BeforePopup(Sender: TObject; procedure TCookieVisitorFrm.Chromium1BeforePopup(Sender: TObject;
@ -259,6 +271,7 @@ begin
case commandId of case commandId of
MINIBROWSER_CONTEXTMENU_DELETECOOKIES : Chromium1.DeleteCookies; MINIBROWSER_CONTEXTMENU_DELETECOOKIES : Chromium1.DeleteCookies;
MINIBROWSER_CONTEXTMENU_GETCOOKIES : MINIBROWSER_CONTEXTMENU_GETCOOKIES :
begin begin
// This should be protected by a mutex // This should be protected by a mutex
@ -266,6 +279,27 @@ begin
TempManager := TCefCookieManagerRef.Global(nil); TempManager := TCefCookieManagerRef.Global(nil);
TempManager.VisitAllCookies(FVisitor); TempManager.VisitAllCookies(FVisitor);
end; end;
MINIBROWSER_CONTEXTMENU_SETCOOKIE :
begin
TempManager := TCefCookieManagerRef.Global(nil);
if TempManager.SetCookie('https://www.example.com',
'example_cookie_name',
'1234',
'',
'/',
True,
True,
False,
now,
now,
now,
nil) then
PostMessage(Handle, MINIBROWSER_SETCOOKIERSLT, ord(True), 0)
else
PostMessage(Handle, MINIBROWSER_SETCOOKIERSLT, ord(False), 0);
end;
end; end;
end; end;