Skip to content

Commit

Permalink
feat(context): Treatment of the puppeteer.connect() function
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Oct 13, 2020
1 parent fed693c commit 0dbebee
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 147 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ venom
(statusSession) => {
console.log('Status Session: ', statusSession);
//return isLogged || notLogged || browserClose || qrReadSuccess || qrReadFail || autocloseCalled
//
A criação de sessão no wss retorna "serverClose" se o servidor não estiver funcionando.
},
undefined
)
Expand Down
3 changes: 2 additions & 1 deletion src/api/layers/retriever.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import { Page } from 'puppeteer';
import { async } from 'rxjs';
import {
Chat,
Contact,
Expand Down Expand Up @@ -97,7 +98,7 @@ declare module WAPI {
includeMe: boolean,
includeNotifications: boolean
) => Message[];
const getSessionTokenBrowser: () => object;
const getSessionTokenBrowser: () => void;
}

export class RetrieverLayer extends SenderLayer {
Expand Down
30 changes: 19 additions & 11 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export const isInsideChat = (waPage: puppeteer.Page) => {
};
export async function retrieveQR(page: puppeteer.Page) {
const { code, data } = await decodeQR(page);
if (data === null || code === null) {
return false;
}
const asciiQR = await asciiQr(code);
return { code, data, asciiQR };
}
Expand All @@ -120,22 +123,27 @@ async function decodeQR(
page: puppeteer.Page
): Promise<{ code: string; data: string }> {
await page.waitForSelector('canvas', { timeout: 0 });

await page.addScriptTag({
path: require.resolve(path.join(__dirname, '../lib/jsQR', 'jsQR.js')),
});

return await page.evaluate(() => {
const canvas = document.querySelector('canvas');
const context = canvas.getContext('2d');

// @ts-ignore
const code = jsQR(
context.getImageData(0, 0, canvas.width, canvas.height).data,
canvas.width,
canvas.height
);

return { code: code.data, data: canvas.toDataURL() };
const canvas = document.querySelector('canvas') || null;
if (canvas !== null) {
const context = canvas.getContext('2d') || null;
if (context !== null) {
// @ts-ignore
const code = jsQR(
context.getImageData(0, 0, canvas.width, canvas.height).data,
canvas.width,
canvas.height
);
return { code: code.data, data: canvas.toDataURL() };
}
} else {
return { code: null, data: null };
}
});
}

Expand Down
6 changes: 4 additions & 2 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export async function initBrowser(
browser = e;
})
.catch(() => {
throw new Error('Error when try to connect');
browser = 'connect';
});
} else {
await puppeteer
Expand All @@ -159,7 +159,9 @@ export async function initBrowser(
.then((e) => {
browser = e;
})
.catch(() => {});
.catch(() => {
browser = 'launch';
});
}

return browser;
Expand Down
Loading

1 comment on commit 0dbebee

@jonalan7
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please sign in to comment.