@@ -47,6 +47,7 @@ TCustomWkePage = class(TComponent)
47
47
FWindowWidth: Integer;
48
48
FOnPromptBox: TOnPromptBoxEvent;
49
49
FOnConsoleMessage: TOnConsoleMessgeEvent;
50
+ FisReady: boolean;
50
51
function GetZoom : Integer;
51
52
procedure SetZoom (const Value : Integer);
52
53
@@ -55,8 +56,7 @@ TCustomWkePage = class(TComponent)
55
56
procedure DoWebViewUrlChange (Sender: TObject; sUrl: string);
56
57
procedure DoWebViewLoadStart (Sender: TObject; sUrl: string; navigationType: wkeNavigationType; var Cancel: boolean);
57
58
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);
60
60
procedure DoWebViewAlertBox (Sender: TObject; smsg: string);
61
61
function DoWebViewConfirmBox (Sender: TObject; smsg: string): boolean;
62
62
function DoWebViewPromptBox (Sender: TObject; smsg, defaultres, Strres: string): boolean;
@@ -98,7 +98,7 @@ TCustomWkePage = class(TComponent)
98
98
procedure LoadFile (const AFile: string);
99
99
procedure SetFocusToWebbrowser ;
100
100
function ExecuteJavascript (const js: string): boolean;
101
- function GetSource : string;
101
+ function GetSource ( const delay: Integer = 200 ) : string;
102
102
103
103
// / <summary>
104
104
// / 执行js并得到string返回值
@@ -110,6 +110,7 @@ TCustomWkePage = class(TComponent)
110
110
property LocationTitle: string read GetLocationTitle;
111
111
property LoadFinished: Boolean read GetLoadFinished; // 加载完成
112
112
property Transparent: Boolean read GetTransparent write SetTransparent;
113
+ property Mainwkeview: TWkeWebView read thewebview;
113
114
published
114
115
property Caption: string write SetCaption;
115
116
property WindowLeft: Integer read FWindowLeft write FWindowLeft;
@@ -119,6 +120,7 @@ TCustomWkePage = class(TComponent)
119
120
property UserAgent: string read FwkeUserAgent write FwkeUserAgent;
120
121
property HtmlFile: string read FHtmlFile write FHtmlFile;
121
122
property WindowHandle: Hwnd read GetWebHandle;
123
+ property isReady: boolean read FisReady write FisReady;
122
124
property Headless: Boolean write SetHeadless;
123
125
property CookieEnabled: Boolean read FCookieEnabled write FCookieEnabled default true;
124
126
property CookiePath: string read FwkeCookiePath write FWkeCookiePath;
@@ -148,12 +150,6 @@ TWkePopupPage = class(TCustomWkePage)
148
150
FVisible, FHeadLess: Boolean;
149
151
protected
150
152
procedure CreateWebView ; override;
151
- public
152
- // / <summary>
153
- // / 取源码
154
- // / </summary>
155
- function GetSource (const delay:Integer=200 ): string;
156
-
157
153
end ;
158
154
159
155
TWkeGetSource = class
@@ -162,13 +158,15 @@ TWkeGetSource = class
162
158
function GetSourceHtml : string;
163
159
function GetSourceText : string;
164
160
procedure DoAlertBox (Sender: TObject; sMsg: string);
161
+ function getReady : boolean;
165
162
public
166
163
constructor Create();
167
164
destructor Destroy; override;
168
165
procedure ShowWebPage (const bVisible, bHeadLess: Boolean);
169
166
procedure LoadUrl (const Aurl: string);
170
167
property SourceHtml: string read GetSourceHtml;
171
168
property SourceText: string read GetSourceText;
169
+ property isReady: boolean read getReady;
172
170
end ;
173
171
174
172
// / <summary>
@@ -183,11 +181,20 @@ function GetSourceByWke(const Aurl: string; const AshowWindow, AHeadLess: Boolea
183
181
184
182
function GetSourceTextByWke (const Aurl: string; const AshowWindow, AHeadLess: Boolean; const ADelay: Integer): string;
185
183
184
+ var
185
+ tmpSource: string = ' ' ;
186
+
186
187
implementation
187
188
188
- uses
189
+ uses // Vcl.Dialogs,
189
190
math;
190
191
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
+
191
198
procedure DoTitleChange (webView: wkeWebView; param: Pointer; title: wkeString); cdecl;
192
199
begin
193
200
TCustomWkePage(param).DoWebViewTitleChange(TCustomWkePage(param), wkeWebView.GetString(title));
@@ -212,8 +219,7 @@ function DoLoadStart(webView: wkeWebView; param: Pointer; navigationType: wkeNav
212
219
result := not cancel;
213
220
end ;
214
221
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;
217
223
begin
218
224
TCustomWkePage(param).DoWebViewCreateView(TCustomWkePage(param), wkeWebView.GetString(url), navigationType, windowFeatures, result);
219
225
end ;
@@ -235,15 +241,12 @@ function DoConfirmBox(webView: wkeWebView; param: Pointer; msg: wkeString): Bool
235
241
236
242
function DoPromptBox (webView: wkeWebView; param: Pointer; msg: wkeString; defaultResult: wkeString; sresult: wkeString): Boolean; cdecl;
237
243
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));
240
245
end ;
241
246
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;
244
248
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));
247
250
end ;
248
251
249
252
procedure DoDocumentReady (webView: wkeWebView; param: Pointer); cdecl;
@@ -261,14 +264,12 @@ procedure DoWindowDestroy(webWindow: wkeWebView; param: Pointer); cdecl;
261
264
TCustomWkePage(param).DoWebViewWindowDestroy(TCustomWkePage(param));
262
265
end ;
263
266
264
- var
265
- tmpSource: string = ' ' ;
266
-
267
267
function DoGetSource (p1, p2, es: jsExecState): jsValue;
268
268
var
269
269
s: string;
270
270
begin
271
271
s := es.ToTempString(es.Arg(0 ));
272
+ // ShowMessage(s);
272
273
tmpSource := s;
273
274
result := 0 ;
274
275
end ;
@@ -285,8 +286,7 @@ constructor TCustomWkePage.Create(AOwner: TComponent);
285
286
FWindowTop := 10 ;
286
287
FWindowWidth := 640 ;
287
288
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' ;
290
290
end ;
291
291
292
292
destructor TCustomWkePage.Destroy;
@@ -329,22 +329,21 @@ function TCustomWkePage.DoWebViewConfirmBox(Sender: TObject; smsg: string): bool
329
329
FOnConfirmBox(self, smsg, result);
330
330
end ;
331
331
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);
334
333
begin
335
334
if Assigned(FOnConsoleMessage) then
336
335
FOnConsoleMessage(self, AMessage, sourceName, sourceLine);
337
336
end ;
338
337
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);
341
339
begin
342
340
if Assigned(FOnCreateView) then
343
341
FOnCreateView(self, sUrl, navigationType, windowFeatures, wvw);
344
342
end ;
345
343
346
344
procedure TCustomWkePage.DoWebViewDocumentReady (Sender: TObject);
347
345
begin
346
+ FisReady := true;
348
347
if Assigned(FOnDocumentReady) then
349
348
FOnDocumentReady(Self);
350
349
end ;
@@ -358,6 +357,7 @@ procedure TCustomWkePage.DoWebViewLoadEnd(Sender: TObject; sUrl: string; loadres
358
357
359
358
procedure TCustomWkePage.DoWebViewLoadStart (Sender: TObject; sUrl: string; navigationType: wkeNavigationType; var Cancel: boolean);
360
359
begin
360
+ FisReady := false;
361
361
if Assigned(FOnLoadStart) then
362
362
FOnLoadStart(self, sUrl, navigationType, Cancel);
363
363
FLoadFinished := false;
@@ -475,9 +475,14 @@ function TCustomWkePage.GetMediaVolume: Single;
475
475
result := thewebview.MediaVolume;
476
476
end ;
477
477
478
- function TCustomWkePage.GetSource : string; // 获取源码
478
+ function TCustomWkePage.GetSource ( const delay: Integer = 200 ) : string; // 获取源码
479
479
begin
480
480
result := ' ' ;
481
+ if Assigned(thewebview) then
482
+ ExecuteJavascript(' GetSource(document.getElementsByTagName("html")[0].outerHTML);' );
483
+ Sleep(delay);
484
+ result := tmpSource;
485
+ // showmessage(tmpSource);
481
486
end ;
482
487
483
488
function TCustomWkePage.GetTransparent : boolean;
@@ -619,14 +624,18 @@ procedure TWkeTransparentPage.CreateWebView;
619
624
if Assigned(FOnPromptBox) then
620
625
thewebview.SetOnPromptBox(DoPromptBox, self);
621
626
thewebview.SetOnConsoleMessage(DoConsoleMessage, self);
622
- thewebview.SetOnDocumentReady(DoDocumentReady, self);
627
+ // thewebview.SetOnDocumentReady(DoDocumentReady, self);
628
+ wkeOnDocumentReady2(thewebview, doDucumentReadyCallback, Self);
623
629
thewebview.SetOnWindowClosing(DoWindowClosing, self);
624
630
thewebview.SetOnWindowDestroy(DoWindowDestroy, self);
625
631
if FwkeUserAgent <> ' ' then
626
632
wkeSetUserAgent(thewebview, PansiChar(AnsiString(FwkeUserAgent)));
627
633
wkeSetCookieEnabled(thewebview, FCookieEnabled);
628
634
if DirectoryExists(FwkeCookiePath) and Assigned(wkeSetCookieJarPath) then
629
635
wkeSetCookieJarPath(thewebview, PwideChar(FwkeCookiePath));
636
+
637
+ wkeSetCspCheckEnable(thewebview, False); // 关闭跨域检查
638
+ jsBindFunction(' GetSource' , DoGetSource, 1 );
630
639
end ;
631
640
632
641
end ;
@@ -638,10 +647,10 @@ procedure TWkePopupPage.CreateWebView;
638
647
thewebview := wkeCreateWebWindow(WKE_WINDOW_TYPE_POPUP, 0 , FWindowLeft, FWindowTop, FWindowWidth, FWindowHeight);
639
648
if Assigned(thewebview) then
640
649
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 );
645
654
thewebview.SetOnTitleChanged(DoTitleChange, self);
646
655
thewebview.SetOnURLChanged(DoUrlChange, self);
647
656
thewebview.SetOnNavigation(DoLoadStart, self);
@@ -656,7 +665,8 @@ procedure TWkePopupPage.CreateWebView;
656
665
if Assigned(FOnPromptBox) then
657
666
thewebview.SetOnPromptBox(DoPromptBox, self);
658
667
thewebview.SetOnConsoleMessage(DoConsoleMessage, self);
659
- thewebview.SetOnDocumentReady(DoDocumentReady, self);
668
+ // thewebview.SetOnDocumentReady(DoDocumentReady, self);
669
+ wkeOnDocumentReady2(thewebview, doDucumentReadyCallback, Self);
660
670
thewebview.SetOnWindowClosing(DoWindowClosing, self);
661
671
thewebview.SetOnWindowDestroy(DoWindowDestroy, self);
662
672
if FwkeUserAgent <> ' ' then
@@ -665,19 +675,12 @@ procedure TWkePopupPage.CreateWebView;
665
675
if DirectoryExists(FwkeCookiePath) and Assigned(wkeSetCookieJarPath) then
666
676
wkeSetCookieJarPath(thewebview, PwideChar(FwkeCookiePath));
667
677
wkeSetCspCheckEnable(thewebview, False); // 关闭跨域检查
668
- // wkeSetHeadlessEnabled(thewebview,True);// FHeadLess);
678
+ wkeSetHeadlessEnabled(thewebview, FHeadLess);
669
679
jsBindFunction(' GetSource' , DoGetSource, 1 );
670
680
end ;
671
681
end ;
672
682
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
+
681
684
682
685
683
686
@@ -686,8 +689,8 @@ function TWkePopupPage.GetSource(const delay:Integer): string;
686
689
constructor TWkeGetSource.Create;
687
690
begin
688
691
Fwke := TWkePopupPage.Create(nil );
689
- Fwke.WindowLeft := -600 ;
690
- Fwke.WindowTop := -480 ;
692
+ Fwke.WindowLeft := 0 ; // -600;
693
+ Fwke.WindowTop := 0 ; // -480;
691
694
Fwke.WindowWidth := 600 ;
692
695
Fwke.WindowHeight := 480 ;
693
696
// Fwke.OnAlertBox :=DoAlertBox;
@@ -707,6 +710,11 @@ procedure TWkeGetSource.DoAlertBox(Sender: TObject; sMsg: string);
707
710
// do nothing
708
711
end ;
709
712
713
+ function TWkeGetSource.getReady : boolean;
714
+ begin
715
+ result := Fwke.isReady;
716
+ end ;
717
+
710
718
function TWkeGetSource.GetSourceHtml : string;
711
719
begin
712
720
result := Fwke.GetSource;
@@ -730,10 +738,10 @@ procedure TWkeGetSource.LoadUrl(const Aurl: string);
730
738
731
739
procedure TWkeGetSource.ShowWebPage (const bVisible, bHeadLess: Boolean);
732
740
begin
733
- Fwke.CreateWebView;
734
741
Fwke.Headless := bHeadLess;
735
- if not bVisible then
736
- ShowWindow(Fwke.WindowHandle, SW_HIDE);
742
+ Fwke.FVisible := bVisible;
743
+ Fwke.CreateWebView;
744
+
737
745
end ;
738
746
739
747
procedure Dodelay (const ADelay: Integer);
@@ -748,14 +756,25 @@ procedure Dodelay(const ADelay: Integer);
748
756
end ;
749
757
750
758
function GetSourceByWke (const Aurl: string; const AshowWindow, AHeadLess: Boolean; const ADelay: Integer): string; overload;
759
+ var
760
+ ntry: Integer;
751
761
begin
752
762
result := ' ' ;
753
763
with TWkeGetSource.Create do
754
764
begin
755
765
try
756
766
ShowWebPage(AshowWindow, AHeadLess);
757
767
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 );
759
778
result := SourceHtml;
760
779
finally
761
780
Free;
@@ -769,14 +788,25 @@ function GetSourceByWke(const Aurl: string; const AshowWindow: Boolean; const AD
769
788
end ;
770
789
771
790
function GetSourceTextByWke (const Aurl: string; const AshowWindow, AHeadLess: Boolean; const ADelay: Integer): string;
791
+ var
792
+ ntry: Integer;
772
793
begin
773
794
result := ' ' ;
774
795
with TWkeGetSource.Create do
775
796
begin
776
797
try
777
798
ShowWebPage(AshowWindow, AHeadLess);
778
799
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 );
780
810
result := SourceText;
781
811
finally
782
812
Free;
0 commit comments