Skip to content

Commit

Permalink
Small improvements in inject function
Browse files Browse the repository at this point in the history
  • Loading branch information
MobCode100 committed Jul 3, 2024
1 parent a936725 commit 93e9b57
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Client extends EventEmitter {
*/
async inject(reinject = false) {
await this.pupPage.waitForFunction('window.Debug?.VERSION != undefined', {timeout: this.options.authTimeoutMs});

const pairWithPhoneNumber = this.options.pairWithPhoneNumber;
const version = await this.getWWebVersion();
const isCometOrAbove = parseInt(version.split('.')?.[1]) >= 3000;

Expand Down Expand Up @@ -140,26 +140,26 @@ class Client extends EventEmitter {
}

// Register qr/code events
let qrRetries = 0;
const injected = await this.pupPage.evaluate(() => {
return typeof window.onQRChangedEvent !== 'undefined' || typeof window.onCodeReceivedEvent !== 'undefined';
});
if(this.options.pairWithPhoneNumber.phoneNumber){
if(pairWithPhoneNumber.phoneNumber){
if (!injected) {
await this.pupPage.exposeFunction('onCodeReceivedEvent', async (code) => {
/**
* Emitted when a pairing code is received
* @event Client#code
* @param {string} code Code
* @returns {string} Code that was just received
*/
this.emit(Events.CODE_RECEIVED, code);
return code;
});
}
const pairWithPhoneNumber = this.options.pairWithPhoneNumber;
const code = await this.requestPairingCode(pairWithPhoneNumber.phoneNumber,pairWithPhoneNumber.showNotification,pairWithPhoneNumber.intervalMs);
this.emit(Events.CODE_RECEIVED, code); // initial code
this.requestPairingCode(pairWithPhoneNumber.phoneNumber,pairWithPhoneNumber.showNotification,pairWithPhoneNumber.intervalMs);
} else {
if(!injected){
let qrRetries = 0;
await this.pupPage.exposeFunction('onQRChangedEvent', async (qr) => {
/**
* Emitted when a QR code is received
Expand Down Expand Up @@ -195,7 +195,7 @@ class Client extends EventEmitter {

if (!reinject) {
await this.pupPage.exposeFunction('onAuthAppStateChangedEvent', async (state) => {
if (state == 'UNPAIRED_IDLE') {
if (state == 'UNPAIRED_IDLE' && !pairWithPhoneNumber.phoneNumber) {
// refresh qr code
window.Store.Cmd.refreshQR();
}
Expand Down Expand Up @@ -382,7 +382,7 @@ class Client extends EventEmitter {
return window.AuthStore.PairingCodeLinkUtils.startAltLinkingFlow(phoneNumber, showNotification);
}
if (window.codeInterval) {
clearInterval(window.codeInterval) // remove existing interval
clearInterval(window.codeInterval); // remove existing interval
}
window.codeInterval = setInterval(async () => {
if (window.AuthStore.AppState.state != 'UNPAIRED' && window.AuthStore.AppState.state != 'UNPAIRED_IDLE') {
Expand All @@ -391,7 +391,7 @@ class Client extends EventEmitter {
}
window.onCodeReceivedEvent(await getCode());
}, intervalMs);
return getCode();
return window.onCodeReceivedEvent(await getCode());
}, phoneNumber, showNotification, intervalMs);
}

Expand Down

0 comments on commit 93e9b57

Please sign in to comment.