-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotpixel.user.js
More file actions
388 lines (327 loc) · 12.1 KB
/
notpixel.user.js
File metadata and controls
388 lines (327 loc) · 12.1 KB
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
// ==UserScript==
// @name Notpixel
// @namespace Violentmonkey Scripts
// @grant none
// @version 1.0
// @author -
// @description 9/1/2024, 7:13:21 PM
// @match *://*notpx.app/*
// @downloadURL https://github.com/vsotreshko/bug-free-happiness/raw/main/notpixel.user.js
// @updateURL https://github.com/vsotreshko/bug-free-happiness/raw/main/notpixel.user.js
// ==/UserScript==
/** Custom functions -------------------------------------------------------------- */
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
const getRandomInt = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};
const simulateClickIfExist = async (name, selector) => {
const claimButton = await waitForElement(document, selector);
if (claimButton) {
simulateClick(claimButton);
} else {
console.error(`${name} wasn't found`);
}
};
const simulateClick = (button) => {
const events = [
new PointerEvent("pointerdown", {
bubbles: true,
cancelable: true,
isTrusted: true,
pointerId: 1,
width: 1,
height: 1,
pressure: 0.5,
pointerType: "touch",
}),
new MouseEvent("mousedown", { bubbles: true, cancelable: true, isTrusted: true, screenX: 182, screenY: 877 }),
new PointerEvent("pointerup", {
bubbles: true,
cancelable: true,
isTrusted: true,
pointerId: 1,
width: 1,
height: 1,
pressure: 0,
pointerType: "touch",
}),
new MouseEvent("mouseup", { bubbles: true, cancelable: true, isTrusted: true, screenX: 182, screenY: 877 }),
new MouseEvent("clic", { bubbles: true, cancelable: true, isTrusted: true, screenX: 182, screenY: 877 }),
new PointerEvent("click", {
bubbles: true,
cancelable: true,
isTrusted: true,
pointerId: 1,
width: 1,
height: 1,
pressure: 0,
pointerType: "touch",
}),
new PointerEvent("pointerout", {
bubbles: true,
cancelable: true,
isTrusted: true,
pointerId: 1,
width: 1,
height: 1,
pressure: 0,
pointerType: "touch",
}),
new PointerEvent("pointerleave", {
bubbles: true,
cancelable: true,
isTrusted: true,
pointerId: 1,
width: 1,
height: 1,
pressure: 0,
pointerType: "touch",
}),
new MouseEvent("mouseout", { bubbles: true, cancelable: true, isTrusted: true, screenX: 182, screenY: 877 }),
new MouseEvent("mouseleave", { bubbles: true, cancelable: true, isTrusted: true, screenX: 182, screenY: 877 }),
];
events.forEach((event, index) => {
setTimeout(() => button.dispatchEvent(event), index * 100);
});
};
const simulateClickX = (button) => {
const events = [
new PointerEvent("pointerdown", {
bubbles: true,
cancelable: true,
isTrusted: true,
pointerId: 1,
width: 1,
height: 1,
pressure: 0.5,
pointerType: "touch",
}),
new MouseEvent("mousedown", { bubbles: true, cancelable: true, isTrusted: true, offsetX: 182, offsetY: 877 }),
new PointerEvent("pointerup", {
bubbles: true,
cancelable: true,
isTrusted: true,
pointerId: 1,
width: 1,
height: 1,
pressure: 0,
pointerType: "touch",
}),
new MouseEvent("mouseup", { bubbles: true, cancelable: true, isTrusted: true, offsetX: 315, offsetY: 483 }),
new PointerEvent("click", { bubbles: true, cancelable: true, isTrusted: true, offsetX: 315, offsetY: 483 }),
];
events.forEach((event, index) => {
setTimeout(() => button.dispatchEvent(event), index * 100);
});
};
const simulateClickHaloween = (button) => {
const events = [
new PointerEvent("pointerdown", {
bubbles: true,
cancelable: true,
isTrusted: true,
pointerId: 1,
width: 1,
height: 1,
pressure: 0.5,
pointerType: "touch",
}),
new MouseEvent("mousedown", { bubbles: true, cancelable: true, isTrusted: true, offsetX: 137, offsetY: 91 }),
new PointerEvent("pointerup", {
bubbles: true,
cancelable: true,
isTrusted: true,
pointerId: 1,
width: 1,
height: 1,
pressure: 0,
pointerType: "touch",
}),
new MouseEvent("mouseup", { bubbles: true, cancelable: true, isTrusted: true, offsetX: 137, offsetY: 91 }),
new PointerEvent("click", { bubbles: true, cancelable: true, isTrusted: true, offsetX: 137, offsetY: 91 }),
];
events.forEach((event, index) => {
setTimeout(() => button.dispatchEvent(event), index * 100);
});
};
const waitForElement = async (document, selector, timeout = 10000) => {
console.warn(`Waiting for: ${selector}`);
return new Promise((resolve, reject) => {
if (document.querySelector(selector)) {
console.warn(`waitForElement: found ${selector} immediately`);
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver((mutations) => {
if (document.querySelector(selector)) {
console.warn(`waitForElement: found ${selector} after DOM changed`);
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
// Resolve promise after 10 seconds if element is not found
setTimeout(() => {
console.warn(`waitForElement: ${selector} not found after ${timeout} ms`);
observer.disconnect();
resolve(null);
}, timeout);
});
};
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
/** ------------------------------------------------------------------------------- */
// Симуляция событий указателя
function simulatePointerEvents(element, startX, startY, endX, endY) {
const events = [
new PointerEvent("pointerdown", { clientX: startX, clientY: startY, bubbles: true }),
new PointerEvent("pointermove", { clientX: startX, clientY: startY, bubbles: true }),
new PointerEvent("pointermove", { clientX: endX, clientY: endY, bubbles: true }),
new PointerEvent("pointerup", { clientX: endX, clientY: endY, bubbles: true }),
];
events.forEach((event) => element.dispatchEvent(event));
}
const clickPaintButton = async (document) => {
const paintButton = await waitForElement(document, "#root > div > div._order_panel_lwgvy_1 > div > button > span");
simulateClickX(paintButton);
};
const findColors = async (document) => {
// Click on the active color
const activeColorSelector =
"#root > div > div._order_panel_lwgvy_1 > div > div._info_lwgvy_42 > div._active_color_lwgvy_51";
const activeColor = await waitForElement(document, activeColorSelector);
simulateClickX(activeColor);
// Await palette to appear
await delay(1000);
// Get palette colors
const paletteSelector =
"#root > div > div._order_panel_lwgvy_1 > div > div._expandable_panel_layout_1v9vd_1 > div > div._color_line_epppt_15";
const palette = await waitForElement(document, paletteSelector);
const colors = [];
for (const child of palette.children) {
if (child.style.backgroundColor === "rgb(109, 72, 47)") {
colors.push(child);
}
if (child.style.backgroundColor === "rgb(0, 0, 0)") {
colors.push(child);
}
}
return colors;
};
const clickByCoof = async (canvas, x, y) => {
simulatePointerEvents(canvas, canvas.width * x, canvas.height * y, canvas.width * x, canvas.height * y);
simulateClickX(canvas);
};
const changeCursorPositionOnCanvas = async (canvas) => {
const quarter = getRandomInt(20, 30) / 100; // (20 - 30)
await clickByCoof(canvas, quarter, quarter);
};
const selectBlumTemplate = async (document) => {
const blumTemplateSelector = "#root > div > div:nth-child(3) > div > div > button:nth-of-type(2)";
const blumTemplate = await waitForElement(document, blumTemplateSelector);
if (blumTemplate) {
console.log("Found blum template");
simulateClick(blumTemplate);
return true;
}
return false;
};
const getCanPaintCount = async (document) => {
const canPaintCountSelector =
"#root > div > div:nth-child(8) > div > button > div:nth-child(1) > div > div:nth-child(2) > span:nth-child(2)";
const canPaintCountElement = await waitForElement(document, canPaintCountSelector);
return parseInt(canPaintCountElement.textContent);
};
const autoClaimReward = async (document, toClose = true) => {
await simulateClickIfExist(
"claimMenuButton",
"#root > div > div:nth-child(1) > div > div:nth-child(2) > div:nth-child(2) > button"
);
await delay(1000);
await simulateClickIfExist(
"claimButton",
"#root > div > div._layout_q8u4d_1 > div._content_q8u4d_22 > div._container_13oyr_1 > button"
);
await delay(1000);
if (toClose) {
await simulateClickIfExist("backButton", "#root > div > div:nth-child(2) > button");
}
};
const resolveTasks = async (document) => {
// TODO: NOT WORKING
const taskRowSelector =
"#root > div > div._layout_4nkxd_1 > div._content_4nkxd_22 > div._info_layout_bt2qf_1 > div > div:nth-child(1) > div";
const taskButton = await waitForElement(document, taskRowSelector);
simulateClick(taskButton);
await delay(1000);
const symbolTaskSelector =
"#root > div > div._layout_4nkxd_1 > div._content_4nkxd_22 > div._info_layout_bt2qf_1 > div > div:nth-child(9) > div";
const symbolTask = await waitForElement(document, symbolTaskSelector);
simulateClick(symbolTask);
await delay(1000);
const checkButtonSelector = "body > div._popup_1ub07_11 > div > div:nth-child(4) > button";
const checkButton = await waitForElement(document, checkButtonSelector);
simulateClick(checkButton);
console.log("Task resolved");
};
const resolveBoosts = async (document) => {
await simulateClickIfExist(
"boostsTab",
"#root > div > div._layout_q8u4d_1 > div._content_q8u4d_22 > div._panel_1mia4_1 > div:nth-child(2)"
);
const paintRewardLevelSelector =
"#root > div > div._layout_q8u4d_1 > div._content_q8u4d_22 > div._info_layout_bt2qf_1 > div > div._group_v8prs_7 > div:nth-child(1) > div._content_container_8sbvi_21 > div > div._item_reward_container_8sbvi_40 > span._level_text_8sbvi_53";
const paintRewardLevel = await waitForElement(document, paintRewardLevelSelector);
// console.log("paintRewardLevel", paintRewardLevel.textContent.trim().replace("lvl").toLowerCase());
};
const init = async () => {
console.log("NotPixel Running...");
// Wait for the page to load
await delay(3000);
let res = await selectBlumTemplate(document);
if (!res) {
console.log("!!! !!! Blum template not found !!! !!!");
return;
}
await delay(2000);
const templateCatalogTitleSelector = "#root > div > div:nth-child(2) > div:nth-child(2) > div:nth-child(1) > h1";
const templateCatalogTitle = await waitForElement(document, templateCatalogTitleSelector, 2000);
if (templateCatalogTitle && templateCatalogTitle.textContent.trim().toLowerCase().includes("templates")) {
await delay(2000);
const blumTemplateSelector =
"#root > div > div:nth-child(2) > div:nth-child(2) > div:nth-child(3) > div > div:nth-child(1) > div:nth-child(2) > div > img";
const blumTemplate = await waitForElement(document, blumTemplateSelector);
simulateClick(blumTemplate);
await delay(1000);
const buttonSelector = "body > div:nth-child(8) > div > div > div > div:nth-child(4) > button";
const button = await waitForElement(document, buttonSelector);
simulateClick(button);
await delay(2000);
window.location.reload();
}
const canvas = await waitForElement(document, "#canvasHolder");
await changeCursorPositionOnCanvas(canvas);
await delay(1000);
const canPaintCount = await getCanPaintCount(document);
if (canPaintCount > 1) {
for (let i = 0; i < Math.floor(canPaintCount / 2); i++) {
await delay(1000);
await changeCursorPositionOnCanvas(canvas);
const colors = await findColors(document);
for (const color of colors) {
simulateClickX(color);
await delay(500);
await clickPaintButton(document);
await delay(500);
}
}
}
await autoClaimReward(document, false);
await resolveBoosts(document);
};
init();