-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
574 lines (543 loc) · 18.6 KB
/
background.js
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
let requestState = {};
let requestCache = {};
chrome.webRequest.onBeforeRequest.addListener(function(details) {
if (!requestState[details.tabId])
{
requestState[details.tabId] = {};
}
requestState[details.tabId][details.requestId] = "starting";
requestCache[details.requestId] = details;
}, {urls: [ "<all_urls>" ]});
chrome.webRequest.onCompleted.addListener(function(details) {
if (!requestState[details.tabId])
{
requestState[details.tabId] = {};
}
requestState[details.tabId][details.requestId] = "completed";
}, {urls: [ "<all_urls>" ]});
chrome.webRequest.onBeforeRedirect.addListener(function(details) {
if (!requestState[details.tabId])
{
requestState[details.tabId] = {};
}
requestState[details.tabId][details.requestId] = "redirecting";
}, {urls: [ "<all_urls>" ]});
chrome.webRequest.onErrorOccurred.addListener(function(details) {
if (!requestState[details.tabId])
{
requestState[details.tabId] = {};
}
requestState[details.tabId][details.requestId] = "error";
}, {urls: [ "<all_urls>" ]});
const browseAction = async function(tabId, allFlag = false, numSteps = -1, pauseMs = 1000, tries = 30) {
let thisTarget = await getDebuggerTarget(tabId);
let connected = false;
//let res = await sendDebuggerCommand(thisTarget, "Page.AddScriptToEvaluateOnNewDocument", {
// source: "Object.defineProperty(window, 'hidden', { get: () => false });"
//});
let waiting = 0;
let thisHost = hostCache[tabId];
if (!thisHost)
{
console.log("Cannot find which host is loaded in this tab");
return false;
}
playing[tabId] = true;
let stepsRun = 0;
let inprogress = 0;
let setStepBackward = false;
while (waiting < tries && (numSteps == -1 || stepsRun < numSteps))
{
if (allFlag && currentStep[tabId] == 0 && waiting == 0)
{
// Wait before moving to new site, in case download is still going
if (playAllList[tabId].indexOf(thisHost) != 0)
{
await timeout(pauseMs*5);
if (!playing[tabId])
{
break;
}
}
let goUrl = "https://"+thisHost+"/";
console.log("Browsing to "+goUrl);
let beginTime = null;
if (lastLoadCache[tabId])
{
beginTime = lastLoadCache[tabId];
}
chrome.tabs.update(tabId, {
url: goUrl
});
// Wait for new page to load
for (let i = 0; i < 15; i++)
{
await timeout(1000);
if (!playing[tabId])
{
break;
}
if (beginTime != lastLoadCache[tabId])
{
beginTime = lastLoadCache[tabId];
break;
}
}
// Wait for page to get moving
for (let i = 0; i < 5; i++)
{
if (beginTime != lastLoadCache[tabId])
{
console.log("Saw new page load again, resetting timer");
i = 0;
beginTime = lastLoadCache[tabId];
}
await timeout(1000);
if (!playing[tabId])
{
break;
}
}
if (!playing[tabId])
{
break;
}
}
if (!connected)
{
await connectDebugger(thisTarget, '1.3');
connected = true;
}
if (pauseMs)
{
await timeout(pauseMs);
}
else if (waiting > 0)
{
await timeout(1000);
}
if (!playing[tabId])
{
break;
}
let stepresult = await getData('steps_'+thisHost);
if (currentStep[tabId] > stepresult.length-1)
{
if (allFlag)
{
currentStep[tabId] = 0;
let thisInd = playAllList[tabId].indexOf(thisHost);
if (thisInd >= playAllList[tabId].length - 1 || thisInd == -1)
{
break;
}
thisHost = playAllList[tabId][thisInd+1];
hostCache[tabId] = thisHost;
continue;
}
else
{
break;
}
}
let step = stepresult[currentStep[tabId]];
let frames = await getAllFrames(tabId);
let frameId = 0;
let result = null;
inprogress = 0;
if (requestState[tabId])
{
for (var rid in requestState[tabId])
{
if (requestState[tabId][rid] == "starting")
{
inprogress++;
}
}
}
if (inprogress <= 0)
{
for (var i = 0; i < frames.length; i++)
{
frameId = frames[i].frameId;
try
{
result = await sendMessage(tabId, frameId, {command: "getBounds", xpaths: step.xpaths});
if (result && result.bounds && result.bounds.width > 0 && result.bounds.height > 0)
{
break;
}
}
catch (err)
{
console.log(err);
}
}
if (result && result.bounds && result.bounds.width > 0 && result.bounds.height > 0)
{
// Reset setStepBackward since we found something to action
setStepBackward = false;
if (pauseMs)
{
await timeout(pauseMs);
}
if (!playing[tabId])
{
break;
}
waiting = 0;
if (step.command == "click")
{
try
{
let reply = await sendMessage(tabId, frameId, {command:"scrollIntoView", xpaths: step.xpaths});
if (reply.result != "success")
{
console.log("Step "+(currentStep[tabId]+1).toString()+" scrollIntoView failed");
}
}
catch (err)
{
console.log("Failed to send message to tab/frame that we found match on ");
console.log(err);
}
// Get bounds again since scrolling may have made it go out of view
try
{
result = await sendMessage(tabId, frameId, {command: "getBounds", xpaths: step.xpaths});
}
catch (err)
{
console.log(err);
}
// Wait half a second
await timeout(500);
if (!playing[tabId])
{
break;
}
// Mouse over
console.log("moving mouse to "+(result.bounds.x+result.bounds.width/2).toString()+" "+(result.bounds.y+result.bounds.height/2).toString());
try
{
let response = await sendDebuggerCommand(thisTarget, "Input.dispatchMouseEvent", {
type: 'mouseMoved',
x: result.bounds.x+result.bounds.width/2,
y: result.bounds.y+result.bounds.height/2,
modifiers: 0,
});
}
catch (err)
{
console.log("Failed to send debugger commands to mouse over");
console.log(err);
//break;
}
// Wait half a second
await timeout(500);
if (!playing[tabId])
{
break;
}
console.log("Clicking");
// Send click down and up
try
{
let response = await sendDebuggerCommand(thisTarget, "Input.dispatchMouseEvent", {
type: 'mousePressed',
button: 'left',
x: result.bounds.x+result.bounds.width/2,
y: result.bounds.y+result.bounds.height/2,
modifiers: 0,
clickCount: 1
});
// Wait 1/20 of a second
await timeout(50);
response = await sendDebuggerCommand(thisTarget, "Input.dispatchMouseEvent", {
type: 'mouseReleased',
button: 'left',
x: result.bounds.x+result.bounds.width/2,
y: result.bounds.y+result.bounds.height/2,
modifiers: 0,
clickCount: 1
});
currentStep[tabId]++;
stepsRun++;
}
catch (err)
{
console.log("Failed to send debugger commands");
console.log(err);
//break;
}
}
else
{
try
{
let reply = await sendMessage(tabId, frameId, step);
if (reply.result != "success")
{
console.log("Step "+(currentStep[tabId]+1).toString()+" failed");
break;
}
else
{
currentStep[tabId]++;
stepsRun++;
}
}
catch (err)
{
console.log("Failed to send message to tab/frame that we found match on ");
console.log(err);
//break;
}
}
}
else
{
waiting++;
// If we've been waiting for 75% of the total time we're going to wait, try setting the step backwards
// and see if we re-running the previous step gets us going again.
if (!setStepBackward && waiting > tries * 0.75 && currentStep[tabId] > 0)
{
currentStep[tabId]--;
stepsRun--;
setStepBackward = true;
console.log("Set the step backwards to re-try previous step");
}
}
}
else
{
waiting++;
}
}
if (setStepBackward)
{
// We we set step backward, let's set it back forwards now so the user knows where we got stuck
currentStep[tabId]++;
}
if (waiting >= tries)
{
if (inprogress > 0)
{
alert("Network requests are still pending and therefore Webhelper timed out");
for (var rid in requestState[tabId])
{
if (requestState[tabId][rid] == "starting")
{
console.log(requestCache[rid]);
}
}
}
console.log("Waited too long and did not find element");
await disconnectDebugger(thisTarget);
return false;
}
if (!playing[tabId])
{
console.log("Play has been interrupted by user");
}
await disconnectDebugger(thisTarget);
return true;
};
let currentStep = {};
let hostCache = {};
let lastLoadCache = {};
let playing = {};
let playAll = {};
let playAllList = {};
let playAction = async function(allFlag, num = -1) {
let tabs = await getTabs({currentWindow: true, active: true});
console.log("Play action for tab " + tabs[0].id.toString());
let thisHost = hostCache[tabs[0].id];
if (!thisHost)
{
alert("Error! Could not find any domain for this tab!");
}
else
{
let stepresult = await getData('steps_'+thisHost);
if (!stepresult)
{
alert("Error, found the following host for this tab but no steps defined: " + thisHost);
return;
}
await browseAction(tabs[0].id, allFlag, num);
}
};
const loadAll = async function(tabId) {
let alldata = await getData(null);
playAllList[tabId] = [];
for (let k in alldata)
{
let sp = k.split('_');
if (sp[0] == "steps" && sp.length > 1)
{
playAllList[tabId].push(sp[1]);
}
}
console.log("setting tab " +tabId.toString()+" to "+playAllList[tabId][0]);
hostCache[tabId] = playAllList[tabId][0];
currentStep[tabId] = 0;
};
const onMessageFunc = async function(msg, sender, sendResponse) {
if (msg.command == "addstep")
{
let win = await getCurrentWindow();
let imageData = await captureVisibleTab(win.id, {format: 'png', quality: 100});
let imgObject = await loadImage(imageData);
var tnCanvas = document.createElement('canvas');
var tnCanvasContext = tnCanvas.getContext('2d');
var scale = 1;
tnCanvas.width = msg.step.sz.x/scale;
tnCanvas.height = msg.step.sz.y/scale;
var bufferCanvas = document.createElement('canvas');
var bufferContext = bufferCanvas.getContext('2d');
bufferCanvas.width = imgObject.width;
bufferCanvas.height = imgObject.height;
bufferContext.drawImage(imgObject, 0, 0, imgObject.width, imgObject.height);
tnCanvasContext.drawImage(bufferCanvas, msg.step.start.x, msg.step.start.y, msg.step.sz.x, msg.step.sz.y, 0, 0, msg.step.sz.x/scale, msg.step.sz.y/scale);
msg.step.sz.x = msg.step.sz.x/scale;
msg.step.sz.y = msg.step.sz.y/scale;
var step = msg.step;
step.image = tnCanvas.toDataURL();
console.log(step.image);
let steps = await getData('steps_'+msg.host);
if (!steps)
{
steps = [];
}
console.log(currentStep[sender.tab.id]);
steps.splice(currentStep[sender.tab.id], 0, msg.step);
console.log(steps);
await setData('steps_'+msg.host, steps);
sendResponse({success: true});
}
else if (msg.command == "clearsteps")
{
await setData('steps_'+msg.host, []);
console.log("Cleared steps");
sendResponse({success: true});
}
else if (msg.command == "clearlaststep")
{
let result = await getData('steps_'+msg.host);
let val = (result && result.length > 0) ? result.slice(0, -1) : [];
await setData('steps_'+msg.host, val);
console.log("Cleared last step");
sendResponse({success: true});
}
else if (msg.command == "runlaststep")
{
try
{
let success_or_failure = await browseAction(sender.tab.id, false, 1, 0, 1);
sendResponse({success: success_or_failure});
}
catch (err)
{
console.log("failed to run last step");
sendResponse({success: false});
throw err;
}
}
else if (msg.command == "getsteps")
{
let tabs = await getTabs({currentWindow: true, active: true});
let result = await getData('steps_'+hostCache[tabs[0].id]);
sendResponse({success: true, steps: result, currentStep: currentStep[tabs[0].id]});
}
else if (msg.command == "selectstep")
{
let tabs = await getTabs({currentWindow: true, active: true});
currentStep[tabs[0].id] = msg.stepNum;
sendResponse({success: true});
}
else if (msg.command == "deletestep")
{
try
{
let tabs = await getTabs({currentWindow: true, active: true});
let result = await getData('steps_'+hostCache[tabs[0].id]);
result.splice(msg.stepNum, 1);
await setData('steps_'+hostCache[tabs[0].id], result);
if (currentStep[tabs[0].id] > result.length)
{
currentStep[tabs[0].id] = result.length;
}
sendResponse({success: true});
}
catch(err)
{
sendResponse({success: false});
}
}
else if (msg.command == "whereami")
{
if (!hostCache[sender.tab.id] && sender.frameId == 0)
{
hostCache[sender.tab.id] = msg.host;
currentStep[sender.tab.id] = 0;
}
lastLoadCache[sender.tab.id] = new Date();
sendResponse({host:hostCache[sender.tab.id]});
}
else if (msg.command == "exportimportwindow")
{
chrome.tabs.create({url: chrome.extension.getURL('exportimport.html')}, (tab) => {
})
}
else if (msg.command == "importdata")
{
let dat = JSON.parse(msg.contents);
await clearData();
for (let k in dat)
{
await setData(k, dat[k]);
}
alert("Successfully imported file");
}
else if (msg.command == "exportdata")
{
let alldata = await getData(null);
let blob = new Blob([JSON.stringify(alldata)], {type: "text/plain"});
let url = URL.createObjectURL(blob);
let today = new Date();
chrome.downloads.download({
url: url,
conflictAction: "overwrite",
filename: 'export-'+today.getFullYear().toString()+'-'+(today.getMonth()+1).toString()+'-'+today.getDate().toString()+'.json'
});
}
else if (msg.command == "stop")
{
let tabs = await getTabs({currentWindow: true, active: true});
playing[tabs[0].id] = false;
}
else if (msg.command == "play")
{
playAction(false);
}
else if (msg.command == "playone")
{
playAction(false, 1);
}
else if (msg.command == "playall")
{
let tabs = await getTabs({currentWindow: true, active: true});
if (!playAll[tabs[0].id])
{
playAll[tabs[0].id] = true;
await loadAll(tabs[0].id);
}
playAction(true);
}
};
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
onMessageFunc(msg, sender, sendResponse);
// Return true means hold the message channel open until we send a response
return true;
});