Improve login verification and layout access handling
- Add login restriction detection for private layout URLs - Implement re-authentication when layout access is denied - Add proper login state management with flag updates - Verify user login status before accessing layout URLs - Add fallback to base chart when layout is not accessible - Enhance error handling for login-protected layouts - Ensure session persistence across layout navigation This fixes the issue where layout URLs were failing due to insufficient login verification.
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user