-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpuppeteer.ts
101 lines (93 loc) · 2.24 KB
/
puppeteer.ts
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
// A wrapper for puppeteer.
import * as P from "puppeteer"
export const browser = P.launch()
export async function createPage() {
const page = await (await browser).newPage()
return new Page(page)
}
export class Page {
readonly #page: P.Page
constructor(page: P.Page) {
this.#page = page
page.evaluate(`// This injects a box into the page that moves with the mouse;
// Useful for debugging
(function () {
const box = document.createElement('div');
box.classList.add('mouse-helper');
const styleElement = document.createElement('style');
styleElement.innerHTML = \`
.mouse-helper {
pointer-events: none;
position: absolute;
top: 0;
left: 0;
width: 20px;
height: 20px;
background: rgba(0,0,0,.4);
border: 1px solid white;
border-radius: 10px;
margin-left: -10px;
margin-top: -10px;
transition: background .2s, border-radius .2s, border-color .2s;
}
.mouse-helper.button-1 {
transition: none;
background: rgba(0,0,0,0.9);
}
.mouse-helper.button-2 {
transition: none;
border-color: rgba(0,0,255,0.9);
}
.mouse-helper.button-3 {
transition: none;
border-radius: 4px;
}
.mouse-helper.button-4 {
transition: none;
border-color: rgba(255,0,0,0.9);
}
.mouse-helper.button-5 {
transition: none;
border-color: rgba(0,255,0,0.9);
}
\`;
document.head.appendChild(styleElement);
document.body.appendChild(box);
document.addEventListener(
'mousemove',
(event) => {
box.style.left = event.pageX + 'px';
box.style.top = event.pageY + 'px';
updateButtons(event.buttons);
},
true
);
document.addEventListener(
'mousedown',
(event) => {
updateButtons(event.buttons);
box.classList.add('button-' + event.which);
},
true
);
document.addEventListener(
'mouseup',
(event) => {
updateButtons(event.buttons);
box.classList.remove('button-' + event.which);
},
true
);
function updateButtons(buttons) {
for (let i = 0; i < 5; i++)
{box.classList.toggle('button-' + i, buttons & (1 << i));}
}
})();`)
}
setViewport(width: number, height: number) {
this.#page.setViewport({ height, width })
}
screenshot() {
return this.#page.screenshot()
}
}