Skip to content

Commit a4e1439

Browse files
author
Langji
committed
增加判断文档主页加载完成事件ondocumentready
修正代理参数。
1 parent e38f221 commit a4e1439

7 files changed

+227
-160
lines changed

WkeForDelphi.stat

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[Stats]
2-
EditorSecs=10709
2+
EditorSecs=10952
33
DesignerSecs=1
44
InspectorSecs=1
5-
CompileSecs=75923
6-
OtherSecs=500
5+
CompileSecs=78519
6+
OtherSecs=538
77
StartTime=2018-01-17 10:52:11
88
RealKeys=0
99
EffectiveKeys=0

bin/mb.dll

364 KB
Binary file not shown.

bin/node.dll

-911 KB
Binary file not shown.

src/Langji.Wke.CustomPage.pas

+80-50
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ TCustomWkePage = class(TComponent)
4747
FWindowWidth: Integer;
4848
FOnPromptBox: TOnPromptBoxEvent;
4949
FOnConsoleMessage: TOnConsoleMessgeEvent;
50+
FisReady: boolean;
5051
function GetZoom: Integer;
5152
procedure SetZoom(const Value: Integer);
5253

@@ -55,8 +56,7 @@ TCustomWkePage = class(TComponent)
5556
procedure DoWebViewUrlChange(Sender: TObject; sUrl: string);
5657
procedure DoWebViewLoadStart(Sender: TObject; sUrl: string; navigationType: wkeNavigationType; var Cancel: boolean);
5758
procedure DoWebViewLoadEnd(Sender: TObject; sUrl: string; loadresult: wkeLoadingResult);
58-
procedure DoWebViewCreateView(Sender: TObject; sUrl: string; navigationType: wkeNavigationType; windowFeatures:
59-
PwkeWindowFeatures; var wvw: wkeWebView);
59+
procedure DoWebViewCreateView(Sender: TObject; sUrl: string; navigationType: wkeNavigationType; windowFeatures: PwkeWindowFeatures; var wvw: wkeWebView);
6060
procedure DoWebViewAlertBox(Sender: TObject; smsg: string);
6161
function DoWebViewConfirmBox(Sender: TObject; smsg: string): boolean;
6262
function DoWebViewPromptBox(Sender: TObject; smsg, defaultres, Strres: string): boolean;
@@ -98,7 +98,7 @@ TCustomWkePage = class(TComponent)
9898
procedure LoadFile(const AFile: string);
9999
procedure SetFocusToWebbrowser;
100100
function ExecuteJavascript(const js: string): boolean;
101-
function GetSource: string;
101+
function GetSource(const delay: Integer = 200): string;
102102

103103
/// <summary>
104104
/// 执行js并得到string返回值
@@ -110,6 +110,7 @@ TCustomWkePage = class(TComponent)
110110
property LocationTitle: string read GetLocationTitle;
111111
property LoadFinished: Boolean read GetLoadFinished; //加载完成
112112
property Transparent: Boolean read GetTransparent write SetTransparent;
113+
property Mainwkeview: TWkeWebView read thewebview;
113114
published
114115
property Caption: string write SetCaption;
115116
property WindowLeft: Integer read FWindowLeft write FWindowLeft;
@@ -119,6 +120,7 @@ TCustomWkePage = class(TComponent)
119120
property UserAgent: string read FwkeUserAgent write FwkeUserAgent;
120121
property HtmlFile: string read FHtmlFile write FHtmlFile;
121122
property WindowHandle: Hwnd read GetWebHandle;
123+
property isReady: boolean read FisReady write FisReady;
122124
property Headless: Boolean write SetHeadless;
123125
property CookieEnabled: Boolean read FCookieEnabled write FCookieEnabled default true;
124126
property CookiePath: string read FwkeCookiePath write FWkeCookiePath;
@@ -148,12 +150,6 @@ TWkePopupPage = class(TCustomWkePage)
148150
FVisible, FHeadLess: Boolean;
149151
protected
150152
procedure CreateWebView; override;
151-
public
152-
/// <summary>
153-
/// 取源码
154-
/// </summary>
155-
function GetSource(const delay:Integer=200): string;
156-
157153
end;
158154

159155
TWkeGetSource = class
@@ -162,13 +158,15 @@ TWkeGetSource = class
162158
function GetSourceHtml: string;
163159
function GetSourceText: string;
164160
procedure DoAlertBox(Sender: TObject; sMsg: string);
161+
function getReady: boolean;
165162
public
166163
constructor Create();
167164
destructor Destroy; override;
168165
procedure ShowWebPage(const bVisible, bHeadLess: Boolean);
169166
procedure LoadUrl(const Aurl: string);
170167
property SourceHtml: string read GetSourceHtml;
171168
property SourceText: string read GetSourceText;
169+
property isReady: boolean read getReady;
172170
end;
173171

174172
/// <summary>
@@ -183,11 +181,20 @@ function GetSourceByWke(const Aurl: string; const AshowWindow, AHeadLess: Boolea
183181

184182
function GetSourceTextByWke(const Aurl: string; const AshowWindow, AHeadLess: Boolean; const ADelay: Integer): string;
185183

184+
var
185+
tmpSource: string = '';
186+
186187
implementation
187188

188-
uses
189+
uses //Vcl.Dialogs,
189190
math;
190191

192+
procedure doDucumentReadyCallback(webView: wkeWebView; param: Pointer; frameid: wkeFrameHwnd); cdecl;
193+
begin
194+
if wkeIsMainFrame(webView, Cardinal(frameid)) then
195+
TCustomWkePage(param).DoWebViewDocumentReady(TCustomWkePage(param));
196+
end;
197+
191198
procedure DoTitleChange(webView: wkeWebView; param: Pointer; title: wkeString); cdecl;
192199
begin
193200
TCustomWkePage(param).DoWebViewTitleChange(TCustomWkePage(param), wkeWebView.GetString(title));
@@ -212,8 +219,7 @@ function DoLoadStart(webView: wkeWebView; param: Pointer; navigationType: wkeNav
212219
result := not cancel;
213220
end;
214221

215-
function DoCreateView(webView: wkeWebView; param: Pointer; navigationType: wkeNavigationType; url: wkeString;
216-
windowFeatures: PwkeWindowFeatures): wkeWebView; cdecl;
222+
function DoCreateView(webView: wkeWebView; param: Pointer; navigationType: wkeNavigationType; url: wkeString; windowFeatures: PwkeWindowFeatures): wkeWebView; cdecl;
217223
begin
218224
TCustomWkePage(param).DoWebViewCreateView(TCustomWkePage(param), wkeWebView.GetString(url), navigationType, windowFeatures, result);
219225
end;
@@ -235,15 +241,12 @@ function DoConfirmBox(webView: wkeWebView; param: Pointer; msg: wkeString): Bool
235241

236242
function DoPromptBox(webView: wkeWebView; param: Pointer; msg: wkeString; defaultResult: wkeString; sresult: wkeString): Boolean; cdecl;
237243
begin
238-
result := TCustomWkePage(param).DoWebViewPromptBox(TCustomWkePage(param), wkeWebView.GetString(msg), wkeWebView.GetString
239-
(defaultResult), wkeWebView.GetString(sresult));
244+
result := TCustomWkePage(param).DoWebViewPromptBox(TCustomWkePage(param), wkeWebView.GetString(msg), wkeWebView.GetString(defaultResult), wkeWebView.GetString(sresult));
240245
end;
241246

242-
procedure DoConsoleMessage(webView: wkeWebView; param: Pointer; level: wkeMessageLevel; const AMessage, sourceName:
243-
wkeString; sourceLine: Cardinal; const stackTrack: wkeString); cdecl;
247+
procedure DoConsoleMessage(webView: wkeWebView; param: Pointer; level: wkeMessageLevel; const AMessage, sourceName: wkeString; sourceLine: Cardinal; const stackTrack: wkeString); cdecl;
244248
begin
245-
TCustomWkePage(param).DoWebViewConsoleMessage(TCustomWkePage(param), wkeWebView.GetString(AMessage), wkeWebView.GetString
246-
(sourceName), sourceLine, wkeWebView.GetString(stackTrack));
249+
TCustomWkePage(param).DoWebViewConsoleMessage(TCustomWkePage(param), wkeWebView.GetString(AMessage), wkeWebView.GetString(sourceName), sourceLine, wkeWebView.GetString(stackTrack));
247250
end;
248251

249252
procedure DoDocumentReady(webView: wkeWebView; param: Pointer); cdecl;
@@ -261,14 +264,12 @@ procedure DoWindowDestroy(webWindow: wkeWebView; param: Pointer); cdecl;
261264
TCustomWkePage(param).DoWebViewWindowDestroy(TCustomWkePage(param));
262265
end;
263266

264-
var
265-
tmpSource: string = '';
266-
267267
function DoGetSource(p1, p2, es: jsExecState): jsValue;
268268
var
269269
s: string;
270270
begin
271271
s := es.ToTempString(es.Arg(0));
272+
// ShowMessage(s);
272273
tmpSource := s;
273274
result := 0;
274275
end;
@@ -285,8 +286,7 @@ constructor TCustomWkePage.Create(AOwner: TComponent);
285286
FWindowTop := 10;
286287
FWindowWidth := 640;
287288
FWindowHeight := 480;
288-
FwkeUserAgent :=
289-
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 Langji.Wke 1.0';
289+
FwkeUserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 Langji.Wke 1.0';
290290
end;
291291

292292
destructor TCustomWkePage.Destroy;
@@ -329,22 +329,21 @@ function TCustomWkePage.DoWebViewConfirmBox(Sender: TObject; smsg: string): bool
329329
FOnConfirmBox(self, smsg, result);
330330
end;
331331

332-
procedure TCustomWkePage.DoWebViewConsoleMessage(Sender: TObject; const AMessage, sourceName: string; sourceLine:
333-
Cardinal; const stackTrack: string);
332+
procedure TCustomWkePage.DoWebViewConsoleMessage(Sender: TObject; const AMessage, sourceName: string; sourceLine: Cardinal; const stackTrack: string);
334333
begin
335334
if Assigned(FOnConsoleMessage) then
336335
FOnConsoleMessage(self, AMessage, sourceName, sourceLine);
337336
end;
338337

339-
procedure TCustomWkePage.DoWebViewCreateView(Sender: TObject; sUrl: string; navigationType: wkeNavigationType;
340-
windowFeatures: PwkeWindowFeatures; var wvw: wkeWebView);
338+
procedure TCustomWkePage.DoWebViewCreateView(Sender: TObject; sUrl: string; navigationType: wkeNavigationType; windowFeatures: PwkeWindowFeatures; var wvw: wkeWebView);
341339
begin
342340
if Assigned(FOnCreateView) then
343341
FOnCreateView(self, sUrl, navigationType, windowFeatures, wvw);
344342
end;
345343

346344
procedure TCustomWkePage.DoWebViewDocumentReady(Sender: TObject);
347345
begin
346+
FisReady := true;
348347
if Assigned(FOnDocumentReady) then
349348
FOnDocumentReady(Self);
350349
end;
@@ -358,6 +357,7 @@ procedure TCustomWkePage.DoWebViewLoadEnd(Sender: TObject; sUrl: string; loadres
358357

359358
procedure TCustomWkePage.DoWebViewLoadStart(Sender: TObject; sUrl: string; navigationType: wkeNavigationType; var Cancel: boolean);
360359
begin
360+
FisReady := false;
361361
if Assigned(FOnLoadStart) then
362362
FOnLoadStart(self, sUrl, navigationType, Cancel);
363363
FLoadFinished := false;
@@ -475,9 +475,14 @@ function TCustomWkePage.GetMediaVolume: Single;
475475
result := thewebview.MediaVolume;
476476
end;
477477

478-
function TCustomWkePage.GetSource: string; //获取源码
478+
function TCustomWkePage.GetSource(const delay: Integer = 200): string; //获取源码
479479
begin
480480
result := '';
481+
if Assigned(thewebview) then
482+
ExecuteJavascript('GetSource(document.getElementsByTagName("html")[0].outerHTML);');
483+
Sleep(delay);
484+
result := tmpSource;
485+
//showmessage(tmpSource);
481486
end;
482487

483488
function TCustomWkePage.GetTransparent: boolean;
@@ -619,14 +624,18 @@ procedure TWkeTransparentPage.CreateWebView;
619624
if Assigned(FOnPromptBox) then
620625
thewebview.SetOnPromptBox(DoPromptBox, self);
621626
thewebview.SetOnConsoleMessage(DoConsoleMessage, self);
622-
thewebview.SetOnDocumentReady(DoDocumentReady, self);
627+
// thewebview.SetOnDocumentReady(DoDocumentReady, self);
628+
wkeOnDocumentReady2(thewebview, doDucumentReadyCallback, Self);
623629
thewebview.SetOnWindowClosing(DoWindowClosing, self);
624630
thewebview.SetOnWindowDestroy(DoWindowDestroy, self);
625631
if FwkeUserAgent <> '' then
626632
wkeSetUserAgent(thewebview, PansiChar(AnsiString(FwkeUserAgent)));
627633
wkeSetCookieEnabled(thewebview, FCookieEnabled);
628634
if DirectoryExists(FwkeCookiePath) and Assigned(wkeSetCookieJarPath) then
629635
wkeSetCookieJarPath(thewebview, PwideChar(FwkeCookiePath));
636+
637+
wkeSetCspCheckEnable(thewebview, False); //关闭跨域检查
638+
jsBindFunction('GetSource', DoGetSource, 1);
630639
end;
631640

632641
end;
@@ -638,10 +647,10 @@ procedure TWkePopupPage.CreateWebView;
638647
thewebview := wkeCreateWebWindow(WKE_WINDOW_TYPE_POPUP, 0, FWindowLeft, FWindowTop, FWindowWidth, FWindowHeight);
639648
if Assigned(thewebview) then
640649
begin
641-
// if not FVisible then
642-
// ShowWindow(thewebview.WindowHandle, SW_HIDE)
643-
// else
644-
ShowWindow(thewebview.WindowHandle, SW_hide);
650+
if not FVisible then
651+
ShowWindow(thewebview.WindowHandle, SW_HIDE)
652+
else
653+
ShowWindow(thewebview.WindowHandle, SW_NORMAL);
645654
thewebview.SetOnTitleChanged(DoTitleChange, self);
646655
thewebview.SetOnURLChanged(DoUrlChange, self);
647656
thewebview.SetOnNavigation(DoLoadStart, self);
@@ -656,7 +665,8 @@ procedure TWkePopupPage.CreateWebView;
656665
if Assigned(FOnPromptBox) then
657666
thewebview.SetOnPromptBox(DoPromptBox, self);
658667
thewebview.SetOnConsoleMessage(DoConsoleMessage, self);
659-
thewebview.SetOnDocumentReady(DoDocumentReady, self);
668+
// thewebview.SetOnDocumentReady(DoDocumentReady, self);
669+
wkeOnDocumentReady2(thewebview, doDucumentReadyCallback, Self);
660670
thewebview.SetOnWindowClosing(DoWindowClosing, self);
661671
thewebview.SetOnWindowDestroy(DoWindowDestroy, self);
662672
if FwkeUserAgent <> '' then
@@ -665,19 +675,12 @@ procedure TWkePopupPage.CreateWebView;
665675
if DirectoryExists(FwkeCookiePath) and Assigned(wkeSetCookieJarPath) then
666676
wkeSetCookieJarPath(thewebview, PwideChar(FwkeCookiePath));
667677
wkeSetCspCheckEnable(thewebview, False); //关闭跨域检查
668-
// wkeSetHeadlessEnabled(thewebview,True);// FHeadLess);
678+
wkeSetHeadlessEnabled(thewebview, FHeadLess);
669679
jsBindFunction('GetSource', DoGetSource, 1);
670680
end;
671681
end;
672682

673-
function TWkePopupPage.GetSource(const delay:Integer): string;
674-
begin
675-
result := '';
676-
if Assigned(thewebview) then
677-
ExecuteJavascript('GetSource(document.getElementsByTagName("html")[0].outerHTML);');
678-
Sleep(delay);
679-
result := tmpSource;
680-
end;
683+
681684

682685

683686

@@ -686,8 +689,8 @@ function TWkePopupPage.GetSource(const delay:Integer): string;
686689
constructor TWkeGetSource.Create;
687690
begin
688691
Fwke := TWkePopupPage.Create(nil);
689-
Fwke.WindowLeft := -600;
690-
Fwke.WindowTop := -480;
692+
Fwke.WindowLeft := 0; // -600;
693+
Fwke.WindowTop := 0; // -480;
691694
Fwke.WindowWidth := 600;
692695
Fwke.WindowHeight := 480;
693696
// Fwke.OnAlertBox :=DoAlertBox;
@@ -707,6 +710,11 @@ procedure TWkeGetSource.DoAlertBox(Sender: TObject; sMsg: string);
707710
//do nothing
708711
end;
709712

713+
function TWkeGetSource.getReady: boolean;
714+
begin
715+
result := Fwke.isReady;
716+
end;
717+
710718
function TWkeGetSource.GetSourceHtml: string;
711719
begin
712720
result := Fwke.GetSource;
@@ -730,10 +738,10 @@ procedure TWkeGetSource.LoadUrl(const Aurl: string);
730738

731739
procedure TWkeGetSource.ShowWebPage(const bVisible, bHeadLess: Boolean);
732740
begin
733-
Fwke.CreateWebView;
734741
Fwke.Headless := bHeadLess;
735-
if not bVisible then
736-
ShowWindow(Fwke.WindowHandle, SW_HIDE);
742+
Fwke.FVisible := bVisible;
743+
Fwke.CreateWebView;
744+
737745
end;
738746

739747
procedure Dodelay(const ADelay: Integer);
@@ -748,14 +756,25 @@ procedure Dodelay(const ADelay: Integer);
748756
end;
749757

750758
function GetSourceByWke(const Aurl: string; const AshowWindow, AHeadLess: Boolean; const ADelay: Integer): string; overload;
759+
var
760+
ntry: Integer;
751761
begin
752762
result := '';
753763
with TWkeGetSource.Create do
754764
begin
755765
try
756766
ShowWebPage(AshowWindow, AHeadLess);
757767
LoadUrl(Aurl);
758-
Dodelay(ADelay);
768+
ntry := 0;
769+
while not isReady do
770+
begin
771+
Sleep(100);
772+
Application.ProcessMessages;
773+
Inc(ntry);
774+
if ntry > 100 then
775+
break;
776+
end;
777+
Sleep(100);
759778
result := SourceHtml;
760779
finally
761780
Free;
@@ -769,14 +788,25 @@ function GetSourceByWke(const Aurl: string; const AshowWindow: Boolean; const AD
769788
end;
770789

771790
function GetSourceTextByWke(const Aurl: string; const AshowWindow, AHeadLess: Boolean; const ADelay: Integer): string;
791+
var
792+
ntry: Integer;
772793
begin
773794
result := '';
774795
with TWkeGetSource.Create do
775796
begin
776797
try
777798
ShowWebPage(AshowWindow, AHeadLess);
778799
LoadUrl(Aurl);
779-
Dodelay(ADelay);
800+
ntry := 0;
801+
while not isReady do
802+
begin
803+
Sleep(100);
804+
Application.ProcessMessages;
805+
Inc(ntry);
806+
if ntry > 100 then
807+
break;
808+
end;
809+
Sleep(100);
780810
result := SourceText;
781811
finally
782812
Free;

0 commit comments

Comments
 (0)