Skip to content

Commit

Permalink
fix: close and inject
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Apr 11, 2021
1 parent b822fce commit 20e2f74
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 32 deletions.
50 changes: 26 additions & 24 deletions src/api/helpers/closes-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,32 @@ export async function checkingCloses(
mergedOptions: CreateConfig,
callStatus: (e: string) => void
) {
if (typeof browser !== 'string') {
let err: boolean;
do {
try {
await new Promise((r) => setTimeout(r, 2000));
if (
browser['isClose'] ||
(mergedOptions.browserWS && !browser.isConnected())
) {
if (mergedOptions.browserWS) {
browser.disconnect();
callStatus && callStatus('serverClose');
new Promise(async (resolve, reject) => {
if (typeof browser !== 'string') {
let err: boolean;
do {
try {
await new Promise((r) => setTimeout(r, 2000));
if (
browser['isClose'] ||
(mergedOptions.browserWS && !browser.isConnected())
) {
if (mergedOptions.browserWS) {
browser.disconnect();
callStatus && callStatus('serverClose');
}
if (browser['isClose']) {
browser.close().catch((e) => reject(e));
callStatus && callStatus('browserClose');
}
err = false;
} else {
throw 1;
}
if (browser['isClose']) {
browser.close();
callStatus && callStatus('browserClose');
}
err = false;
} else {
throw 1;
} catch (e) {
err = true;
}
} catch (e) {
err = true;
}
} while (err);
}
} while (err);
}
});
}
2 changes: 1 addition & 1 deletion src/api/layers/host.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export class HostLayer {

let authenticated = await isAuthenticated(this.page).catch(() => null);

if (typeof authenticated === 'object') {
if (typeof authenticated === 'object' && authenticated.type) {
this.spin(`Error http: ${authenticated.type}`, 'fail');
this.page.close();
throw `Error http: ${authenticated.type}`;
Expand Down
1 change: 0 additions & 1 deletion src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import { existsSync, readFileSync } from 'fs';
import { CreateConfig } from '../config/create-config';
import { ScrapQrcode } from '../api/model/qrcode';
import { tokenSession } from '../config/tokenSession.config';
import { puppeteerConfig } from '../config/puppeteer.config';

export const getInterfaceStatus = async (
waPage: puppeteer.Page
Expand Down
15 changes: 9 additions & 6 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,21 @@ export async function initWhatsapp(
): Promise<false | Page> {
const waPage: Page = await getWhatsappPage(browser);
if (waPage != null) {
await waPage.setUserAgent(useragentOverride);
try {
await waPage.setUserAgent(useragentOverride);
await waPage.goto(puppeteerConfig.whatsappUrl, {
waitUntil: 'networkidle0',
waitUntil: 'domcontentloaded',
});
// Auth with token
await auth_InjectToken(waPage, session, options, token);
await waPage.evaluate(() => {
window.location.reload();
});
return waPage;
} catch {
waPage.close();
waPage.close().catch(() => {});
return false;
}
// Auth with token
await auth_InjectToken(waPage, session, options, token);
return waPage;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/controllers/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ export async function create(

checkingCloses(browser, mergedOptions, (result) => {
statusFind && statusFind(result, session);
}).catch(() => {
throw 'The client has been closed';
});

if (SessionTokenCkeck(browserSessionToken)) {
Expand Down

0 comments on commit 20e2f74

Please sign in to comment.