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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const puppeteer = require('puppeteer-extra');
|
| 2 |
+
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
|
| 3 |
+
const FormData = require('form-data');
|
| 4 |
+
const fs = require('fs');
|
| 5 |
+
const https = require('https');
|
| 6 |
+
|
| 7 |
+
// Use stealth plugin to avoid detection
|
| 8 |
+
puppeteer.use(StealthPlugin());
|
| 9 |
+
|
| 10 |
+
// Helper function to wait (replaces deprecated waitForTimeout)
|
| 11 |
+
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
| 12 |
+
|
| 13 |
+
// Random delay to appear more human-like
|
| 14 |
+
const randomWait = (min, max) => wait(Math.floor(Math.random() * (max - min + 1)) + min);
|
| 15 |
+
|
| 16 |
+
// Function to upload image to catbox.moe
|
| 17 |
+
async function uploadToCatbox(filePath) {
|
| 18 |
+
return new Promise((resolve, reject) => {
|
| 19 |
+
const form = new FormData();
|
| 20 |
+
form.append('reqtype', 'fileupload');
|
| 21 |
+
form.append('fileToUpload', fs.createReadStream(filePath));
|
| 22 |
+
|
| 23 |
+
form.submit('https://catbox.moe/user/api.php', (err, res) => {
|
| 24 |
+
if (err) {
|
| 25 |
+
reject(err);
|
| 26 |
+
return;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
let data = '';
|
| 30 |
+
res.on('data', chunk => {
|
| 31 |
+
data += chunk;
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
res.on('end', () => {
|
| 35 |
+
if (res.statusCode === 200) {
|
| 36 |
+
resolve(data.trim());
|
| 37 |
+
} else {
|
| 38 |
+
reject(new Error(`Upload failed with status code: ${res.statusCode}`));
|
| 39 |
+
}
|
| 40 |
+
});
|
| 41 |
+
|
| 42 |
+
res.on('error', reject);
|
| 43 |
+
});
|
| 44 |
+
});
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
(async () => {
|
| 48 |
+
const browser = await puppeteer.launch({
|
| 49 |
+
headless: false, // Set to true if you want headless mode
|
| 50 |
+
args: [
|
| 51 |
+
'--no-sandbox',
|
| 52 |
+
'--disable-setuid-sandbox',
|
| 53 |
+
'--disable-blink-features=AutomationControlled',
|
| 54 |
+
'--disable-web-security',
|
| 55 |
+
'--disable-features=IsolateOrigins,site-per-process',
|
| 56 |
+
'--disable-dev-shm-usage',
|
| 57 |
+
'--disable-accelerated-2d-canvas',
|
| 58 |
+
'--no-first-run',
|
| 59 |
+
'--no-zygote',
|
| 60 |
+
'--disable-gpu'
|
| 61 |
+
],
|
| 62 |
+
ignoreHTTPSErrors: true
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
const page = await browser.newPage();
|
| 66 |
+
|
| 67 |
+
try {
|
| 68 |
+
// Enable JavaScript
|
| 69 |
+
await page.setJavaScriptEnabled(true);
|
| 70 |
+
|
| 71 |
+
// Set realistic mobile viewport (iPhone 12 Pro)
|
| 72 |
+
await page.setViewport({
|
| 73 |
+
width: 390,
|
| 74 |
+
height: 844,
|
| 75 |
+
isMobile: true,
|
| 76 |
+
hasTouch: true,
|
| 77 |
+
deviceScaleFactor: 3
|
| 78 |
+
});
|
| 79 |
+
|
| 80 |
+
// Set realistic mobile user agent (latest iOS)
|
| 81 |
+
await page.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 17_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Mobile/15E148 Safari/604.1');
|
| 82 |
+
|
| 83 |
+
// Set additional headers to appear more legitimate
|
| 84 |
+
await page.setExtraHTTPHeaders({
|
| 85 |
+
'Accept-Language': 'en-US,en;q=0.9,en-NG;q=0.8',
|
| 86 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
|
| 87 |
+
'Accept-Encoding': 'gzip, deflate, br',
|
| 88 |
+
'Connection': 'keep-alive',
|
| 89 |
+
'Upgrade-Insecure-Requests': '1',
|
| 90 |
+
'Sec-Fetch-Dest': 'document',
|
| 91 |
+
'Sec-Fetch-Mode': 'navigate',
|
| 92 |
+
'Sec-Fetch-Site': 'none',
|
| 93 |
+
'Sec-Fetch-User': '?1'
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
// Override WebDriver and other bot detection properties
|
| 97 |
+
await page.evaluateOnNewDocument(() => {
|
| 98 |
+
// Override the navigator.webdriver property
|
| 99 |
+
Object.defineProperty(navigator, 'webdriver', {
|
| 100 |
+
get: () => undefined
|
| 101 |
+
});
|
| 102 |
+
|
| 103 |
+
// Override permissions
|
| 104 |
+
const originalQuery = window.navigator.permissions.query;
|
| 105 |
+
window.navigator.permissions.query = (parameters) => (
|
| 106 |
+
parameters.name === 'notifications' ?
|
| 107 |
+
Promise.resolve({ state: Notification.permission }) :
|
| 108 |
+
originalQuery(parameters)
|
| 109 |
+
);
|
| 110 |
+
|
| 111 |
+
// Add Chrome object
|
| 112 |
+
window.chrome = {
|
| 113 |
+
runtime: {}
|
| 114 |
+
};
|
| 115 |
+
|
| 116 |
+
// Override plugins
|
| 117 |
+
Object.defineProperty(navigator, 'plugins', {
|
| 118 |
+
get: () => [1, 2, 3, 4, 5]
|
| 119 |
+
});
|
| 120 |
+
|
| 121 |
+
// Override languages
|
| 122 |
+
Object.defineProperty(navigator, 'languages', {
|
| 123 |
+
get: () => ['en-US', 'en']
|
| 124 |
+
});
|
| 125 |
+
});
|
| 126 |
+
|
| 127 |
+
console.log('Navigating to the page...');
|
| 128 |
+
|
| 129 |
+
// Add a small random delay before navigation to appear more human
|
| 130 |
+
await randomWait(500, 1500);
|
| 131 |
+
|
| 132 |
+
const response = await page.goto('https://m.betking.com/en-ng?', {
|
| 133 |
+
waitUntil: ['networkidle0', 'domcontentloaded'],
|
| 134 |
+
timeout: 45000
|
| 135 |
+
});
|
| 136 |
+
|
| 137 |
+
const statusCode = response.status();
|
| 138 |
+
console.log(`Response status: ${statusCode}`);
|
| 139 |
+
|
| 140 |
+
if (statusCode === 403) {
|
| 141 |
+
console.log('β οΈ Received 403 Forbidden error.');
|
| 142 |
+
console.log('The site is blocking automated access. Possible solutions:');
|
| 143 |
+
console.log('1. Try running from a different IP/location');
|
| 144 |
+
console.log('2. Use a residential proxy');
|
| 145 |
+
console.log('3. The site may require CAPTCHA solving');
|
| 146 |
+
console.log('4. Try accessing from a real mobile device first to establish cookies');
|
| 147 |
+
} else if (statusCode >= 400) {
|
| 148 |
+
console.log(`β οΈ Received error status: ${statusCode}`);
|
| 149 |
+
} else {
|
| 150 |
+
console.log('β Page loaded successfully');
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
console.log('Waiting 3 seconds...');
|
| 154 |
+
await wait(3000);
|
| 155 |
+
|
| 156 |
+
// Take initial screenshot
|
| 157 |
+
console.log('Taking initial screenshot...');
|
| 158 |
+
await page.screenshot({
|
| 159 |
+
path: '/mnt/user-data/outputs/betking-initial.png',
|
| 160 |
+
fullPage: true
|
| 161 |
+
});
|
| 162 |
+
|
| 163 |
+
console.log('Looking for the Sign In button...');
|
| 164 |
+
|
| 165 |
+
// Try multiple selectors to find the button
|
| 166 |
+
const buttonSelectors = [
|
| 167 |
+
'button[data-testid="signInButton"]',
|
| 168 |
+
'button#signInButton',
|
| 169 |
+
'button.pill.pill-text.pill-text--inherit.pill--medium.button',
|
| 170 |
+
'button:has-text("Login")',
|
| 171 |
+
'button:has-text("Sign In")'
|
| 172 |
+
];
|
| 173 |
+
|
| 174 |
+
let buttonFound = false;
|
| 175 |
+
let buttonSelector = null;
|
| 176 |
+
|
| 177 |
+
for (const selector of buttonSelectors) {
|
| 178 |
+
try {
|
| 179 |
+
const button = await page.$(selector);
|
| 180 |
+
if (button) {
|
| 181 |
+
buttonFound = true;
|
| 182 |
+
buttonSelector = selector;
|
| 183 |
+
console.log(`β Button found using selector: ${selector}`);
|
| 184 |
+
|
| 185 |
+
// Check if button is visible
|
| 186 |
+
const isVisible = await button.isIntersectingViewport();
|
| 187 |
+
console.log(`Button is visible: ${isVisible}`);
|
| 188 |
+
|
| 189 |
+
if (isVisible) {
|
| 190 |
+
console.log('Clicking the button...');
|
| 191 |
+
// Add small random delay to appear more human
|
| 192 |
+
await randomWait(200, 800);
|
| 193 |
+
await button.click();
|
| 194 |
+
console.log('β Button clicked successfully!');
|
| 195 |
+
} else {
|
| 196 |
+
console.log('Button exists but is not visible, scrolling into view...');
|
| 197 |
+
await page.evaluate((sel) => {
|
| 198 |
+
document.querySelector(sel).scrollIntoView({ behavior: 'smooth', block: 'center' });
|
| 199 |
+
}, selector);
|
| 200 |
+
await wait(500);
|
| 201 |
+
await randomWait(200, 800);
|
| 202 |
+
await button.click();
|
| 203 |
+
console.log('β Button clicked after scrolling!');
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
// Wait a moment for any transitions/animations after click
|
| 207 |
+
await wait(1500);
|
| 208 |
+
break;
|
| 209 |
+
}
|
| 210 |
+
} catch (error) {
|
| 211 |
+
console.log(`Selector ${selector} failed: ${error.message}`);
|
| 212 |
+
}
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
if (!buttonFound) {
|
| 216 |
+
console.log('β οΈ Button not found with any selector.');
|
| 217 |
+
console.log('Taking screenshot of current page state...');
|
| 218 |
+
|
| 219 |
+
// Log available buttons for debugging
|
| 220 |
+
const allButtons = await page.evaluate(() => {
|
| 221 |
+
return Array.from(document.querySelectorAll('button')).map(btn => ({
|
| 222 |
+
id: btn.id,
|
| 223 |
+
class: btn.className,
|
| 224 |
+
text: btn.innerText,
|
| 225 |
+
testId: btn.getAttribute('data-testid')
|
| 226 |
+
}));
|
| 227 |
+
});
|
| 228 |
+
console.log('Available buttons on page:', JSON.stringify(allButtons, null, 2));
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
console.log('Taking final screenshot...');
|
| 232 |
+
await page.screenshot({
|
| 233 |
+
path: '/mnt/user-data/outputs/betking-final.png',
|
| 234 |
+
fullPage: true
|
| 235 |
+
});
|
| 236 |
+
console.log('β Screenshot saved to betking-final.png');
|
| 237 |
+
|
| 238 |
+
// Upload screenshot to catbox
|
| 239 |
+
console.log('\nπ€ Uploading screenshot to Catbox...');
|
| 240 |
+
try {
|
| 241 |
+
const catboxUrl = await uploadToCatbox('/mnt/user-data/outputs/betking-final.png');
|
| 242 |
+
console.log('β Upload successful!');
|
| 243 |
+
console.log('\nπ Download link:');
|
| 244 |
+
console.log(catboxUrl);
|
| 245 |
+
console.log('\nπ Direct link (copy this):');
|
| 246 |
+
console.log(catboxUrl);
|
| 247 |
+
} catch (uploadError) {
|
| 248 |
+
console.error('β Failed to upload to Catbox:', uploadError.message);
|
| 249 |
+
console.log('Screenshot is still saved locally at: /mnt/user-data/outputs/betking-final.png');
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
} catch (error) {
|
| 253 |
+
console.error('β Error occurred:', error.message);
|
| 254 |
+
console.error('Stack trace:', error.stack);
|
| 255 |
+
|
| 256 |
+
// Take error screenshot
|
| 257 |
+
try {
|
| 258 |
+
await page.screenshot({
|
| 259 |
+
path: '/mnt/user-data/outputs/betking-error.png',
|
| 260 |
+
fullPage: true
|
| 261 |
+
});
|
| 262 |
+
console.log('Error screenshot saved to betking-error.png');
|
| 263 |
+
} catch (screenshotError) {
|
| 264 |
+
console.error('Could not take error screenshot:', screenshotError.message);
|
| 265 |
+
}
|
| 266 |
+
} finally {
|
| 267 |
+
await browser.close();
|
| 268 |
+
console.log('Browser closed');
|
| 269 |
+
}
|
| 270 |
+
})();
|