-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.js
657 lines (614 loc) · 21.3 KB
/
run.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
const puppeteer = require("puppeteer-core");
const SmartBuffer = require("smart-buffer").SmartBuffer;
const getPixels = require("get-pixels");
const findChrome = require("chrome-finder");
const awaitifyStream = require("awaitify-stream");
const { getElements } = require("./getElements");
const uuid = require("uuid").v1;
Array.prototype.flat = function () {
return this.reduce((acc, x) => acc.concat(x), []);
};
Array.prototype.flatMap = function (mapper) {
return this.map(mapper).flat();
};
SmartBuffer.prototype.writeStringPrependSize = function (string) {
const buffer = Buffer.from(string, "utf8");
this.writeUInt32LE(buffer.length);
this.writeBuffer(buffer);
};
const ID_ATTRIBUTE = "data-magic-mouse-id";
const IMAGE_FORMAT = "jpeg"; // "raw", "jpeg", "png"
console.error("Node.js arguments", process.argv);
console.error("Image format", IMAGE_FORMAT);
const run = async () => {
const url = process.argv[process.argv.length - 5];
const screenSize = {
x: parseInt(process.argv[process.argv.length - 4], 10),
y: parseInt(process.argv[process.argv.length - 3], 10),
};
const headless = process.argv[process.argv.length - 2] === "headless";
const chromeProfilePath = process.argv[process.argv.length - 1];
const terminate = async () => {
console.error("Terminating...");
// await page.stopScreencast();
await browser.close();
process.exit();
};
const sendCommand = (buffer) => {
const lenBuffer = new Buffer(4);
lenBuffer.writeUInt32BE(buffer.length);
process.stdout.write(lenBuffer);
process.stdout.write(buffer);
};
const browser = await puppeteer.launch({
ignoreDefaultArgs: true,
args: [
"--enable-automation",
...(headless ? ["--headless"] : []),
// '--start-fullscreen',
"--force-device-scale-factor=1",
`--user-data-dir=${chromeProfilePath}`,
],
executablePath: findChrome(),
});
const page = await browser.newPage();
await page.setBypassCSP(true);
await page.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
);
await page.setViewport({ width: screenSize.x, height: screenSize.y });
process.on("SIGTERM", terminate);
const instrumentGoogleSlides = async () => {
if (!page.url().startsWith("https://docs.google.com/presentation")) {
return;
}
console.error("Instrumenting Google Slides");
const morphPositions = (
await Promise.all(
page
.frames()
.filter((frame) => !frame.isDetached())
.map(async (frame) =>
frame.evaluate(
(ID_ATTRIBUTE) =>
Promise.all(
Array.from(
new Set(
Array.from(
document.querySelectorAll('.punch-viewer-svgpage g[id*="paragraph"]'),
).map((element) => element.parentNode),
),
)
.map((element) => ({
element,
text: Array.from(element.querySelectorAll('g[id*="paragraph"]'))
.map((paragraph) =>
Array.from(paragraph.querySelectorAll(".sketchy-text-content-text text"))
.map((text) => text.textContent)
.join(" "),
)
.join("\r"),
}))
.filter((tmp) => tmp.text.startsWith("!"))
.map((tmp) => {
let element = tmp.element;
let path = null;
do {
element = element.parentNode;
path = element.querySelector("path");
} while (path === null);
return { ...tmp, path };
})
.map(async (tmp) => {
const rect = tmp.path.getBoundingClientRect();
let id = tmp.path.getAttribute(ID_ATTRIBUTE);
if (!id) {
id = await window.uuid();
tmp.path.setAttribute(ID_ATTRIBUTE, id);
}
return {
id,
type: "morph",
x: rect.x,
y: rect.y,
w: rect.width,
h: rect.height,
data: tmp.text,
};
}),
),
ID_ATTRIBUTE,
),
),
)
).flat();
console.error("Google Slides Morph Positions", morphPositions);
morphPositions.forEach(sendPortalDataCommand);
};
const instrumentGitHub = async () => {
if (!page.url().startsWith("https://github.com/")) {
return;
}
console.error("Instrumenting clone button");
page.evaluate(() => {
const bar = document.getElementsByClassName("file-navigation");
if (bar.length === 0) {
return;
}
const urlField = document.querySelector(".https-clone-options input");
if (!urlField) {
return;
}
const cloneUrl = urlField.value;
let name = cloneUrl.split("/")[4];
name = name.substr(0, name.length - 4);
bar[0].insertAdjacentHTML(
"beforeEnd",
`<span class="btn btn-sm btn-primary ml-2" onClick="gitClone('${name}', '${cloneUrl}')">Clone to Squeak</span>`,
);
const normalCloneButton = document.getElementsByClassName("get-repo-select-menu");
if (normalCloneButton.length === 0 || normalCloneButton[0].children.length === 0) {
return;
}
normalCloneButton[0].children[0].classList.remove("btn-primary");
});
};
const parseLDJsons = async () => {
const ldJsons = await page.$$eval('script[type="application/ld+json"]', (nodes) =>
nodes.map((node) => JSON.parse(node.innerText)),
);
console.error("LD JSON", ldJsons);
const buf = new SmartBuffer();
buf.writeString("s");
buf.writeUInt32LE(ldJsons.length);
ldJsons.forEach((ldJson) => {
const json = JSON.stringify(ldJson);
buf.writeStringPrependSize(json);
});
await sendCommand(buf.toBuffer());
};
const ignoreExecutionContextDestroyed = (error) => {
// Sometimes the frame is destroyed right after navigation, thus throwing an error
// when trying to evaluate a function in the frame's context. That's why we catch
// those errors here.
if (
!error.message.endsWith(
"Execution context was destroyed, most likely because of a navigation.",
) &&
!error.message.endsWith("Cannot find context with specified id")
) {
throw error;
}
};
page.on("dialog", async (dialog) => {
// TODO: We could send the dialog contents to Squeak and ask the user what to do.
// For now, accept all dialogs, so that the page remains responsive.
console.error("Dialog!", dialog.defaultValue(), dialog.message(), dialog.type());
await dialog.accept();
});
page.on("framenavigated", async (frame) => {
await new Promise((resolve) => setTimeout(() => resolve(), 50));
try {
await parseLDJsons();
await instrumentGoogleSlides();
await instrumentGitHub();
if (!frame.parentFrame()) {
const url = page.url();
console.error(`Navigating to ${url}`);
const buf = new SmartBuffer();
buf.writeString("l");
buf.writeString(url);
sendCommand(buf.toBuffer());
}
} catch (error) {
ignoreExecutionContextDestroyed(error);
}
});
page.on("domcontentloaded", async () => {
await parseLDJsons();
// TODO: Is this needed when we have framenavigated?
// await instrumentGitHub();
// await instrumentGoogleSlides();
});
const refreshTrackedElements = async () => {
const trackedElements = (
await Promise.all(
page
.frames()
.filter((frame) => !frame.isDetached())
.map((frame) =>
frame.evaluate(getElements, "refreshInfo").catch((error) => {
ignoreExecutionContextDestroyed(error);
return [];
}),
),
)
).flat();
// console.error(trackedElements);
const buf = new SmartBuffer();
buf.writeString("hr"); // portal refresh
buf.writeUInt32LE(trackedElements.length);
trackedElements.forEach((element) =>
buf
.writeStringNT(element.id)
.writeInt32LE(element.x)
.writeInt32LE(element.y)
.writeInt32LE(element.w)
.writeInt32LE(element.h),
);
sendCommand(buf.toBuffer());
};
page.on("screencastframe", async (frame) => {
const screenshot = Buffer.from(frame.data, "base64");
const buf = new SmartBuffer();
if (IMAGE_FORMAT === "png") {
buf.writeString("ip");
buf.writeBuffer(screenshot);
} else if (IMAGE_FORMAT === "jpeg") {
buf.writeString("ij");
buf.writeBuffer(screenshot);
} else if (IMAGE_FORMAT === "raw") {
const pixels = await new Promise((resolve, reject) =>
getPixels(screenshot, "image/png", (err, pixels) => {
if (err) {
reject(err);
} else {
resolve(pixels);
}
}),
);
buf.writeString("ir");
buf.writeUInt32LE(pixels.shape[0]);
buf.writeUInt32LE(pixels.shape[1]);
buf.writeBuffer(Buffer.from(pixels.data));
} else {
throw new Error(`Unsupported image format ${IMAGE_FORMAT}.`);
}
sendCommand(buf.toBuffer());
await refreshTrackedElements();
await page.screencastFrameAck(frame.sessionId);
console.error(`Sent frame at ${Date.now() / 1000}`);
});
await page.exposeFunction("uuid", () => uuid());
await page.exposeFunction("gitClone", async (name, url) => {
console.error("GIT CLONE", name, url);
const buf = new SmartBuffer();
buf.writeString("g");
buf.writeStringPrependSize(name);
buf.writeString(url);
await sendCommand(buf.toBuffer());
});
const sendPortalDataCommand = (data) => {
const buf = new SmartBuffer();
switch (data.type) {
case "img":
case "canvas":
buf.writeString("hi");
break;
case "pre":
buf.writeString("hc");
break;
case "morph":
buf.writeString("hm");
break;
}
buf.writeStringNT(data.id);
buf.writeInt32LE(data.x);
buf.writeInt32LE(data.y);
buf.writeInt32LE(data.w);
buf.writeInt32LE(data.h);
switch (data.type) {
case "img":
case "canvas":
buf.writeBuffer(Buffer.from(data.data, "base64"));
break;
case "pre":
case "morph":
buf.writeString(data.data);
break;
}
sendCommand(buf.toBuffer());
};
// TODO: This throws an error in case the page can't be reached (e.g., when you have no network connection)
console.error(`Navigating to ${url}`);
await page.goto(url);
console.error("Starting screencast...");
await page.startScreencast({
format: IMAGE_FORMAT === "jpeg" ? "jpeg" : "png",
everyNthFrame: 1,
});
console.error("Recording screencast...");
const reader = awaitifyStream.createReader(process.stdin);
while (true) {
const size = (await reader.readAsync(4)).readUInt32BE();
console.error(`Waiting for payload of size ${size}`);
const command = String.fromCharCode((await reader.readAsync(1)).readUInt8());
const payload =
size > 1 ? SmartBuffer.fromBuffer(await reader.readAsync(size - 1)) : new SmartBuffer();
console.error(`Received command ${command} with payload of size ${size}.`);
switch (command) {
case "s": {
const x = payload.readInt32LE();
const y = payload.readInt32LE();
const str = payload.readString();
console.error(`Dropped String at ${x}@${y}: "${str}"`);
await page.evaluateHandle(
(x, y, str) => {
const field = document
.elementsFromPoint(x, y)
.find(
(element) =>
["INPUT", "TEXTAREA"].includes(element.tagName) ||
element.contentEditable === "true",
);
if (!field) {
return;
}
if (field.tagName === "INPUT") {
field.value = str;
} else {
field.innerText = str;
}
},
x,
y,
str,
);
break;
}
case "f": {
const x = payload.readInt32LE();
const y = payload.readInt32LE();
console.error(`DROPPED MORPH AT ${x}@${y}`);
const form = await page.evaluateHandle(
(x, y) => document.elementsFromPoint(x, y).find((element) => element.tagName === "FORM"),
x,
y,
);
const fields =
(await form.jsonValue()) !== undefined
? await form.$$("input:not([type=checkbox])") /* , select */
: [];
const buf = new SmartBuffer();
buf.writeString("f");
const inputs = (
await Promise.all(
fields.map(async (field) => {
if ((await (await field.getProperty("offsetParent")).jsonValue()) === null) {
return undefined;
}
let description = await (await field.getProperty("placeholder")).jsonValue();
if (description === undefined || description.length === 0) {
description = await (await field.getProperty("name")).jsonValue();
}
if (description === undefined || description.length === 0) {
return undefined;
}
const box = (await field.boxModel()).content;
console.error(box);
const boundingBox = {
x: Math.round(box[0].x),
y: Math.round(box[0].y),
width: Math.round(box[2].x - box[0].x),
height: Math.round(box[2].y - box[0].y),
};
console.error(boundingBox);
return {
boundingBox,
description: description,
id: await (
await page.evaluateHandle(
async (element, ID_ATTRIBUTE) => {
let id = element.getAttribute(ID_ATTRIBUTE);
if (!id) {
id = await window.uuid();
element.setAttribute(ID_ATTRIBUTE, id);
}
return id;
},
field,
ID_ATTRIBUTE,
)
).jsonValue(),
};
}),
)
).filter((input) => input !== undefined);
buf.writeInt32LE(inputs.length);
inputs.forEach(({ boundingBox, description, id }) => {
buf.writeInt32LE(boundingBox.x);
buf.writeInt32LE(boundingBox.y);
buf.writeInt32LE(boundingBox.width);
buf.writeInt32LE(boundingBox.height);
buf.writeStringPrependSize(description);
buf.writeStringPrependSize(id);
});
sendCommand(buf.toBuffer());
break;
}
case "t": {
const id = payload.readString(payload.readInt32LE());
const text = payload.readString(payload.readInt32LE());
console.error(`Update Text: ${id} ${text}`);
await page.evaluate(
(id, text, ID_ATTRIBUTE) => {
const element = document.querySelector(`[${ID_ATTRIBUTE}="${id}"]`);
if (!element) {
return;
}
if (element.tagName === "INPUT") {
element.value = text;
} else if (element.tagName === "SELECT") {
const option = Array.from(element.options).find(
(option) => option.innerText.toLocaleLowerCase() === text.toLocaleLowerCase(),
);
if (option) {
element.value = option.value;
}
}
},
id,
text.trim(),
ID_ATTRIBUTE,
);
break;
}
case "k": {
// This command is necessary because the Squeak ProcessWrapper is unable to terminate processes.
await terminate();
break;
}
case "l": {
const url = payload.readString();
console.error(`Navigating to ${url}`);
try {
await page.goto(url);
} catch (error) {
console.error(error);
}
break;
}
case "e": {
// TODO check read boundaries for each event type
const eventType = payload.readUInt8();
console.error("Received event type", eventType);
switch (eventType) {
case 0:
page.mouse.up({ button: "left" });
break;
case 1:
page.mouse.down({ button: "left" });
break;
case 2:
page.mouse.up({ button: "right" });
break;
case 3:
page.mouse.down({ button: "right" });
break;
case 4: {
const x = payload.readUInt32LE();
const y = payload.readUInt32LE();
console.error("Moving to", x, y);
page.mouse.move(x, y);
break;
}
case 5: {
let squeakKeyString = payload.readString(size, "ascii");
let keyName;
const modifiers = [];
if (squeakKeyString.startsWith("<Cmd-")) {
modifiers.push("ControlLeft"); // Not a typo. Squeak send <Cmd-a> when pressing CTRL+A on Windows
squeakKeyString = squeakKeyString.replace("Cmd-", "");
}
if (squeakKeyString.startsWith("<Ctrl-")) {
modifiers.push("ControlLeft");
squeakKeyString = squeakKeyString.replace("Ctrl-", "");
}
if (squeakKeyString.startsWith("<Shift-")) {
modifiers.push("ShiftLeft");
squeakKeyString = squeakKeyString.replace("Shift-", "");
}
if (
squeakKeyString.length === 3 &&
squeakKeyString[0] === "<" &&
squeakKeyString[2] === ">"
) {
squeakKeyString = squeakKeyString[1];
}
if (squeakKeyString.length > 1) {
const conversion = {
"<space>": "Space",
"<tab>": "Tab",
"<cr>": "Enter",
"<lf>": "Enter",
"<enter>": "Enter",
"<backspace>": "Backspace",
"<delete>": "Delete",
"<escape>": "Escape",
"<down>": "ArrowDown",
"<up>": "ArrowUp",
"<left>": "ArrowLeft",
"<right>": "ArrowRight",
"<end>": "End",
"<home>": "Home",
"<pageDown>": "PageDown",
"<pageUp>": "PageUp",
// '<euro>': '', // TODO
"<insert>": "Insert",
};
keyName = conversion[squeakKeyString];
if (keyName === undefined) {
console.error("Unknown key", squeakKeyString);
break;
}
} else {
keyName = squeakKeyString;
}
console.error("Typing", squeakKeyString, modifiers);
try {
await Promise.all(modifiers.map((key) => page.keyboard.down(key)));
await page.keyboard.press(keyName);
await Promise.all(modifiers.map((key) => page.keyboard.up(key)));
} catch (error) {
console.error(error);
}
break;
}
case 6: {
const x = payload.readUInt32LE();
const y = payload.readUInt32LE();
page.setViewport({ width: x, height: y });
break;
}
case 7: {
const y = payload.readUInt32LE();
console.error("scroll", y);
page.evaluate((y) => window.scrollBy(0, y == 0 ? -20 : 20), y);
break;
}
case 8: {
// These are relative to the visible part of the page, not the top left corner
const x = payload.readUInt32LE();
const y = payload.readUInt32LE();
console.error(`Portal event at ${x},${y}`);
let elements;
try {
elements = await page.evaluate(getElements, "extractElements", x, y);
} catch (error) {
elements = [];
console.error(error);
}
if (elements.length === 0) {
console.error("No elements found to create a portal");
break;
}
const element = elements[0];
console.error({
id: element.id,
type: element.type,
x: element.x,
y: element.y,
w: element.w,
h: element.h,
});
sendPortalDataCommand(element);
break;
}
case 9: {
console.error("go back");
await page.goBack();
break;
}
case 10: {
console.error("go forward");
await page.goForward();
break;
}
}
break;
}
}
}
};
// We catch all errors, log them to the console and exit.
run().catch((error) => console.error(error));