diff --git a/debug_layout_menu_Diy_module.png b/debug_layout_menu_Diy_module.png deleted file mode 100644 index 5283f26..0000000 Binary files a/debug_layout_menu_Diy_module.png and /dev/null differ diff --git a/debug_layout_menu_ai.png b/debug_layout_menu_ai.png deleted file mode 100644 index 1a0f3c3..0000000 Binary files a/debug_layout_menu_ai.png and /dev/null differ diff --git a/lib/tradingview.ts b/lib/tradingview.ts index e3efbf8..fe6624f 100644 --- a/lib/tradingview.ts +++ b/lib/tradingview.ts @@ -45,7 +45,6 @@ export class TradingViewCapture { if (!this.loggedIn) { console.log('Logging in to TradingView...') await this.login() - this.loggedIn = true console.log('Logged in to TradingView') } return this.page @@ -64,12 +63,17 @@ export class TradingViewCapture { const loggedInIndicator = await page.waitForSelector('.tv-header__user-menu-button, [data-name="header-user-menu"]', { timeout: 3000 }) if (loggedInIndicator) { console.log('Already logged in to TradingView') + // Reset the loggedIn flag to true to ensure we don't re-login unnecessarily + this.loggedIn = true return } } catch (e) { console.log('Not logged in yet, proceeding with login...') } + // Reset login flag since we need to login + this.loggedIn = false + try { // Wait for the login modal to appear and look for email input directly console.log('Looking for email input field...') @@ -187,12 +191,13 @@ export class TradingViewCapture { try { console.log('Waiting for login to complete...') await page.waitForSelector('.tv-header__user-menu-button, .chart-container, [data-name="header-user-menu"]', { timeout: 30000 }) + this.loggedIn = true + console.log('TradingView login complete') } catch (e) { console.error('Login navigation did not complete.') + this.loggedIn = false throw new Error('Login navigation did not complete.') } - - console.log('TradingView login complete') } async capture(symbol: string, filename: string, layouts?: string[], timeframe?: string) { @@ -238,6 +243,49 @@ export class TradingViewCapture { try { console.log('Navigating to layout URL:', url) await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 }) + + // Check if we landed on the login restriction page + const restrictionCheck = await page.evaluate(() => { + const text = document.body.textContent || '' + return text.includes("We can't open this chart layout for you") || + text.includes("log in to see it") || + text.includes("chart layout sharing") + }) + + if (restrictionCheck) { + console.log(`Layout "${layout}" requires login verification, checking login status...`) + + // Verify we're actually logged in by checking for user menu + const loggedInCheck = await page.waitForSelector('.tv-header__user-menu-button, [data-name="header-user-menu"]', { timeout: 5000 }).catch(() => null) + + if (!loggedInCheck) { + console.log('Not properly logged in, re-authenticating...') + await this.login() + + // Try navigating to the layout URL again + console.log('Retrying navigation to layout URL after login:', url) + await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 }) + + // Check again if we still get the restriction + const secondCheck = await page.evaluate(() => { + const text = document.body.textContent || '' + return text.includes("We can't open this chart layout for you") || + text.includes("log in to see it") || + text.includes("chart layout sharing") + }) + + if (secondCheck) { + console.log(`Layout "${layout}" is private or not accessible, falling back to base chart`) + // Navigate to base chart instead + let baseUrl = `https://www.tradingview.com/chart/?symbol=${finalSymbol}` + if (finalTimeframe) { + baseUrl += `&interval=${encodeURIComponent(finalTimeframe)}` + } + await page.goto(baseUrl, { waitUntil: 'networkidle2', timeout: 60000 }) + } + } + } + console.log('Successfully navigated to layout:', layout) } catch (e: any) { console.error(`Failed to load layout "${layout}":`, e) diff --git a/screenshots/SOLUSD_5_1752065763122_Diy module.png b/screenshots/SOLUSD_5_1752065763122_Diy module.png new file mode 100644 index 0000000..fdfdf31 Binary files /dev/null and b/screenshots/SOLUSD_5_1752065763122_Diy module.png differ diff --git a/screenshots/SOLUSD_5_1752065763122_ai.png b/screenshots/SOLUSD_5_1752065763122_ai.png new file mode 100644 index 0000000..fdfdf31 Binary files /dev/null and b/screenshots/SOLUSD_5_1752065763122_ai.png differ diff --git a/screenshots/SOLUSD_5_1752066176889_Diy module.png b/screenshots/SOLUSD_5_1752066176889_Diy module.png new file mode 100644 index 0000000..fdfdf31 Binary files /dev/null and b/screenshots/SOLUSD_5_1752066176889_Diy module.png differ diff --git a/screenshots/SOLUSD_5_1752066176889_ai.png b/screenshots/SOLUSD_5_1752066176889_ai.png new file mode 100644 index 0000000..fdfdf31 Binary files /dev/null and b/screenshots/SOLUSD_5_1752066176889_ai.png differ