feat: refresh qr code (#68)

close #60
This commit is contained in:
sahalMoidu
2020-02-23 21:50:11 +05:30
committed by GitHub
parent 37932d9a69
commit 5c9e76e23f

View File

@@ -83,21 +83,34 @@ class Client extends EventEmitter {
} }
} else { } else {
// Wait for QR Code const getQrCode = async () => {
const QR_CANVAS_SELECTOR = 'canvas'; // Check if retry button is present
await page.waitForSelector(QR_CANVAS_SELECTOR); var QR_RETRY_SELECTOR = 'div[data-ref] > span > div';
const qrImgData = await page.$eval(QR_CANVAS_SELECTOR, canvas => [].slice.call(canvas.getContext('2d').getImageData(0, 0, 264, 264).data)); var qrRetry = await page.$(QR_RETRY_SELECTOR);
const qr = jsQR(qrImgData, 264, 264).data; if (qrRetry) {
await qrRetry.click();
}
/** // Wait for QR Code
* Emitted when the QR code is received
* @event Client#qr const QR_CANVAS_SELECTOR = 'canvas';
* @param {string} qr QR Code await page.waitForSelector(QR_CANVAS_SELECTOR);
*/ const qrImgData = await page.$eval(QR_CANVAS_SELECTOR, canvas => [].slice.call(canvas.getContext('2d').getImageData(0, 0, 264, 264).data));
this.emit(Events.QR_RECEIVED, qr); const qr = jsQR(qrImgData, 264, 264).data;
/**
* Emitted when the QR code is received
* @event Client#qr
* @param {string} qr QR Code
*/
this.emit(Events.QR_RECEIVED, qr);
};
getQrCode();
let retryInterval = setInterval(getQrCode, 20000); // check for qr code every 20 seconds
// Wait for code scan // Wait for code scan
await page.waitForSelector(KEEP_PHONE_CONNECTED_IMG_SELECTOR, { timeout: 0 }); await page.waitForSelector(KEEP_PHONE_CONNECTED_IMG_SELECTOR, { timeout: 0 });
clearInterval(retryInterval);
} }
await page.evaluate(ExposeStore, moduleRaid.toString()); await page.evaluate(ExposeStore, moduleRaid.toString());
@@ -281,7 +294,7 @@ class Client extends EventEmitter {
if (newChatId) { if (newChatId) {
//get the topmost chat object and assign the new chatId to it . //get the topmost chat object and assign the new chatId to it .
//This is just a workaround.May cause problem if there are no chats at all. Need to dig in and emulate how whatsapp web does //This is just a workaround.May cause problem if there are no chats at all. Need to dig in and emulate how whatsapp web does
let chat = window.Store.Chat.models[0]; let chat = window.Store.Chat.models[0];
let originalChatObjId = chat.id; let originalChatObjId = chat.id;
chat.id = newChatId; chat.id = newChatId;
msg = await window.WWebJS.sendMessage(chat, message, options); msg = await window.WWebJS.sendMessage(chat, message, options);