This repository was archived by the owner on Apr 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbot.js
More file actions
261 lines (216 loc) · 8.34 KB
/
Copy pathbot.js
File metadata and controls
261 lines (216 loc) · 8.34 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
const { chromium } = require("playwright-extra");
const stealth = require("puppeteer-extra-plugin-stealth")();
chromium.use(stealth);
// --- Config ---
const TARGET_URL = "https://youtu.be/gNcXZZNoSto";
const ENGINES = [
{ name: "ecosia", url: "https://www.ecosia.org/search?q=" },
{ name: "yahoo", url: "https://search.yahoo.com/search?p=" },
{ name: "startpage",url: "https://www.startpage.com/sp/search?query=" },
{ name: "yandex", url: "https://yandex.com/search/?text=" },
{ name: "duckduckgo",url: "https://duckduckgo.com/?q=" },
{ name: "google", url: "https://www.google.com/search?q=" },
{ name: "swisscows",url: "https://swisscows.com/en/web?query=" }
];
const SEARCH_QUERIES = [
"how+to+get+all+benefits",
"benefits+of+online+services",
"youtube+video+gNcXZZNoSto",
"all+benefits+guide"
];
const USER_AGENTS = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
];
const VIEWPORTS = [
{ width: 1920, height: 1080 },
{ width: 1366, height: 768 }
];
// --- Global State ---
let isRunning = false;
let stopRequested = false;
let clients = [];
let targetUrl = "https://youtu.be/gNcXZZNoSto";
// --- Helpers ---
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const rand = (min, max) => Math.random() * (max - min) + min;
const randInt = (min, max) => Math.floor(rand(min, max + 1));
const pick = (arr) => arr[randInt(0, arr.length - 1)];
function log(msg, engineName = "", isError = false) {
const ts = new Date().toLocaleTimeString();
const prefix = engineName ? `[${engineName.toUpperCase()}] ` : "";
const fullMsg = `${prefix}${msg}`;
console.log(`[${ts}] ${fullMsg}`);
// Broadcast to SSE clients
const data = JSON.stringify({ type: 'log', ts, engine: engineName, msg, isError });
clients.forEach(client => client.res.write(`data: ${data}\n\n`));
}
function broadcastStatus() {
const data = JSON.stringify({ type: 'status', running: isRunning });
clients.forEach(client => client.res.write(`data: ${data}\n\n`));
}
// ... (Rest of the bot logic: bezierPoint, humanMouseMove, randomPause, skipAds remain the same)
// I will include them below but they are unchanged from the previous version except for using the new log()
function bezierPoint(t, p0, p1, p2, p3) {
const u = 1 - t;
return u * u * u * p0 + 3 * u * u * t * p1 + 3 * u * t * t * p2 + t * t * t * p3;
}
async function humanMouseMove(page, x0, y0, x1, y1) {
const steps = randInt(30, 60);
const cx1 = x0 + (x1 - x0) * rand(0.2, 0.4) + rand(-50, 50);
const cy1 = y0 + (y1 - y0) * rand(0.1, 0.3) + rand(-40, 40);
const cx2 = x0 + (x1 - x0) * rand(0.6, 0.8) + rand(-50, 50);
const cy2 = y0 + (y1 - y0) * rand(0.7, 0.9) + rand(-40, 40);
for (let i = 0; i <= steps; i++) {
const t = i / steps;
const mx = bezierPoint(t, x0, cx1, cx2, x1);
const my = bezierPoint(t, y0, cy1, cy2, y1);
await page.mouse.move(mx, my);
await sleep(rand(5, 15));
}
}
async function randomPause(page) {
if (Math.random() < 0.3) {
const pauseTime = rand(5000, 15000);
log(`⏸ Random pause: ${Math.round(pauseTime/1000)}s`);
await sleep(pauseTime);
}
}
async function skipAds(page) {
try {
const skipBtn = await page.$(".ytp-skip-ad-button, .ytp-ad-skip-button-hover");
if (skipBtn) {
log("⏭ Ad skip button found!");
await sleep(rand(2000, 4000));
const box = await skipBtn.boundingBox();
if (box) {
const cur = await page.evaluate(() => ({ x: window.innerWidth/2, y: window.innerHeight/2 }));
await humanMouseMove(page, cur.x, cur.y, box.x + box.width/2, box.y + box.height/2);
await page.mouse.click(box.x + box.width/2, box.y + box.height/2);
log("✅ Ad skipped");
}
}
} catch (e) {}
}
async function runFlow(engine) {
if (stopRequested) return;
const ua = pick(USER_AGENTS);
const viewport = pick(VIEWPORTS);
const query = pick(SEARCH_QUERIES);
const referrer = engine.url + query;
log(`Starting flow`, engine.name);
const isHeadless = process.env.HEADLESS !== "false"; // Default to true if not "false"
log(`Headless mode: ${isHeadless}`, engine.name);
const browser = await chromium.launch({
headless: isHeadless,
args: [
"--disable-blink-features=AutomationControlled",
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--disable-accelerated-2d-canvas",
"--disable-gpu"
]
});
const context = await browser.newContext({
userAgent: ua,
viewport,
extraHTTPHeaders: { 'Referer': referrer }
});
const page = await context.newPage();
try {
log(`Navigating to target video`, engine.name);
await page.goto(targetUrl, { waitUntil: "domcontentloaded", timeout: 60000 });
await page.waitForSelector(".html5-video-player");
const playBtn = await page.$(".ytp-large-play-button");
if (playBtn) {
await sleep(rand(2000, 4000));
await playBtn.click();
log("Clicking play button", engine.name);
}
let duration = await page.evaluate(() => {
const video = document.querySelector("video");
return video ? video.duration : 0;
});
if (duration === 0) {
log("Could not detect duration, using 5m default", engine.name);
duration = 300;
} else {
log(`Video duration: ${Math.round(duration)}s`, engine.name);
}
let elapsed = 0;
while (elapsed < duration && !stopRequested) {
await skipAds(page);
await randomPause(page);
if (Math.random() < 0.2) {
const rx = rand(100, viewport.width - 100);
const ry = rand(100, viewport.height - 100);
const cur = await page.evaluate(() => ({ x: window.innerWidth/2, y: window.innerHeight/2 }));
await humanMouseMove(page, cur.x, cur.y, rx, ry);
}
await sleep(10000);
elapsed += 10;
}
log(`Finished watching`, engine.name);
} catch (err) {
log(`Error: ${err.message}`, engine.name, true);
} finally {
await browser.close();
}
}
async function botMain() {
isRunning = true;
stopRequested = false;
broadcastStatus();
log("🚀 Bot execution started...");
while (!stopRequested) {
const tasks = ENGINES.map(engine => runFlow(engine));
await Promise.all(tasks);
if (stopRequested) break;
log("🔄 All parallel flows completed. Waiting 30s...");
await sleep(30000);
}
isRunning = false;
stopRequested = false;
broadcastStatus();
log("🛑 Bot execution stopped.");
}
// --- Web Server ---
const express = require("express");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 7860;
app.use(express.json());
app.use(express.static(path.join(__dirname)));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
app.get("/api/logs", (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.flushHeaders();
const clientId = Date.now();
const newClient = { id: clientId, res };
clients.push(newClient);
// Send initial status
const data = JSON.stringify({ type: 'status', running: isRunning });
res.write(`data: ${data}\n\n`);
req.on('close', () => {
clients = clients.filter(c => c.id !== clientId);
});
});
app.post("/api/start", (req, res) => {
if (isRunning) return res.status(400).json({ error: "Already running" });
targetUrl = req.body.url;
botMain(); // Run in background
res.json({ success: true });
});
app.post("/api/stop", (req, res) => {
if (!isRunning) return res.status(400).json({ error: "Not running" });
stopRequested = true;
res.json({ success: true });
});
app.listen(PORT, () => {
console.log(`🌍 Dashboard available at http://localhost:${PORT}`);
});