Skip to content

Commit

Permalink
feat: check site access
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Apr 11, 2021
1 parent 60458e5 commit b822fce
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 85 deletions.
15 changes: 9 additions & 6 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ export async function initWhatsapp(
options: CreateConfig,
browser: Browser,
token?: tokenSession
): Promise<Page> {
): Promise<false | Page> {
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;
Expand Down
169 changes: 90 additions & 79 deletions src/controllers/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit b822fce

Please sign in to comment.