From 559a14832a906e06c6fe9d32d0fab0ab948e5b09 Mon Sep 17 00:00:00 2001 From: Francisco Pessano Date: Thu, 3 Apr 2025 20:21:34 -0300 Subject: [PATCH] Main logic implemented --- .tool-versions | 2 + injected.js | 123 ++++++++++++++++++++++++++++++------------------- 2 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..a381f94 --- /dev/null +++ b/.tool-versions @@ -0,0 +1,2 @@ +nodejs 22.14.0 +pnpm 8.15.4 \ No newline at end of file diff --git a/injected.js b/injected.js index 868009f..770a2a3 100644 --- a/injected.js +++ b/injected.js @@ -1,56 +1,83 @@ +const EMOJI_TARGET = "❤"; // Acting like ❤️ on Whatsapp + (function () { - console.log("WhatsApp Heart Favorites script - Candidate Test"); + console.log("WhatsApp Heart Favorites script - Candidate Test"); - // Initialize WhatsApp Store access - function initializeWAStore() { - // Wait for WhatsApp to initialize its modules - const storeCheckInterval = setInterval(() => { - if (window.Store) { - clearInterval(storeCheckInterval); - console.log("WhatsApp store already initialized"); - setupReactionListener(); - return; - } + // Initialize WhatsApp Store access + function initializeWAStore() { + window.Store = null; - if (typeof window.require === "function") { - try { - // Load the essential modules - window.Store = Object.assign({}, window.require("WAWebCollections")); - window.Store.Cmd = window.require("WAWebCmd").Cmd; + // Wait for WhatsApp to initialize its modules + const storeCheckInterval = setInterval(() => { + if (window.Store) { + clearInterval(storeCheckInterval); + console.log("WhatsApp store already initialized"); + setupReactionListener(); + return; + } - clearInterval(storeCheckInterval); - console.log("WhatsApp store initialized"); - setupReactionListener(); - } catch (error) { - console.error("Error initializing WhatsApp store:", error); - } - } - }, 1000); - } + if (typeof window.require === "function") { + try { + if (!window.require("WAWebCollections").Reactions._models?.length) { + console.log("Not injected. Still not defined"); + return; + } + // Load the essential modules + window.Store = Object.assign({}, window.require("WAWebCollections")); + window.Store.Cmd = window.require("WAWebCmd").Cmd; - // Set up listeners for heart reactions - function setupReactionListener() { - // TODO: Implement the reaction listener here - // - // CANDIDATE TASK: Write code that will star a message when you react to it with a heart emoji - // - // Hints: - // 1. You'll need to listen for reaction events using Store.Reactions - // 2. Check if the reaction is a heart emoji (❤️) - // 3. Use Store.Msg to get the message object - // 4. Use Store.Chat to get the chat object - // 5. Use Store.Cmd.sendStarMsgs to star the message - // - // Your code should detect when the user adds a heart reaction and then - // automatically star that message in the chat - // - // TIP: You can inspect WhatsApp modules directly in your Chrome console by typing: - // require("WAWebCmd") or any other module name. This will show you available methods, - // _events, and other properties that might be helpful for this task. + clearInterval(storeCheckInterval); + console.log("WhatsApp store initialized"); + setupReactionListener(); + } catch (error) { + console.error("Error initializing WhatsApp store:", error); + } + } + }, 1000); + } - console.log("Reaction listener needs to be implemented"); - } + // Set up listeners for heart reactions + function setupReactionListener() { + // + // CANDIDATE TASK: Write code that will star a message when you react to it with a heart emoji ✅ + // + // Hints: + // 1. You'll need to listen for reaction events using Store.Reactions + // 2. Check if the reaction is a heart emoji (❤️) + // 3. Use Store.Msg to get the message object + // 4. Use Store.Chat to get the chat object + // 5. Use Store.Cmd.sendStarMsgs to star the message + // + // Your code should detect when the user adds a heart reaction and then + // automatically star that message in the chat + // + // TIP: You can inspect WhatsApp modules directly in your Chrome console by typing: + // require("WAWebCmd") or any other module name. This will show you available methods, + // _events, and other properties that might be helpful for this task. - // Initialize - initializeWAStore(); + Store.Reactions._models.forEach((model) => { + model.reactions._events.add = [ + ...model.reactions._events.add, + { + callback: async (...args) => { + if (args[0].__x_aggregateEmoji === EMOJI_TARGET) { + const msgKey = args[1].parent.__x_id._serialized; + const msg = Store.Msg.get(msgKey); + const chat = await Store.Chat.find(msg.id.remote); + const messageHasStar = msg.__x_star; + + if (!messageHasStar) { + Store.Cmd.sendStarMsgs(chat, [msg], false); + } else { + Store.Cmd.sendUnstarMsgs(chat, [msg], false); + } + } + }, + }, + ]; + }); + } + + // Initialize + initializeWAStore(); })();