Skip to content

Commit

Permalink
fix: Improved script inject to fix #1366
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Oct 18, 2022
1 parent 20328c7 commit 4f7bd6e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 55 deletions.
33 changes: 19 additions & 14 deletions src/api/layers/host.layer.ts
Expand Up @@ -125,10 +125,6 @@ export class HostLayer {

let clear = !hasUserDataDir || (hasUserDataDir && !isValidToken);

this.page.on('load', () => {
this.log('verbose', 'Page loaded', { type: 'page' });
this.afterPageLoad();
});
this.page.on('close', () => {
this.cancelAutoClose();
this.log('verbose', 'Page Closed', { type: 'page' });
Expand All @@ -140,6 +136,11 @@ export class HostLayer {
clear,
this.options.whatsappVersion
);

this.page.on('load', () => {
this.log('verbose', 'Page loaded', { type: 'page' });
this.afterPageLoad();
});
}

protected async afterPageLoad() {
Expand All @@ -164,22 +165,26 @@ export class HostLayer {
await injectApi(this.page, this.onLoadingScreen)
.then(() => {
this.log('verbose', 'wapi.js injected');
this.getWAVersion()
.then((version) => {
this.log('info', `WhatsApp WEB version: ${version}`);
})
.catch(() => null);
this.getWAJSVersion()
.then((version) => {
this.log('info', `WA-JS version: ${version}`);
})
.catch(() => null);
this.afterPageScriptInjected();
})
.catch((e) => {
this.log('verbose', 'wapi.js failed');
});
}

protected async afterPageScriptInjected() {
this.getWAVersion()
.then((version) => {
this.log('info', `WhatsApp WEB version: ${version}`);
})
.catch(() => null);
this.getWAJSVersion()
.then((version) => {
this.log('info', `WA-JS version: ${version}`);
})
.catch(() => null);
}

protected tryAutoClose() {
if (this.autoCloseInterval) {
this.cancelAutoClose();
Expand Down
4 changes: 2 additions & 2 deletions src/api/layers/listener.layer.ts
Expand Up @@ -73,8 +73,8 @@ export class ListenerLayer extends ProfileLayer {
};
}

protected async afterPageLoad() {
await super.afterPageLoad();
protected async afterPageScriptInjected() {
await super.afterPageScriptInjected();

const functions = [
...Object.values(ExposedFn),
Expand Down
17 changes: 4 additions & 13 deletions src/controllers/browser.ts
Expand Up @@ -186,23 +186,13 @@ export async function injectApi(
path: require.resolve('@wppconnect/wa-js'),
});

await page
.waitForFunction(
() => {
return typeof window.WPP !== 'undefined' && window.WPP.isReady;
},
{
timeout: 60000,
}
)
.catch(() => false);

await page
.evaluate(() => {
WPP.chat.defaultSendMessageOptions.createChat = true;
WPP.conn.setKeepAlive(true);
})
.catch(() => false);

await page.addScriptTag({
path: require.resolve(
path.join(__dirname, '../../dist/lib/wapi', 'wapi.js')
Expand All @@ -211,11 +201,12 @@ export async function injectApi(

await onLoadingScreen(page, onLoadingScreenCallBack);
// Make sure WAPI is initialized
return await page
await page
.waitForFunction(() => {
return (
typeof window.WAPI !== 'undefined' &&
typeof window.Store !== 'undefined'
typeof window.Store !== 'undefined' &&
window.WPP.isReady
);
})
.catch(() => false);
Expand Down
59 changes: 33 additions & 26 deletions src/lib/wapi/wapi.js
Expand Up @@ -128,26 +128,33 @@ import {
import { getBusinessProfilesProducts, getOrderbyMsg } from './business';
import { storeObjects } from './store/store-objects';

window['webpackJsonp'] = window['webpackJsonp'] || [];
window['webpackChunkbuild'] = window['webpackChunkbuild'] || [];
const readyPromise = new Promise((resolve) => {
if (WPP.isReady) {
resolve();
return;
}
WPP.webpack.onReady(resolve);
});

if (typeof window.Store === 'undefined') {
window.Store = {};
window.Store.promises = {};

for (const store of storeObjects) {
window.Store.promises[store.id] = Promise.resolve(
WPP.webpack.search(store.conditions)
)
.then(store.conditions)
.then((m) => {
if (store.id === 'Store') {
window.Store = Object.assign({}, window.Store, m);
} else {
window.Store[store.id] = m;
}
});
}
readyPromise.then(() => {
for (const store of storeObjects) {
window.Store.promises[store.id] = Promise.resolve(
WPP.webpack.search(store.conditions)
)
.then(store.conditions)
.then((m) => {
if (store.id === 'Store') {
window.Store = Object.assign({}, window.Store, m);
} else {
window.Store[store.id] = m;
}
});
}
});
}

if (typeof window.WAPI === 'undefined') {
Expand Down Expand Up @@ -449,15 +456,15 @@ if (typeof window.WAPI === 'undefined') {
return await WPP.conn.logout();
};

addOnStreamChange();
addOnStateChange();
initNewMessagesListener();
addNewMessagesListener();
allNewMessagesListener();
addOnNewAcks();
addOnAddedToGroup();
addOnLiveLocation();
addOnParticipantsChange();
addOnNotificationMessage();
addOnPresenceChanged();
readyPromise.then(addOnStreamChange);
readyPromise.then(addOnStateChange);
readyPromise.then(initNewMessagesListener);
readyPromise.then(addNewMessagesListener);
readyPromise.then(allNewMessagesListener);
readyPromise.then(addOnNewAcks);
readyPromise.then(addOnAddedToGroup);
readyPromise.then(addOnLiveLocation);
readyPromise.then(addOnParticipantsChange);
readyPromise.then(addOnNotificationMessage);
readyPromise.then(addOnPresenceChanged);
}

0 comments on commit 4f7bd6e

Please sign in to comment.