我用TChromium创建了一个浏览器。TChromium是动态创建的。Facebook访问(登录)在过程结束时,组件将被销毁。再次创建组件时出现问题他继续上一个会话(登录)。我需要清理所有缓存和cookie。(强制注销)
下面的代码我创建组件通过:
var
Chromium: TChromium;
begin
try
Chromium := TChromium.Create(nil);
Chromium.SetParentComponent(Form1);
Chromium.Align := alClient;
chromium.Browser.MainFrame.LoadUrl('www.facebook.com');
我销毁它并释放这样的内存:
FreeAndNil(Chromium)
我该怎么办?
DCEF1:
要删除DCEF1包装中的cookie,ICefCookieManager
管理器接口中有DeleteCookies
函数。然而,我尝试了以下代码来删除所有cookie,但总是失败:
procedure TForm1.Button1Click(Sender: TObject);
var
CookieManager: ICefCookieManager;
begin
CookieManager := TCefCookieManagerRef.GetGlobalManager;
if not CookieManager.DeleteCookies('', '') then
ShowMessage('DeleteCookies failed!');
end;
幸运的是,还有另一个选项可以使用此cookie管理器删除cookie。访问所有这些,并在访问者函数中将True分配给deleteCookie
输出参数。使用cookie管理器时要小心,它是在您第一次导航到某个地方时创建的(GetGlobalManager
类函数不安全,没有正确处理以避免意外结果),所以请确保在导航后使用以下代码:
procedure TForm1.Button1Click(Sender: TObject);
var
CookieManager: ICefCookieManager;
begin
CookieManager := TCefCookieManagerRef.GetGlobalManager;
CookieManager.VisitAllCookiesProc(
function(const name, value, domain, path: ustring; secure, httponly,
hasExpires: Boolean; const creation, lastAccess, expires: TDateTime;
count, total: Integer; out deleteCookie: Boolean): Boolean
begin
deleteCookie := True;
ShowMessage('A cookie from domain ' + domain + ' will be unmercifully ' +
'deleted!');
end
);
end;
DCEF3:
在DCEF3包装器中,您可以使用以下内容。归功于Eric Santos:
type
CefTask = class(TCefTaskOwn)
procedure Execute; override;
end;
procedure CefTask.Execute;
var
CookieManager: ICefCookieManager;
begin
CookieManager := TCefCookieManagerRef.Global;
CookieManager.DeleteCookies('', '');
end;
procedure ClearCookies;
var
Task: CefTask;
begin
Task := CefTask.Create;
CefPostTask(TID_IO, Task);
end;
TLama你的帮助对我的项目起到了决定性的作用。我很感激与您分享经验。
用这个代码解决了我的问题:
procedure TForm1.Button1Click(Sender: TObject);
var
CookieManager: ICefCookieManager;
begin
CookieManager := TCefCookieManagerRef.GetGlobalManager;
CookieManager.VisitAllCookiesProc(
function(const name, value, domain, path: ustring; secure, httponly,
hasExpires: Boolean; const creation, lastAccess, expires: TDateTime;
count, total: Integer; out deleteCookie: Boolean): Boolean
begin
deleteCookie := True;
ShowMessage('A cookie from domain ' + domain + ' will be unmercifully ' +
'deleted!');
end
);
end;
拥抱