diff --git a/src/chat/events/registerAckMessageEvent.ts b/src/chat/events/registerAckMessageEvent.ts index 9d6b0ee3d7..d6d32308e9 100644 --- a/src/chat/events/registerAckMessageEvent.ts +++ b/src/chat/events/registerAckMessageEvent.ts @@ -25,7 +25,7 @@ import { SimpleAckData, } from '../../whatsapp/functions'; -webpack.onInjected(() => registerAckMessageEvent()); +webpack.onReady(registerAckMessageEvent); function registerAckMessageEvent() { MsgStore.on('change:ack', (msg: MsgModel) => { diff --git a/src/chat/events/registerLabelEvent.ts b/src/chat/events/registerLabelEvent.ts index 019863936b..0368fb7381 100644 --- a/src/chat/events/registerLabelEvent.ts +++ b/src/chat/events/registerLabelEvent.ts @@ -26,7 +26,7 @@ import { } from '../../whatsapp/functions'; import { get as getChat } from '../functions/'; -webpack.onInjected(() => register()); +webpack.onReady(register); function register() { async function processLabelEvent(event: 'add' | 'remove', ...args: any) { diff --git a/src/chat/events/registerPollEvent.ts b/src/chat/events/registerPollEvent.ts index 7a29f63c72..7d3f1da5cc 100644 --- a/src/chat/events/registerPollEvent.ts +++ b/src/chat/events/registerPollEvent.ts @@ -20,7 +20,7 @@ import { wrapModuleFunction } from '../../whatsapp/exportModule'; import { upsertVotes } from '../../whatsapp/functions'; import { getMessageById } from '../functions'; -webpack.onInjected(() => register()); +webpack.onReady(register); function register() { wrapModuleFunction(upsertVotes, async (func, ...args) => { diff --git a/src/chat/events/registerReactionsEvent.ts b/src/chat/events/registerReactionsEvent.ts index 32d8805318..bae2be28eb 100644 --- a/src/chat/events/registerReactionsEvent.ts +++ b/src/chat/events/registerReactionsEvent.ts @@ -21,7 +21,7 @@ import { MsgKey } from '../../whatsapp'; import { wrapModuleFunction } from '../../whatsapp/exportModule'; import { createOrUpdateReactions } from '../../whatsapp/functions'; -webpack.onInjected(() => register()); +webpack.onReady(register); function register() { wrapModuleFunction(createOrUpdateReactions, (func, ...args) => { diff --git a/src/chat/functions/prepareMessageButtons.ts b/src/chat/functions/prepareMessageButtons.ts index 4739b7096f..ec1579e40d 100644 --- a/src/chat/functions/prepareMessageButtons.ts +++ b/src/chat/functions/prepareMessageButtons.ts @@ -196,7 +196,7 @@ export function prepareMessageButtons( return message; } -webpack.onInjected(() => { +webpack.onReady(() => { wrapModuleFunction(createMsgProtobuf, (func, ...args) => { const [message] = args; const r = func(...args); diff --git a/src/chat/functions/sendListMessage.ts b/src/chat/functions/sendListMessage.ts index 5fe5dd6e05..eebefb3846 100644 --- a/src/chat/functions/sendListMessage.ts +++ b/src/chat/functions/sendListMessage.ts @@ -107,7 +107,7 @@ export async function sendListMessage( return await sendRawMessage(chatId, message, options); } -webpack.onInjected(() => { +webpack.onReady(() => { wrapModuleFunction(typeAttributeFromProtobuf, (func, ...args) => { const [proto] = args; if (proto.listMessage) { diff --git a/src/chat/functions/sendLocationMessage.ts b/src/chat/functions/sendLocationMessage.ts index 081ae56a09..97f386f88c 100644 --- a/src/chat/functions/sendLocationMessage.ts +++ b/src/chat/functions/sendLocationMessage.ts @@ -152,7 +152,7 @@ export async function sendLocationMessage( return await sendRawMessage(chatId, rawMessage, options); } -webpack.onInjected(() => { +webpack.onReady(() => { wrapModuleFunction(mediaTypeFromProtobuf, (func, ...args) => { const [proto] = args; if (proto.locationMessage) { diff --git a/src/chat/patch.ts b/src/chat/patch.ts index f221fd966f..3a5d7e2fcb 100644 --- a/src/chat/patch.ts +++ b/src/chat/patch.ts @@ -22,10 +22,7 @@ import { typeAttributeFromProtobuf, } from '../whatsapp/functions'; -webpack.onInjected(() => { - // Delay the register to ensure that is the last wrapped function - setTimeout(applyPatch, 1000); -}); +webpack.onReady(applyPatch, 1000); function applyPatch() { wrapModuleFunction(mediaTypeFromProtobuf, (func, ...args) => { diff --git a/src/group/events/registerParticipantsChangedEvent.ts b/src/group/events/registerParticipantsChangedEvent.ts index 22ca19fd39..c38e98c17d 100644 --- a/src/group/events/registerParticipantsChangedEvent.ts +++ b/src/group/events/registerParticipantsChangedEvent.ts @@ -20,7 +20,7 @@ import { Wid } from '../../whatsapp'; import { wrapModuleFunction } from '../../whatsapp/exportModule'; import { updateDBForGroupAction } from '../../whatsapp/functions'; -webpack.onInjected(() => register()); +webpack.onReady(register); function register() { const eventTypes = ['add', 'remove', 'demote', 'promote']; diff --git a/src/webpack/index.ts b/src/webpack/index.ts index a77c79b5d8..7081defef1 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -36,12 +36,10 @@ export function onInjected(listener: () => void, delay = 0): void { }); } -export function onReady(listener: () => void): void { - internalEv.on('webpack.ready', () => - Promise.resolve() - .then(listener) - .catch(() => null) - ); +export function onReady(listener: () => void, delay = 1000): void { + internalEv.on('webpack.ready', () => { + setTimeout(listener, delay); + }); } export type SearchModuleCondition = (module: any, moduleId: string) => boolean; @@ -99,7 +97,7 @@ export function injectLoader(): void { filename.includes(`locales/${lang}`) ); } - return true; + return filename.includes('main'); }); const sortWeight: [RegExp, number][] = [ diff --git a/src/whatsapp/exportModule.ts b/src/whatsapp/exportModule.ts index fcb23a8de6..a20a0ca18f 100644 --- a/src/whatsapp/exportModule.ts +++ b/src/whatsapp/exportModule.ts @@ -14,10 +14,14 @@ * limitations under the License. */ +import Debug from 'debug'; + import { trackException } from '../gtag'; import { InferArgs, InferReturn, wrapFunction } from '../util'; import * as webpack from '../webpack'; +const debug = Debug('WA-JS:export'); + class CustomWeakMap extends WeakMap { protected stringMap = new Map(); @@ -240,6 +244,8 @@ export function wrapModuleFunction any>( return; } + debug.extend('wrap')(`Wrapping '${functionPath} for module ${moduleId}'`); + const parts = functionPath.split('.'); const functionName = parts.pop();