DCEF3 相关资料

 

DCEF3 相关资料

转自:https://www.cnblogs.com/xiefang2008/p/5969610.html

 

DCEF3 调用 js

http://www.cnblogs.com/Delphi-Farmer/p/4103708.html

 

DCEF3 调用 js

http://www.cnblogs.com/Delphi-Farmer/p/4103708.html

复制代码

interface
 
uses
  ceflib;//其它
 
type
//这里建议用class  不建议用class(TThread)  不然有些地方要报错
TMyExtension = class(TThread) // or just class, (extension code execute in thread)
  public
  class function _geta:string;
end;
 
TCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
protected
    procedure OnWebKitInitialized; override;
end;
 
implementation
 
class function TMyExtension._geta: string;
begin
  Result:='调用成功';
end;
 
procedure TCustomRenderProcessHandler.OnWebKitInitialized;
begin
  TCefRTTIExtension.Register('JS_DELPHI', TMyExtension);
end;
 
initialization
  CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
 
end.

复制代码

JS调用实例:

 注意:CallbackObjectForJs的showMessage方法首字母不能使大写,不然javascript回调的时候找不到对应的方法。原因还在分析中。。。

 PS:cefsharp是一个用于C#的浏览器控件(开源),C#自带的控件在IE内核适配的问题上处理起来有点麻烦,同时如果网页是重度使用javascript,那你可以考虑基于cef的各种浏览器控件,执行效率飙升。cefsharp的github:https://github.com/cefsharp/CefSharp

 

Use this code to delete Cookies from Chromium Version CEF3:

Use c_WB_ClearCookies for deleating all Cookies

Use c_WB_Clear_url_Cookies for deleating all Cookies only from one speceally Url like this -> c_WB_Clear_url_Cookies('http://google.com','cookie_name');

复制代码

type
  CefTask = class(TCefTaskOwn)
    procedure Execute; override;

    public
    var url,cookieName: ustring;
    constructor create; virtual;
  end;

constructor CefTask.create;
begin
  inherited create;
  url := '';
  cookieName := '';
end;

procedure CefTask.Execute;
var CookieManager: ICefCookieManager;
begin
  CookieManager := TCefCookieManagerRef.Global;
  CookieManager.DeleteCookies(url,cookieName);
end;

procedure c_WB_ClearCookies;
var Task: CefTask;
begin
  Task := CefTask.Create;
  CefPostTask(TID_IO, Task);
end;

// c_WB_Clear_url_Cookies('http://google.com','cookie_name');
procedure c_WB_Clear_url_Cookies(c_url,c_cookieName: ustring);
var Task: CefTask;
begin
  Task := CefTask.Create;
  Task.url := c_url;
  Task.cookieName := c_cookieName;
  CefPostTask(TID_IO, Task);
end;

复制代码

For list all Cookies to get the cookieName use Procedure list_all_cookies

复制代码

procedure pausek;
var M: TMsg;
begin
  while PeekMessage(M, 0, 0, 0, pm_Remove) do
    begin
      TranslateMessage(M);
      DispatchMessage(M);
    end;
end;

procedure pause(i:longint);
var j : nativeint;
begin
  for j := 1 to i do
    begin
      pausek;
      sleep(100);
    end;
end;



procedure list_all_cookies;
var CookieManager: ICefCookieManager;
    cookie_list : string;
const lf = chr(13) + chr(10); 
begin

  cookie_list := '';

  CookieManager := TCefCookieManagerRef.Global;

  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

      cookie_list := cookie_list + inttostr(count) + ': ' +  domain + ' - ' + name + ' - ' + value + ' - ' + path + lf;

     if (count 
  

复制代码

 

Create and get a cookie

http://stackoverflow.com/questions/16086160/delphi-chromium-embedded-create-and-get-a-cookie/23723741#23723741

复制代码

Uses 
  ceflib;


const
DefaultCookiesDir = 'Cookies/';

implementation
{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
var
  CookieManager: ICefCookieManager;
  CookiesPath : String;
begin
  CookiesPath := ExtractFilePath(Application.ExeName) + DefaultCookiesDir + 'User1';
  CookieManager := TCefCookieManagerRef.GetGlobalManager;
  CookieManager.SetStoragePath(CookiesPath);
  Chromium1.Load('www.vk.com');
end;

复制代码

A guy form the official's DCEF3 forum provided the solution below, tested and approved !

复制代码

CookieManager: ICefCookieManager;

FormCreate:
begin
   CookiesPath := ExtractFilePath(Application.ExeName) + 'cookies';
   CookieManager := TCefCookieManagerRef.Global(nil);
   CookieManager.SetStoragePath(CookiesPath, True, nil);
end;

FormClose:   
begin
  CookieManager.FlushStore(nil);
end

复制代码

 

为按钮添加单击事件 Sample

复制代码

{$I cef.inc}

type
  TCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
  protected
    procedure OnWebKitInitialized; override;
    function OnProcessMessageReceived(const browser: ICefBrowser; sourceProcess: TCefProcessId;
      const message: ICefProcessMessage): Boolean; override;
  end;

  TTestExtension = class
    class function hello: string;
  end;

procedure TMainForm.Button2Click(Sender: TObject);
begin
  Chromium.browser.SendProcessMessage(PID_RENDERER,
    TCefProcessMessageRef.New('visitdom'));//操作DOM
end;

procedure ButtonClickProc(const Event: ICefDomEvent);
begin
  ShowMessage('Click The Button');
end;

procedure VisitDomProc(const Doc: ICefDomDocument);
var
  ButtonNode: ICefDomNode;
begin
  ButtonNode := Doc.GetElementById('su1');
  if Assigned(ButtonNode) then
    ButtonNode.AddEventListenerProc('click', True, ButtonClickProc);
end;

{ TCustomRenderProcessHandler }

function TCustomRenderProcessHandler.OnProcessMessageReceived(
  const browser: ICefBrowser; sourceProcess: TCefProcessId;
  const message: ICefProcessMessage): Boolean;
begin
{$IFDEF DELPHI14_UP}
  if (message.Name = 'visitdom') then
    begin
      browser.MainFrame.VisitDomProc( VisitDomProc);
        Result := True;
    end
  else
{$ENDIF}
    Result := False;
end;

procedure TCustomRenderProcessHandler.OnWebKitInitialized;
begin
{$IFDEF DELPHI14_UP}
  TCefRTTIExtension.Register('app', TTestExtension);
{$ENDIF}
end;

{ TTestExtension }

class function TTestExtension.hello: string;
begin
   Result := 'Hello from Delphi';
end;

initialization 
  CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
  CefBrowserProcessHandler := TCefBrowserProcessHandlerOwn.Create;
end.

复制代码

 

你可能感兴趣的:(delphi)