如何清除嵌入式Chromium浏览器的缓存和Cookie?

如何清除嵌入式Chromium浏览器的缓存和Cookie?

问题描述:

我用TChromium创建了一个浏览器。
TChromium是动态创建的。
Facebook访问(登录)
在过程结束时,组件被销毁。
再次创建组件时出现问题
他继续前一个会话(登录)。
我需要清除所有缓存和cookie。 (强制退出)

I created a browser with TChromium. The TChromium is created dynamically. Facebook access (login) At the end of the process, the component is destroyed. The problem occurs when the component is created again He continues with the previous session (login). I need to clean all cache and cookies. (Force Log out)

在代码下面我创建组件:

Below the code I create the component by:

var
   Chromium: TChromium;
begin
   try
     Chromium := TChromium.Create(nil);
     Chromium.SetParentComponent(Form1);
     Chromium.Align := alClient;
     chromium.Browser.MainFrame.LoadUrl('www.facebook.com');

我将其销毁并释放内存:

I destroy it and release memory like this:

FreeAndNil(Chromium)

我该怎么办?

DCEF1

删除在DCEF1包装器中的Cookie有 ICefCookieManager 管理器界面中的 DeleteCookies 但是,我试过下面的代码删除所有的cookie,但它总是对我失败:

To delete cookies in DCEF1 wrapper there's the DeleteCookies function in ICefCookieManager manager interface. However, I've tried the following code to delete all cookies, but it always failed to me:

procedure TForm1.Button1Click(Sender: TObject);
var
  CookieManager: ICefCookieManager;
begin
  CookieManager := TCefCookieManagerRef.GetGlobalManager;
  if not CookieManager.DeleteCookies('', '') then
    ShowMessage('DeleteCookies failed!');
end;

幸运的是,还有一个选项可以使用此Cookie管理器删除Cookie。访问所有这些,并在访问函数assign为 deleteCookie 输出参数赋值True。小心获取cookie管理器,它是在你第一次浏览某个地方时创建的( GetGlobalManager 类函数是不安全的,它不能正确处理意外的结果)请在导航后使用此代码:

Fortunately, there is another option to delete cookies using this cookie manager. Visit all of them and in the visitor function assign True to the deleteCookie output parameter. Be careful with getting cookie manager, it's created the first time you navigate somewhere (the GetGlobalManager class function is unsafe, it's not properly handled for unexpected result), so be sure you'll use this code after navigation:

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

In DCEF3 wrapper you can use the following. Credit goes to 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;