-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.ts
191 lines (162 loc) · 4.85 KB
/
App.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
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
import { createCursor } from "ghost-cursor";
import puppeteer, { Page } from "puppeteer";
type Config = {
TW_USERNAME: string;
TW_PASSWORD: string;
TW_SEARCH: string;
CHROME_PATH: string;
};
class App {
public page: Page;
public config: Config;
public cursor: ReturnType<typeof createCursor>;
constructor(config: Config) {
this.config = config;
}
async delay(seconds: number) {
const rnd = Math.random() * (seconds / 15);
const total = (seconds + rnd) * 1000;
return new Promise((r) => setTimeout(r, total));
}
async init() {
if (!this.config.CHROME_PATH) {
throw new Error("No se encontro la ruta de Chrome");
}
console.log(`Chrome: ${this.config.CHROME_PATH}`);
const browser = await puppeteer.launch({
headless: false,
executablePath: this.config.CHROME_PATH,
args: ["--lang=es-ES,es"],
});
this.page = await browser.newPage();
await this.page.setExtraHTTPHeaders({
"Accept-Language": "es",
});
await installMouseHelper(this.page);
await this.page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, "language", {
get: function () {
return "es-ES";
},
});
Object.defineProperty(navigator, "languages", {
get: function () {
return ["es-ES", "es"];
},
});
});
this.cursor = createCursor(this.page);
}
async findByText(selector: string, text: string[]) {
console.log(`Buscando elementos ${selector}`);
const elements = await this.page.$$(selector);
console.log(`Buscando elemento ${selector} con texto ${text}`);
const elementsWithText = await Promise.all(
elements.map(async (b) => ({
element: b,
text: await b.evaluate((e) => e.textContent),
}))
);
const element = elementsWithText.find((b) => text.includes(b.text));
if (!element) {
throw new Error(
`No se consiguio el elemento ${selector} con texto: ${text}`
);
}
return element;
}
async type(selector: string, text: string) {
const handle = await this.page.waitForSelector(selector);
if (null) {
throw new Error(`No se encontro el elemento: ${selector}`);
}
await handle!.scrollIntoView();
this.cursor.click(handle!);
await handle.focus();
await this.delay(2);
await handle!.type(text, { delay: 100 + Math.random() * 20 });
}
}
export default App;
export async function installMouseHelper(page: Page) {
await page.evaluateOnNewDocument(() => {
// Install mouse helper only for top-level frame.
if (window !== window.parent) return;
window.addEventListener(
"DOMContentLoaded",
() => {
const box = document.createElement("puppeteer-mouse-pointer");
const styleElement = document.createElement("style");
styleElement.innerHTML = `
puppeteer-mouse-pointer {
pointer-events: none;
position: absolute;
top: 0;
z-index: 10000;
left: 0;
width: 20px;
height: 20px;
background: rgba(0,0,0,.4);
border: 1px solid white;
border-radius: 10px;
margin: -10px 0 0 -10px;
padding: 0;
transition: background .2s, border-radius .2s, border-color .2s;
}
puppeteer-mouse-pointer.button-1 {
transition: none;
background: rgba(0,0,0,0.9);
}
puppeteer-mouse-pointer.button-2 {
transition: none;
border-color: rgba(0,0,255,0.9);
}
puppeteer-mouse-pointer.button-3 {
transition: none;
border-radius: 4px;
}
puppeteer-mouse-pointer.button-4 {
transition: none;
border-color: rgba(255,0,0,0.9);
}
puppeteer-mouse-pointer.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: number) {
for (let i = 0; i < 5; i++)
box.classList.toggle("button-" + i, Boolean(buttons & (1 << i)));
}
},
false
);
});
}