Spaces:
Runtime error
Runtime error
Update app.js
Browse files
app.js
CHANGED
|
@@ -137,25 +137,35 @@ function loadCookies(cookieFilePath) {
|
|
| 137 |
|
| 138 |
// CRITICAL: Set cookies BEFORE navigating
|
| 139 |
if (cookieData && cookieData.cookies) {
|
| 140 |
-
console.log('\n🍪
|
| 141 |
|
| 142 |
-
//
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
// Now set all cookies
|
|
|
|
|
|
|
| 151 |
for (const cookie of cookieData.cookies) {
|
| 152 |
try {
|
| 153 |
await page.setCookie(cookie);
|
|
|
|
| 154 |
} catch (error) {
|
| 155 |
-
|
| 156 |
}
|
| 157 |
}
|
| 158 |
-
console.log(
|
| 159 |
}
|
| 160 |
|
| 161 |
// Override WebDriver and other bot detection properties
|
|
@@ -203,62 +213,90 @@ function loadCookies(cookieFilePath) {
|
|
| 203 |
await randomWait(500, 1500);
|
| 204 |
|
| 205 |
// Navigate to the actual target URL
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
-
if (
|
| 215 |
-
|
| 216 |
-
console.log(
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
} else {
|
| 225 |
-
console.log('
|
| 226 |
}
|
| 227 |
|
| 228 |
// Set localStorage and sessionStorage if available
|
| 229 |
if (cookieData) {
|
| 230 |
if (cookieData.localStorage && Object.keys(cookieData.localStorage).length > 0) {
|
| 231 |
console.log('\n💾 Setting localStorage...');
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
| 238 |
}
|
| 239 |
-
}
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
| 242 |
}
|
| 243 |
|
| 244 |
if (cookieData.sessionStorage && Object.keys(cookieData.sessionStorage).length > 0) {
|
| 245 |
console.log('\n💾 Setting sessionStorage...');
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
| 252 |
}
|
| 253 |
-
}
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
| 256 |
}
|
| 257 |
|
| 258 |
// One more reload to ensure everything is applied
|
| 259 |
console.log('\n🔄 Final reload to apply all data...');
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
}
|
| 263 |
|
| 264 |
console.log('\n⏳ Waiting 3 seconds...');
|
|
@@ -276,9 +314,7 @@ function loadCookies(cookieFilePath) {
|
|
| 276 |
const buttonSelectors = [
|
| 277 |
'button[data-testid="signInButton"]',
|
| 278 |
'button#signInButton',
|
| 279 |
-
'button.pill.pill-text.pill-text--inherit.pill--medium.button'
|
| 280 |
-
'button:has-text("Login")',
|
| 281 |
-
'button:has-text("Sign In")'
|
| 282 |
];
|
| 283 |
|
| 284 |
let buttonFound = false;
|
|
@@ -361,16 +397,27 @@ function loadCookies(cookieFilePath) {
|
|
| 361 |
console.error('Stack trace:', error.stack);
|
| 362 |
|
| 363 |
try {
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
} catch (screenshotError) {
|
| 370 |
console.error('Could not take error screenshot:', screenshotError.message);
|
| 371 |
}
|
| 372 |
} finally {
|
| 373 |
-
|
| 374 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
}
|
| 376 |
})();
|
|
|
|
| 137 |
|
| 138 |
// CRITICAL: Set cookies BEFORE navigating
|
| 139 |
if (cookieData && cookieData.cookies) {
|
| 140 |
+
console.log('\n🍪 Pre-loading cookies...');
|
| 141 |
|
| 142 |
+
// Strategy 1: Navigate to base domain first with minimal load
|
| 143 |
+
try {
|
| 144 |
+
await page.goto('https://m.betking.com/', {
|
| 145 |
+
waitUntil: 'domcontentloaded',
|
| 146 |
+
timeout: 15000
|
| 147 |
+
});
|
| 148 |
+
console.log('✓ Base domain loaded');
|
| 149 |
+
} catch (baseError) {
|
| 150 |
+
console.log('⚠️ Base domain load issue, trying alternative...');
|
| 151 |
+
// Try setting domain manually
|
| 152 |
+
await page.evaluateOnNewDocument(() => {
|
| 153 |
+
document.domain = 'betking.com';
|
| 154 |
+
});
|
| 155 |
+
}
|
| 156 |
|
| 157 |
// Now set all cookies
|
| 158 |
+
console.log('Setting cookies...');
|
| 159 |
+
let cookiesSet = 0;
|
| 160 |
for (const cookie of cookieData.cookies) {
|
| 161 |
try {
|
| 162 |
await page.setCookie(cookie);
|
| 163 |
+
cookiesSet++;
|
| 164 |
} catch (error) {
|
| 165 |
+
// Silently skip problematic cookies
|
| 166 |
}
|
| 167 |
}
|
| 168 |
+
console.log(`✓ ${cookiesSet}/${cookieData.cookies.length} cookies set successfully`);
|
| 169 |
}
|
| 170 |
|
| 171 |
// Override WebDriver and other bot detection properties
|
|
|
|
| 213 |
await randomWait(500, 1500);
|
| 214 |
|
| 215 |
// Navigate to the actual target URL
|
| 216 |
+
let response;
|
| 217 |
+
try {
|
| 218 |
+
response = await page.goto('https://m.betking.com/en-ng?', {
|
| 219 |
+
waitUntil: ['domcontentloaded', 'networkidle2'],
|
| 220 |
+
timeout: 45000
|
| 221 |
+
});
|
| 222 |
+
} catch (error) {
|
| 223 |
+
console.log(`⚠️ Navigation warning: ${error.message}`);
|
| 224 |
+
console.log('Attempting to continue anyway...');
|
| 225 |
+
}
|
| 226 |
|
| 227 |
+
if (response) {
|
| 228 |
+
const statusCode = response.status();
|
| 229 |
+
console.log(`Response status: ${statusCode}`);
|
| 230 |
+
|
| 231 |
+
if (statusCode === 403) {
|
| 232 |
+
console.log('⚠️ Received 403 Forbidden error.');
|
| 233 |
+
console.log('Trying alternative approaches...');
|
| 234 |
+
|
| 235 |
+
// Wait and try reloading
|
| 236 |
+
await wait(2000);
|
| 237 |
+
|
| 238 |
+
try {
|
| 239 |
+
await page.reload({ waitUntil: ['domcontentloaded', 'networkidle2'], timeout: 30000 });
|
| 240 |
+
console.log('✓ Page reloaded with cookies');
|
| 241 |
+
} catch (reloadError) {
|
| 242 |
+
console.log('Reload had issues, but continuing...');
|
| 243 |
+
}
|
| 244 |
+
} else if (statusCode >= 400) {
|
| 245 |
+
console.log(`⚠️ Received error status: ${statusCode}`);
|
| 246 |
+
} else {
|
| 247 |
+
console.log('✓ Page loaded successfully');
|
| 248 |
+
}
|
| 249 |
} else {
|
| 250 |
+
console.log('⚠️ No response received, but page may have loaded');
|
| 251 |
}
|
| 252 |
|
| 253 |
// Set localStorage and sessionStorage if available
|
| 254 |
if (cookieData) {
|
| 255 |
if (cookieData.localStorage && Object.keys(cookieData.localStorage).length > 0) {
|
| 256 |
console.log('\n💾 Setting localStorage...');
|
| 257 |
+
try {
|
| 258 |
+
await page.evaluate((storage) => {
|
| 259 |
+
for (const [key, value] of Object.entries(storage)) {
|
| 260 |
+
try {
|
| 261 |
+
localStorage.setItem(key, value);
|
| 262 |
+
} catch (e) {
|
| 263 |
+
// Skip problematic items
|
| 264 |
+
}
|
| 265 |
}
|
| 266 |
+
}, cookieData.localStorage);
|
| 267 |
+
console.log('✓ localStorage set successfully');
|
| 268 |
+
} catch (error) {
|
| 269 |
+
console.log('⚠️ Could not set localStorage:', error.message);
|
| 270 |
+
}
|
| 271 |
}
|
| 272 |
|
| 273 |
if (cookieData.sessionStorage && Object.keys(cookieData.sessionStorage).length > 0) {
|
| 274 |
console.log('\n💾 Setting sessionStorage...');
|
| 275 |
+
try {
|
| 276 |
+
await page.evaluate((storage) => {
|
| 277 |
+
for (const [key, value] of Object.entries(storage)) {
|
| 278 |
+
try {
|
| 279 |
+
sessionStorage.setItem(key, value);
|
| 280 |
+
} catch (e) {
|
| 281 |
+
// Skip problematic items
|
| 282 |
+
}
|
| 283 |
}
|
| 284 |
+
}, cookieData.sessionStorage);
|
| 285 |
+
console.log('✓ sessionStorage set successfully');
|
| 286 |
+
} catch (error) {
|
| 287 |
+
console.log('⚠️ Could not set sessionStorage:', error.message);
|
| 288 |
+
}
|
| 289 |
}
|
| 290 |
|
| 291 |
// One more reload to ensure everything is applied
|
| 292 |
console.log('\n🔄 Final reload to apply all data...');
|
| 293 |
+
try {
|
| 294 |
+
await page.reload({ waitUntil: ['domcontentloaded'], timeout: 30000 });
|
| 295 |
+
console.log('✓ Page reloaded');
|
| 296 |
+
} catch (reloadError) {
|
| 297 |
+
console.log('⚠️ Reload warning:', reloadError.message);
|
| 298 |
+
console.log('Continuing anyway...');
|
| 299 |
+
}
|
| 300 |
}
|
| 301 |
|
| 302 |
console.log('\n⏳ Waiting 3 seconds...');
|
|
|
|
| 314 |
const buttonSelectors = [
|
| 315 |
'button[data-testid="signInButton"]',
|
| 316 |
'button#signInButton',
|
| 317 |
+
'button.pill.pill-text.pill-text--inherit.pill--medium.button'
|
|
|
|
|
|
|
| 318 |
];
|
| 319 |
|
| 320 |
let buttonFound = false;
|
|
|
|
| 397 |
console.error('Stack trace:', error.stack);
|
| 398 |
|
| 399 |
try {
|
| 400 |
+
// Check if page is still open before trying to screenshot
|
| 401 |
+
if (!page.isClosed()) {
|
| 402 |
+
await page.screenshot({
|
| 403 |
+
path: './betking-error.png',
|
| 404 |
+
fullPage: true
|
| 405 |
+
});
|
| 406 |
+
console.log('Error screenshot saved to betking-error.png');
|
| 407 |
+
} else {
|
| 408 |
+
console.log('Page was closed, cannot take error screenshot');
|
| 409 |
+
}
|
| 410 |
} catch (screenshotError) {
|
| 411 |
console.error('Could not take error screenshot:', screenshotError.message);
|
| 412 |
}
|
| 413 |
} finally {
|
| 414 |
+
try {
|
| 415 |
+
if (browser) {
|
| 416 |
+
await browser.close();
|
| 417 |
+
console.log('\n✅ Browser closed');
|
| 418 |
+
}
|
| 419 |
+
} catch (closeError) {
|
| 420 |
+
console.log('Browser already closed');
|
| 421 |
+
}
|
| 422 |
}
|
| 423 |
})();
|