Skip to content

Commit

Permalink
fix: QRCode Scan terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Aug 11, 2023
1 parent 9c7d5f3 commit 33d8d50
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
24 changes: 12 additions & 12 deletions src/api/helpers/scrape-img-qr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import { Page } from 'puppeteer';
import { ScrapQrcode } from '../model/qrcode';

export async function scrapeImg(page: Page): Promise<ScrapQrcode | undefined> {
let click = await page
.evaluate(() => {
const buttonReload = document.querySelector('button');
if (buttonReload != null) {
buttonReload.click();
return true;
}
return false;
})
.catch(() => false);
let click = await page.evaluate(async () => {
const buttonReload = document.querySelector('button.Jht5u');
if (buttonReload != null) {
return true;
}
return false;
});

if (click) {
await page.waitForNavigation().catch(() => undefined);
const buttonReloadElementHandle = await page.$('button.Jht5u');
if (buttonReloadElementHandle) {
await buttonReloadElementHandle.click();
}
}

const result = await page
.evaluate(() => {
const selectorImg = document.querySelector('canvas');
const selectorUrl = selectorImg.closest('[data-ref]');
const buttonReload = document.querySelector('button');
const buttonReload = document.querySelector('button.Jht5u');

if (buttonReload === null && selectorImg != null && selectorUrl != null) {
let data = {
Expand Down
8 changes: 4 additions & 4 deletions src/api/layers/host.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,12 @@ export class HostLayer {
}

public async getQrCode() {
let qrResult: ScrapQrcode | undefined;
let qrResult: ScrapQrcode | undefined | any;

qrResult = await scrapeImg(this.page).catch(() => undefined);
qrResult = await scrapeImg(this.page).catch((e) => console.log(e));
if (!qrResult || !qrResult.urlCode) {
qrResult = await retrieveQR(this.page).catch(() => undefined);
qrResult = await retrieveQR(this.page).catch((e) => console.log(e));
}

return qrResult;
}

Expand All @@ -152,6 +151,7 @@ export class HostLayer {
break;
}
const result = await this.getQrCode().catch(() => null);

if (!result.urlCode) {
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export async function retrieveQR(
): Promise<ScrapQrcode | undefined> {
const hasCanvas = await page
.evaluate(() => {
const buttonReload = document.querySelector('button');
const buttonReload = document.querySelector('button.Jht5u');
const canvas = document.querySelector('canvas');
if (canvas !== null && buttonReload === null) {
return true;
Expand All @@ -131,7 +131,7 @@ export async function retrieveQR(

return await page
.evaluate(() => {
const buttonReload = document.querySelector('button');
const buttonReload = document.querySelector('button.Jht5u');
const canvas = document.querySelector('canvas') || null;
if (canvas !== null && buttonReload === null) {
const context = canvas.getContext('2d') || null;
Expand Down
30 changes: 27 additions & 3 deletions src/controllers/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export async function create(
spinnies.fail(`whatzapp-disconnected-${session}`, {
text: 'Was disconnected!'
});
document.querySelectorAll('.MLTJU p')[0].textContent;
statusFind && statusFind('desconnected', session);
}

Expand Down Expand Up @@ -385,7 +386,10 @@ export async function create(
const mode = await page
.evaluate(() => window?.Store?.Stream?.mode)
.catch(() => {});
if (mode === InterfaceMode.QR) {
if (
mode === InterfaceMode.QR
// && checkFileJson(mergedOptions, session)
) {
if (statusFind) {
spinnies.add(`whatzapp-qr-${session}`, {
text: 'check....'
Expand All @@ -403,8 +407,22 @@ export async function create(
client
.onStateChange(async (state) => {
if (state === SocketState.PAIRING) {
if (statusFind) {
statusFind('deviceNotConnected', session);
const device: Boolean = await page
.evaluate(() => {
if (
document.querySelector('[tabindex="-1"]') &&
window?.Store?.Stream?.mode === InterfaceMode.SYNCING &&
window?.Store?.Stream?.obscurity === 'SHOW'
) {
return true;
}
return false;
})
.catch(() => undefined);
if (device === true) {
if (statusFind) {
statusFind('deviceNotConnected', session);
}
}
}
})
Expand Down Expand Up @@ -457,6 +475,12 @@ export async function create(
.waitForSelector('#app .two', { visible: true })
.catch(() => {});

try {
spinnies.succeed(`whatzapp-intro-${session}`, {
text: 'Successfully connected!'
});
} catch {}

await client.initService();
await client.addChatWapi();

Expand Down

1 comment on commit 33d8d50

@jonalan7
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were more than one button on the QR Code, besides the reload button, and that's precisely what posed the issue. #2429

Please sign in to comment.