安装Edge
启动Delphi12编译器 打开菜单项
Tools>>GetIt Packege Maniger…
接下来,找到下载的文件目录,从里面拷贝出来WebView2Loader.dll(找不到就搜索吧)。
把这个动态库放到你发布的程序所在的目录,到这里就可以开发包含Edge浏览器控件的程序了。
cookie的获取
我们从官网上能看到这样的例程:
if (m_cookieManager)
{
CHECK_FAILURE(m_cookieManager->GetCookies(
uri.c_str(),
Callback<ICoreWebView2GetCookiesCompletedHandler>(
[this, uri](HRESULT error_code, ICoreWebView2CookieList* list) -> HRESULT {
CHECK_FAILURE(error_code);
std::wstring result;
UINT cookie_list_size;
CHECK_FAILURE(list->get_Count(&cookie_list_size));
if (cookie_list_size == 0)
{
result += L"No cookies found.";
}
else
{
result += std::to_wstring(cookie_list_size) + L" cookie(s) found";
if (!uri.empty())
{
result += L" on " + uri;
}
result += L"\n\n[";
for (UINT i = 0; i < cookie_list_size; ++i)
{
wil::com_ptr<ICoreWebView2Cookie> cookie;
CHECK_FAILURE(list->GetValueAtIndex(i, &cookie));
if (cookie.get())
{
result += CookieToString(cookie.get());
if (i != cookie_list_size - 1)
{
result += L",\n";
}
}
}
result += L"]";
}
m_appWindow->AsyncMessageBox(std::move(result), L"GetCookies Result");
return S_OK;
})
.Get()));
}
以上代码显然是C++的,其中的Callback函数,让人很是困惑,至少我困了,不知道如何在Delphi中直接使用这样的语法。
根据接口的使用规则,改造一下。
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Winapi.WebView2, Winapi.ActiveX,
Vcl.Edge, Vcl.OleCtrls, SHDocVw, Vcl.StdCtrls, Vcl.ExtCtrls,WebView2_TLB;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
br1: TEdgeBrowser;
Button2: TButton;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TView2CkHandler = class(TInterfacedObject, ICoreWebView2GetCookiesCompletedHandler)
function Invoke(AResult: HResult; const cookieList: ICoreWebView2CookieList): HResult; stdcall;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TView2CkHandler.Invoke(AResult: HResult; const cookieList: ICoreWebView2CookieList): HResult;
var
i: integer;
nCookieCount: Cardinal;
LCookie: ICoreWebView2Cookie;
pName, pValue, pDoMain, pPath: PWidechar;
begin
result := S_OK;
CookieList.Get_Count(nCookieCount);
for i := 0 to nCookieCount - 1 do
begin
CookieList.GetValueAtIndex(i, LCookie);
if Assigned(LCookie) then
begin
LCookie.Get_name(pName);
LCookie.Get_value(pValue);
LCookie.Get_Domain(pDoMain);
LCookie.Get_Path(pPath);
Form1.Memo1.Lines.Add(
format('''
Name=%s,
Value=%s,
DoMain=%s,
Path=%s
''',[pName,pValue,pDoMain,pPath]));
end;
end;
result:=0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
self.br1.Navigate('https://www.baidu.com')
end;
// if Assigned(LCookieMgt) then
// begin
// LCookieMgt.CreateCookie('testname', 'testvalue', 'testdomain.com', '/', LCookie);
// if Assigned(LCookie) then
// LCookieMgt.AddOrUpdateCookie(LCookie) // 在打开网页前写入 cookie end
// end;
procedure getCookies(AWebView2: IInterface); // AWebView2 是 EdgeBrowser1.DefaultInterface
var
LWebView2_2: ICoreWebView2_2;
LCookieMgt: ICoreWebView2CookieManager;
LCookie: ICoreWebView2Cookie;
ckHandler: ICoreWebView2GetCookiesCompletedHandler;
begin
LWebView2_2 := AWebView2 as ICoreWebView2_2; // 强转
LWebView2_2.Get_CookieManager(LCookieMgt);
if Assigned(LCookieMgt) then
begin
LCookieMgt.GetCookies(nil, ICoreWebView2GetCookiesCompletedHandler(TView2CkHandler.create));
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
getCookies(br1.DefaultInterface);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
self.br1.Navigate('https://www.hao123.com')
end;
end.