diff --git a/src/controllers/browser.ts b/src/controllers/browser.ts index 1e9a26668..772464710 100644 --- a/src/controllers/browser.ts +++ b/src/controllers/browser.ts @@ -68,15 +68,18 @@ export async function initWhatsapp( options: CreateConfig, browser: Browser, token?: tokenSession -): Promise { +): Promise { const waPage: Page = await getWhatsappPage(browser); if (waPage != null) { await waPage.setUserAgent(useragentOverride); - const timeout = 2 * 1000; - await Promise.race([ - waPage.goto(puppeteerConfig.whatsappUrl, { timeout }).catch(() => {}), - waPage.waitForSelector('body', { timeout }).catch(() => {}), - ]); + try { + await waPage.goto(puppeteerConfig.whatsappUrl, { + waitUntil: 'networkidle0', + }); + } catch { + waPage.close(); + return false; + } // Auth with token await auth_InjectToken(waPage, session, options, token); return waPage; diff --git a/src/controllers/initializer.ts b/src/controllers/initializer.ts index 2b5608c87..b53af8eae 100644 --- a/src/controllers/initializer.ts +++ b/src/controllers/initializer.ts @@ -244,104 +244,115 @@ export async function create( browserToken = browserSessionToken; } + spinnies.add(`whatzapp-${session}`, { + text: 'Checking page...', + }); + // Initialize whatsapp - const page = await initWhatsapp( + const page: false | Page = await initWhatsapp( session, mergedOptions, browser, browserToken ); - if (page) { - const client = new Whatsapp(page, session, mergedOptions); + if (page === false) { + spinnies.fail(`whatzapp-${session}`, { + text: 'Error accessing the page: "https://web.whatsapp.com"', + }); + throw 'Error when trying to access the page: "https://web.whatsapp.com"'; + } - client.onStreamChange(async (stateStream) => { - if (stateStream === SocketStream.CONNECTED) { - statusFind && statusFind('chatsAvailable', session); - } - if (stateStream === SocketStream.DISCONNECTED) { - let onQR: boolean = await page.evaluate(() => { - if ( - document.querySelector('canvas') && - document.querySelectorAll('#startup').length == 0 - ) { - return true; - } else { - return false; - } - }); - if (onQR === true && checkFileJson(mergedOptions, session)) { - if (statusFind) { - statusFind('desconnectedMobile', session); - } - deleteFiles(mergedOptions, session, spinnies); + spinnies.succeed(`whatzapp-${session}`, { + text: 'Page successfully accessed', + }); + + const client = new Whatsapp(page, session, mergedOptions); + + client.onStreamChange(async (stateStream) => { + if (stateStream === SocketStream.CONNECTED) { + statusFind && statusFind('chatsAvailable', session); + } + if (stateStream === SocketStream.DISCONNECTED) { + let onQR: boolean = await page.evaluate(() => { + if ( + document.querySelector('canvas') && + document.querySelectorAll('#startup').length == 0 + ) { + return true; + } else { + return false; + } + }); + if (onQR === true && checkFileJson(mergedOptions, session)) { + if (statusFind) { + statusFind('desconnectedMobile', session); } + deleteFiles(mergedOptions, session, spinnies); } - }); + } + }); - client.onStateChange((state) => { - if (state === SocketState.PAIRING) { - const device = page.evaluate(() => { - if (document.querySelectorAll('#startup').length) { - return true; - } else { - return false; - } - }); - if (device) { - if (statusFind) { - statusFind('deviceNotConnected', session); - } + client.onStateChange((state) => { + if (state === SocketState.PAIRING) { + const device = page.evaluate(() => { + if (document.querySelectorAll('#startup').length) { + return true; + } else { + return false; + } + }); + if (device) { + if (statusFind) { + statusFind('deviceNotConnected', session); } } - if (mergedOptions.createPathFileToken) { - if (state === SocketState.CONNECTED) { - setTimeout(() => { - saveToken(page, session, mergedOptions).catch((e) => { - spinnies.update(`browser-${session}`, { - text: e, - }); + } + if (mergedOptions.createPathFileToken) { + if (state === SocketState.CONNECTED) { + setTimeout(() => { + saveToken(page, session, mergedOptions).catch((e) => { + spinnies.update(`browser-${session}`, { + text: e, }); - }, 1000); - } + }); + }, 1000); } - }); + } + }); - if (mergedOptions.waitForLogin) { - const isLogged = await client.waitForLogin(catchQR, statusFind); - if (!isLogged) { - throw 'Not Logged'; - } + if (mergedOptions.waitForLogin) { + const isLogged = await client.waitForLogin(catchQR, statusFind); + if (!isLogged) { + throw 'Not Logged'; + } - let waitLoginPromise = null; - client.onStateChange(async (state) => { - if ( - state === SocketState.UNPAIRED || - state === SocketState.UNPAIRED_IDLE - ) { - if (!waitLoginPromise) { - waitLoginPromise = client - .waitForLogin(catchQR, statusFind) - .catch(() => {}) - .finally(() => { - waitLoginPromise = null; - }); - } - await waitLoginPromise; + let waitLoginPromise = null; + client.onStateChange(async (state) => { + if ( + state === SocketState.UNPAIRED || + state === SocketState.UNPAIRED_IDLE + ) { + if (!waitLoginPromise) { + waitLoginPromise = client + .waitForLogin(catchQR, statusFind) + .catch(() => {}) + .finally(() => { + waitLoginPromise = null; + }); } - }); - } + await waitLoginPromise; + } + }); + } - if (mergedOptions.debug) { - const debugURL = `http://localhost:${readFileSync( - `./${session}/DevToolsActivePort` - ).slice(0, -54)}`; - console.log(`\nDebug: \x1b[34m${debugURL}\x1b[0m`); - } - await page - .waitForSelector('#app .two', { visible: true }) - .catch(() => {}); - return client; + if (mergedOptions.debug) { + const debugURL = `http://localhost:${readFileSync( + `./${session}/DevToolsActivePort` + ).slice(0, -54)}`; + console.log(`\nDebug: \x1b[34m${debugURL}\x1b[0m`); } + await page.waitForSelector('#app .two', { visible: true }).catch(() => {}); + return client; } }