-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuCefScriptActionBase.pas
329 lines (293 loc) · 9.25 KB
/
uCefScriptActionBase.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
unit uCefScriptActionBase;
interface
uses
//
System.SysUtils, System.Classes, System.SyncObjs,
//
uCEFTypes, uCEFInterfaces, uCEFProcessMessage, uCEFDownloadImageCallBack,
//
uCefWebAction, uCefScriptBase, uCefWebActionBase, uCefScriptNav, uCefUtilFunc,
uCefUtilType, uCefScriptDict;
type
TCefProcMethod0 = procedure of object;
TCefScriptStep1 = function(const ASender: TCefScriptBase): Boolean of object;
TCefScriptStep1Array = array of TCefScriptStep1;
TCefScriptActionBase = class abstract (TCefScriptBase)
private
procedure RunActionStopFree;
function RunActionStartWait: TWaitResult; overload;
function RunActionStartWait(var AResult: Boolean;
const ASetFail: Boolean): TWaitResult; overload;
protected
FAction: TCefWebActionBase;
FScriptDict: TCefScriptDict;
//---
function DoNavGoBack(const AActionName: string): Boolean;
function RunScript(const AScript: string; const ASetFail: Boolean = True): Boolean;
function RunScriptNav(const AActionName, AUrl: string; const ANavFunc: TCefScriptNavFunc;
const ANavProc0: TCefScriptNavProc0;
const ASetFail, AIsNavigation: Boolean): Boolean; overload;
function RunScriptNav(const AActionName: string; const AUrl: string; const ASetFail: Boolean = True;
const AIsNavigation: Boolean = True): Boolean; overload;
function RunScriptNav(const AActionName: string; const ANavFunc: TCefScriptNavFunc; const ASetFail: Boolean = True;
const AIsNavigation: Boolean = False): Boolean; overload;
function RunScriptNav(const AActionName: string; const ANavProc0: TCefScriptNavProc0; const ASetFail: Boolean = True;
const AIsNavigation: Boolean = False): Boolean; overload;
//---
function RunScriptClickById(const AId: string;
const ASetFail: Boolean = True; const ASetIsNav: Boolean = False): Boolean;
function RunScriptScrollAndClickElement(const ASpeed: TCefUISpeed;
const AElem: TElementParams; const ASetFail: Boolean;
const ASetIsNav: Boolean): Boolean; overload;
function RunScriptScrollAndClickElement(const AElem: TElementParams;
const ASetFail: Boolean = True; const ASetIsNav: Boolean = False): Boolean; overload;
function RunScriptScrollAndClickElementNav(const AElem: TElementParams;
const ASetFail: Boolean = True): Boolean;
//---
function DoStepPause(const ATimeout: string; const ASteps: array of TCefScriptStep1): Boolean; overload;
function DoPause(const ATimeout: string; const ASteps: TCefScriptStep1 = nil): Boolean;
public
destructor Destroy; override;
//---
//---
function Wait: TWaitResult; override;
procedure Abort; override;
//---
procedure SetScriptDict(const A: TCefScriptDict);
property ScriptDict: TCefScriptDict read FScriptDict write SetScriptDict;
end;
implementation
uses
uStringUtils,
//
uCefScriptClickElement, uCefWaitEventList, uCefUIFunc;
{ TCefScriptActionBase }
destructor TCefScriptActionBase.Destroy;
begin
if Assigned(FAction) then
FAction.Free;
inherited;
end;
function TCefScriptActionBase.DoStepPause(const ATimeout: string;
const ASteps: array of TCefScriptStep1): Boolean;
var step: TCefScriptStep1;
begin
for step in ASteps do
begin
if Sleep(RandomRangeStr(ATimeout, PAUSE_STEP_DEF)) <> wrTimeout then
Exit(False);
if Assigned(step) then
if not step(Self) then
Exit(False);
end;
Exit(Sleep(ATimeout) = wrTimeout)
end;
function TCefScriptActionBase.DoPause(const ATimeout: string;
const ASteps: TCefScriptStep1): Boolean;
begin
if Assigned(ASteps) then
Result := DoStepPause(ATimeout, [ASteps])
else
Result := Sleep(ATimeout) = wrTimeout
end;
function TCefScriptActionBase.DoNavGoBack(const AActionName: string): Boolean;
begin
LogInfo('go <- back');
if Chromium.CanGoBack then
begin
Result := RunScriptNav(AActionName,
procedure
begin
Chromium.GoBack();
end,
True, True);
end
else
begin
LogError('not canGoBack');
Result := False
end
end;
function TCefScriptActionBase.RunActionStartWait: TWaitResult;
begin
if FAction.Start() then
Result := FAction.Wait()
else
Result := wrError
end;
function TCefScriptActionBase.RunActionStartWait(var AResult: Boolean;
const ASetFail: Boolean): TWaitResult;
var wr: TWaitResult;
begin
AResult := False;
wr := RunActionStartWait();
if wr = wrSignaled then
begin
FAction.LogDebug('action wrSignaled');
if FAction.IsSuccess then
AResult := True;
end
else
if wr = wrTimeout then
begin
if FAction.IsSuccess then
begin
AResult := True;
FAction.LogDebug('action wrTimeout');
end
else
begin
FAction.LogError('action wrTimeout')
end
end
else
if wr = wrAbandoned then
begin
FAction.LogError('action wrAbandoned');
end
else
if wr = wrError then
begin
FAction.LogError('action wrError');
end;
if ASetFail and FAction.IsFail and not FAction.IgnoreFail then
FFail := True;
Result := wr;
end;
procedure TCefScriptActionBase.RunActionStopFree;
begin
if Assigned(FAction) then
begin
FAction.Abort();
FAction.Wait();
FAction.Free;
FAction := nil;
end;
end;
function TCefScriptActionBase.RunScript(const AScript: string; const ASetFail: Boolean): Boolean;
begin
RunActionStopFree();
try
if not Assigned(FScriptDict) then
begin
LogError('no script dict');
if ASetFail then
FFail := True;
Exit(False)
end;
FAction := FScriptDict.MakeScript(AScript, FController, FLogger, Chromium, FAbortEvent);
if not Assigned(FAction) then
begin
LogError('not found script "%s"', [AScript]);
if ASetFail then
FFail := True;
Exit(False)
end;
RunActionStartWait(Result, ASetFail);
finally
FreeAndNil(FAction);
end;
end;
function TCefScriptActionBase.RunScriptClickById(const AId: string;
const ASetFail, ASetIsNav: Boolean): Boolean;
begin
RunActionStopFree();
try
FAction := TScriptClickElement.Create(FController.Pause, AId, ASetIsNav, Self);
RunActionStartWait(Result, ASetFail);
finally
FreeAndNil(FAction);
end;
end;
function TCefScriptActionBase.RunScriptScrollAndClickElement(const ASpeed: TCefUISpeed;
const AElem: TElementParams; const ASetFail, ASetIsNav: Boolean): Boolean;
var bol: Boolean;
begin
LogDebug('scroll to element');
bol := CefUIScrollToElement(Self, ASpeed, AElem);
if bol then
begin
DoPause(PAUSE_DEF);
LogDebug('mouse move to element');
bol := CefUIMouseMoveToElement(Self, ASpeed, AElem);
if bol then
begin
DoPause(PAUSE_DEF);
bol := RunScriptNav('nav-click',
procedure
begin
LogDebug('click element');
CefUIMouseClick(Self);
end,
ASetFail, ASetIsNav);
//---
if not bol then
LogError('fail click to element');
Exit(bol)
end
else
begin
LogError('fail mouse move to element');
end;
end
else
begin
LogError('fail scroll to element');
end;
Result := False;
end;
function TCefScriptActionBase.RunScriptScrollAndClickElement(
const AElem: TElementParams; const ASetFail, ASetIsNav: Boolean): Boolean;
begin
Result := RunScriptScrollAndClickElement(FController.Speed, AElem, ASetFail, ASetIsNav)
end;
function TCefScriptActionBase.RunScriptScrollAndClickElementNav(
const AElem: TElementParams; const ASetFail: Boolean): Boolean;
begin
Result := RunScriptScrollAndClickElement(FController.Speed, AElem, ASetFail, True)
end;
procedure TCefScriptActionBase.SetScriptDict(const A: TCefScriptDict);
begin
FScriptDict := A
end;
function TCefScriptActionBase.RunScriptNav(const AActionName, AUrl: string;
const ANavFunc: TCefScriptNavFunc; const ANavProc0: TCefScriptNavProc0;
const ASetFail, AIsNavigation: Boolean): Boolean;
begin
RunActionStopFree();
try
FAction := TCefScriptNav.Create(AActionName, AUrl, ANavFunc, ANavProc0, AIsNavigation, Self);
RunActionStartWait(Result, ASetFail);
finally
FreeAndNil(FAction);
end;
end;
function TCefScriptActionBase.RunScriptNav(const AActionName, AUrl: string;
const ASetFail, AIsNavigation: Boolean): Boolean;
begin
Result := RunScriptNav(AActionName, AUrl, nil, nil, ASetFail, AIsNavigation)
end;
function TCefScriptActionBase.RunScriptNav(const AActionName: string;
const ANavFunc: TCefScriptNavFunc; const ASetFail, AIsNavigation: Boolean): Boolean;
begin
Result := RunScriptNav(AActionName, '', ANavFunc, nil, ASetFail, AIsNavigation)
end;
function TCefScriptActionBase.RunScriptNav(const AActionName: string;
const ANavProc0: TCefScriptNavProc0; const ASetFail, AIsNavigation: Boolean): Boolean;
begin
Result := RunScriptNav(AActionName, '', nil, ANavProc0, ASetFail, AIsNavigation)
end;
procedure TCefScriptActionBase.Abort;
begin
if Assigned(FAction) then
FAction.Abort();
inherited;
end;
function TCefScriptActionBase.Wait: TWaitResult;
begin
if Assigned(FAction) then
Result := FAction.Wait()
else
Result := inherited Wait()
end;
end.