Skip to content

Commit

Permalink
fix: Fixed WhatsApp WEB loading error (fix #1249)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Aug 26, 2023
1 parent f17444c commit 331de74
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/chat/events/registerAckMessageEvent.ts
Expand Up @@ -25,7 +25,7 @@ import {
SimpleAckData,
} from '../../whatsapp/functions';

webpack.onInjected(() => registerAckMessageEvent());
webpack.onReady(registerAckMessageEvent);

function registerAckMessageEvent() {
MsgStore.on('change:ack', (msg: MsgModel) => {
Expand Down
2 changes: 1 addition & 1 deletion src/chat/events/registerLabelEvent.ts
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/chat/events/registerPollEvent.ts
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/chat/events/registerReactionsEvent.ts
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/chat/functions/prepareMessageButtons.ts
Expand Up @@ -196,7 +196,7 @@ export function prepareMessageButtons<T extends RawMessage>(
return message;
}

webpack.onInjected(() => {
webpack.onReady(() => {
wrapModuleFunction(createMsgProtobuf, (func, ...args) => {
const [message] = args;
const r = func(...args);
Expand Down
2 changes: 1 addition & 1 deletion src/chat/functions/sendListMessage.ts
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/chat/functions/sendLocationMessage.ts
Expand Up @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions src/chat/patch.ts
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/group/events/registerParticipantsChangedEvent.ts
Expand Up @@ -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'];
Expand Down
12 changes: 5 additions & 7 deletions src/webpack/index.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -99,7 +97,7 @@ export function injectLoader(): void {
filename.includes(`locales/${lang}`)
);
}
return true;
return filename.includes('main');
});

const sortWeight: [RegExp, number][] = [
Expand Down
6 changes: 6 additions & 0 deletions src/whatsapp/exportModule.ts
Expand Up @@ -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<object, string> {
protected stringMap = new Map<string, string>();

Expand Down Expand Up @@ -240,6 +244,8 @@ export function wrapModuleFunction<TFunc extends (...args: any[]) => any>(
return;
}

debug.extend('wrap')(`Wrapping '${functionPath} for module ${moduleId}'`);

const parts = functionPath.split('.');

const functionName = parts.pop();
Expand Down

0 comments on commit 331de74

Please sign in to comment.