Skip to content

Commit

Permalink
fix: wapi and crypto mod
Browse files Browse the repository at this point in the history
  • Loading branch information
vanflux committed Apr 18, 2024
1 parent d509f6c commit 37b7de2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/page/components/windows/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const Windows = () => {
<Window
name="cryptography"
icon={<Icon type="lock" size={16} />}
defaultPosition={{ x: 16, y: 96 }}
defaultPosition={{ x: 112, y: 112 }}
title="Cryptography"
getNextZIndex={getNextZIndex}
>
Expand All @@ -32,7 +32,7 @@ export const Windows = () => {
<Window
name="themes"
icon={<Icon type="paint" size={16} />}
defaultPosition={{ x: 16, y: 56 }}
defaultPosition={{ x: 80, y: 80 }}
title="Themes"
getNextZIndex={getNextZIndex}
>
Expand All @@ -41,7 +41,7 @@ export const Windows = () => {
<Window
name="configs"
icon={<Icon type="config" size={16} />}
defaultPosition={{ x: 16, y: 16 }}
defaultPosition={{ x: 48, y: 48 }}
title="Configs"
getNextZIndex={getNextZIndex}
>
Expand Down
10 changes: 5 additions & 5 deletions src/page/features/configs-toggle/configs-toggle.mod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export class ConfigsMod {
this.root = createRoot(container);
this.root.render(<ConfigsToggleButton />);
this.timer = setInterval(() => {
const icon = document.querySelector('header [data-icon="community-tab"]');
const menuBarCommunityTab = icon?.parentElement?.parentElement;
if (menuBarCommunityTab) {
const parent = menuBarCommunityTab.parentElement;
const icon = document.querySelector('header [data-icon="community-outline"]');
const menuBarCommunity = icon?.parentElement?.parentElement;
if (menuBarCommunity) {
const parent = menuBarCommunity.parentElement;
if (!parent?.contains(container)) {
if (parent) {
parent.insertBefore(container, menuBarCommunityTab);
parent.insertBefore(container, menuBarCommunity);
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/page/features/cryptography/cryptography.mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class CryptographyMod {
if (oldConfig?.autoDecrypt !== config?.autoDecrypt || oldConfig?.hideEncryptedBody !== config?.hideEncryptedBody) this.updateAllMessages();
}

private static async updateMessage(messageModel: any) {
private static async updateMessage(messageModel: any, isNew: boolean) {
if (!messageModel) return;
if (messageModel.__vfOriginalBody == null) messageModel.__vfOriginalBody = messageModel.body;
const originalBody = messageModel.__vfOriginalBody;
Expand All @@ -47,8 +47,13 @@ export class CryptographyMod {
if (!module) return;
if (messageModel.__vfDecryptedBody == null) messageModel.__vfDecryptedBody = await module.decrypt(senderId, encryptedMessage);
const message = messageModel.__vfDecryptedBody;
if (isNew) {
while (messageModel.ack < 1) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
}
if (this.config.hideEncryptedBody) {
if (message) messageModel.body = `[${moduleName}] ${message}`;
if (message) messageModel.body = message;
} else {
if (message) messageModel.body = `[Decrypted-${moduleName}] ${message}\n\n[Encrypted-${moduleName}] ${encryptedMessage}`;
}
Expand All @@ -58,16 +63,16 @@ export class CryptographyMod {
}

private static updateAllMessages() {
WapiMod.getAllMessages().forEach((message: any) => this.updateMessage(message));
WapiMod.getAllMessages().forEach((message: any) => this.updateMessage(message, false));
}

private static applyMessageHooks() {
WapiMod.onAnyMessage((message: any) => this.updateMessage(message));
WapiMod.onAnyMessage((message: any) => this.updateMessage(message, true));
this.destroySetAttributeHook = hookFn(HTMLDivElement.prototype, "setAttribute", async (key, value) => {
if (key === "data-id") {
const message = WapiMod.getMessageById(value);
if (!message) return;
this.updateMessage(message);
this.updateMessage(message, true);
}
});
}
Expand Down
38 changes: 28 additions & 10 deletions src/page/features/wapi/wapi.mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { EventEmitter } from "events";
declare global {
interface Window {
Store: any;
webpackChunkbuild: any;
webpackChunkwhatsapp_web_client: any;
}
}

Expand Down Expand Up @@ -61,19 +59,39 @@ export class WapiMod {
return this.getChatById(id).sendMessage(message);
}

private static async waitModules() {
private static async inject() {
const start = Date.now();
while (Date.now() - start < 10000) {
if (window?.webpackChunkwhatsapp_web_client?.length && window.webpackChunkwhatsapp_web_client.length > 15) return;
await new Promise((resolve) => setTimeout(resolve, 100));
try {
if (
// @ts-ignore
window.Debug?.VERSION != undefined &&
// @ts-ignore
window.require?.("__debug").modulesMap["WAWebLoadMainBundleFileDefinitions"] &&
this.tryInjectWapi()
)
return;
} catch (exc: unknown) {
console.log("Waiting modules to load:", exc);
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
throw new Error("Wait modules failed");
}

private static injectWapi() {
window.webpackChunkbuild = window.webpackChunkwhatsapp_web_client;
require("./downloaded/wapi.js");
private static tryInjectWapi() {
try {
(() => {
WAPI_JS_CODE; // This seams random yeah, but code is injected here
})();
if (window?.Store.Msg && window?.Store.Chat) return true;
} catch (exc: unknown) {
console.log("wapi.js code injection failure:", exc);
}
return false;
}

private static async hook() {
this.listen(window?.Store?.Msg, "add", (message: any) => {
this.events.emit("anyMessage", message);
});
Expand All @@ -89,8 +107,8 @@ export class WapiMod {
}

public static async apply() {
await this.waitModules();
this.injectWapi();
await this.inject();
await this.hook();
}

public static destroy() {
Expand Down
1 change: 1 addition & 0 deletions src/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ declare interface Window {
}

declare var VERSION: string;
declare var WAPI_JS_CODE: string;
10 changes: 8 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { ProgressPlugin } = require("webpack");
const { DefinePlugin, ProgressPlugin } = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const { resolve } = require("path");
const { TsconfigPathsPlugin } = require("tsconfig-paths-webpack-plugin");
const { readFileSync } = require("fs");

module.exports = {
resolve: {
Expand All @@ -18,5 +19,10 @@ module.exports = {
}),
],
},
plugins: [new ProgressPlugin()],
plugins: [
new ProgressPlugin(),
new DefinePlugin({
WAPI_JS_CODE: readFileSync(resolve(__dirname, "./src/page/features/wapi/downloaded/wapi.js"), "utf8"),
}),
],
};

0 comments on commit 37b7de2

Please sign in to comment.