Skip to content

Commit

Permalink
feat: Added support to WhasApp WEB 2.3000.x
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Feb 29, 2024
1 parent 7f68264 commit dca69de
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 45 deletions.
90 changes: 85 additions & 5 deletions src/webpack/index.ts
Expand Up @@ -20,6 +20,11 @@ import { internalEv } from '../eventEmitter';

const debug = Debug('WA-JS:webpack');

/**
* Is setted true when the loader is injected
*/
export let loaderType: 'meta' | 'unknown' | 'webpack' = 'unknown';

/**
* Is setted true when the loader is injected
*/
Expand Down Expand Up @@ -55,6 +60,14 @@ export function onFullReady(listener: () => void, delay = 0): void {

export type SearchModuleCondition = (module: any, moduleId: string) => boolean;

export const __debug = () => {
const global = (self || window) as any;

return global.require('__debug') as {
modulesMap: { [key: string]: any };
};
};

export let webpackRequire: (<T = any>(moduleId: string) => T) & {
/**
* module list
Expand All @@ -75,22 +88,85 @@ export let webpackRequire: (<T = any>(moduleId: string) => T) & {
*/
export const fallbackModules: { [key: string]: any } = {};

const waitMainInit = internalEv.waitFor('conn.main_init');
const waitMainReady = internalEv.waitFor('conn.main_ready');

export function injectLoader(): void {
if (isInjected) {
return;
}

const global = (self || window) as any;

/* BEGIN: For WhatsApp >= 2.3000.0 */
const metaTimer = setInterval(async () => {
if (loaderType !== 'unknown') {
clearInterval(metaTimer);
return;
}
if (!global.require || !global.__d) {
return;
}
loaderType = 'meta';

// A wrap to work like webpack
webpackRequire = function (id: string) {
try {
global.ErrorGuard.skipGuardGlobal(true);
return global.importNamespace(id);
} catch (error) {}
return null;
} as any;

Object.defineProperty(webpackRequire, 'm', {
get: () => {
const modulesMap = __debug().modulesMap;
const ids = Object.keys(modulesMap).filter((id) =>
/^(?:use)?WA/.test(id)
);
const result: any = {};

for (const id of ids) {
result[id] = modulesMap[id]?.factory;
}

return result;
},
});

isInjected = true;
debug('injected');
await internalEv.emitAsync('webpack.injected').catch(() => null);

await waitMainInit;
await new Promise((resolve) => setTimeout(resolve, 1000));
isReady = true;
debug('ready to use');
await internalEv.emitAsync('webpack.ready').catch(() => null);

if ((window as any).wppForceMainLoad) {
await new Promise((resolve) => setTimeout(resolve, 5000));
} else {
await waitMainReady;
}
isFullReady = true;
debug('full ready to use');
await internalEv.emitAsync('webpack.full_ready').catch(() => null);
}, 1000);

/* END: For WhatsApp >= 2.3000.0 */

const chunkName = 'webpackChunkwhatsapp_web_client';

const global = (self || window) as any;
const chunk = global[chunkName] || [];
if (typeof global[chunkName] === 'undefined') {
global[chunkName] = chunk;
} else {
loaderType = 'webpack';
}

const injectFunction = async (__webpack_require__: any) => {
loaderType = 'webpack';
webpackRequire = __webpack_require__;

isInjected = true;
Expand Down Expand Up @@ -165,7 +241,11 @@ export function injectLoader(): void {
const sourceModuleMap = new Map<string, boolean>();

export function moduleSource(moduleId: string) {
if (typeof webpackRequire.m[moduleId] === 'undefined') {
if (loaderType !== 'webpack') {
return '';
}

if (!webpackRequire.m[moduleId]) {
return '';
}

Expand Down Expand Up @@ -311,7 +391,7 @@ export function modules(
}

export function loadModule<T = any>(moduleId: string) {
const module = /^\d+$/.test(moduleId)
const module = !/^fallback_/.test(moduleId)
? webpackRequire(moduleId)
: fallbackModules[moduleId];

Expand All @@ -330,9 +410,9 @@ export function injectFallbackModule(
): void {
moduleId = moduleId + '';

if (/^\d+$/.test(moduleId)) {
if (/^fallback_/.test(moduleId)) {
throw new Error('Invalid fallback ID');
}

fallbackModules[moduleId] = content;
fallbackModules[`fallback_${moduleId}`] = content;
}
2 changes: 1 addition & 1 deletion src/whatsapp/functions/contactFunctions.ts
Expand Up @@ -262,7 +262,7 @@ exportModule(
getFormattedName: 'getFormattedName',
getFormattedUser: 'getFormattedUser',
},
(m) => m.getDisplayName
(m) => m.getDisplayName && m.getFormattedName
);
injectFallbackModule('getDisplayName', {
getDisplayName: (contact: ContactModel) => contact.displayName,
Expand Down
3 changes: 3 additions & 0 deletions src/whatsapp/functions/uploadThumbnail.ts
Expand Up @@ -48,6 +48,9 @@ exportModule(
uploadThumbnail: 'default',
},
(m, id) => {
if (id === 'WAWebMediaUploadMmsThumbnail') {
return true;
}
const source: string = webpack.moduleSource(id);
return (
source.includes('thumbnail') &&
Expand Down
3 changes: 2 additions & 1 deletion src/whatsapp/misc/EventEmitter.ts
Expand Up @@ -125,6 +125,7 @@ exportModule(
{
EventEmitter: 'default',
},
(m) =>
(m, id) =>
id === 'WAWebEventEmitter' ||
m.default.toString().includes('Callback parameter passed is not a function')
);
34 changes: 0 additions & 34 deletions src/whatsapp/misc/Locale.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/whatsapp/misc/index.ts
Expand Up @@ -22,7 +22,6 @@ export * from './Conn';
export * from './Constants';
export * from './EventEmitter';
export * from './ImageUtils';
export * from './Locale';
export * from './MediaBlobCache';
export * from './MediaEntry';
export * from './MediaObject';
Expand Down
8 changes: 6 additions & 2 deletions src/whatsapp/models/ModelChatBase.ts
Expand Up @@ -49,6 +49,10 @@ export declare class ModelChatBase extends Model<ChatCollection> {
notifyMsgCollectionMerge(...args: any[]): void;
}

exportModule(exports, { ModelChatBase: 'default' }, (m) =>
m.default.toString().includes('onEmptyMRM not implemented')
exportModule(
exports,
{ ModelChatBase: 'default' },
(m, id) =>
id === 'WAWebSuperChatMsgs' ||
m.default.toString().includes('onEmptyMRM not implemented')
);
2 changes: 1 addition & 1 deletion webpack.config.js
Expand Up @@ -25,7 +25,7 @@ module.exports = {
path: path.resolve(__dirname, 'dist'),
library: {
name: 'WPP',
type: 'umd',
type: 'global',
},
},
plugins: [
Expand Down

0 comments on commit dca69de

Please sign in to comment.