From e463081a2e68c37ee041b3bb62b2516c83bb83ca Mon Sep 17 00:00:00 2001 From: Tomer <11458759+tomer8007@users.noreply.github.com> Date: Fri, 1 Jul 2022 19:11:38 +0300 Subject: [PATCH] Disabled logs hooking to avoid InvalidStateError in IndexedDB --- core/interception.js | 35 ++++++++++------------------------- core/ui.js | 17 ++++++++++------- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/core/interception.js b/core/interception.js index 58fa33f..21c580a 100644 --- a/core/interception.js +++ b/core/interception.js @@ -24,24 +24,15 @@ initialize(); // wsHook.before = function (originalData, url) { - var isMultiDevice = !WACrypto.isTagBasedPayload(originalData); - var promise = async function(originalData) { if (WAPassthrough) return originalData; try { - var data = originalData; - var isMultiDevice = !WACrypto.isTagBasedPayload(data); + var isMultiDevice = !WACrypto.isTagBasedPayload(originalData); - var tag = ""; - if (!isMultiDevice) - { - payload = WACrypto.parseWebSocketPayload(data); - data = payload.data; - tag = payload.tag; - } + var {tag, data} = isMultiDevice ? {tag: "", data: originalData} : WACrypto.parseWebSocketPayload(originalData); if (data instanceof ArrayBuffer || data instanceof Uint8Array) { @@ -111,6 +102,8 @@ wsHook.before = function (originalData, url) }; + var isMultiDevice = !WACrypto.isTagBasedPayload(originalData); + return isMultiDevice ? MultiDevice.enqueuePromise(promise, originalData, false) : promise(originalData); } @@ -119,25 +112,15 @@ wsHook.before = function (originalData, url) // wsHook.after = function (messageEvent, url) { - var data = messageEvent.data; - var isMultiDevice = !WACrypto.isTagBasedPayload(data); - var promise = async function(messageEvent) { - var data = messageEvent.data; - var isMultiDevice = !WACrypto.isTagBasedPayload(data); - if (WAPassthrough) return messageEvent; try { - var tag = ""; - if (!isMultiDevice) - { - payload = WACrypto.parseWebSocketPayload(data); - tag = payload.tag; - data = payload.data; - } + var isMultiDevice = !WACrypto.isTagBasedPayload(messageEvent.data); + + var {tag, data} = isMultiDevice ? {tag: "", data: messageEvent.data} : WACrypto.parseWebSocketPayload(messageEvent.data); if (data instanceof ArrayBuffer || data instanceof Uint8Array) { @@ -205,6 +188,8 @@ wsHook.after = function (messageEvent, url) }; + var isMultiDevice = !WACrypto.isTagBasedPayload(messageEvent.data); + return isMultiDevice ? MultiDevice.enqueuePromise(promise, messageEvent, true) : promise(messageEvent); } @@ -562,7 +547,7 @@ function exposeWhatsAppAPI() function initialize() { - hookLogs(); + //hookLogs(); initializeDeletedMessagesDB(); } diff --git a/core/ui.js b/core/ui.js index 401a714..67b269f 100644 --- a/core/ui.js +++ b/core/ui.js @@ -116,6 +116,7 @@ function onMainUIReady() setTimeout(checkInterception, 1000); // if the menu item is gone somehow after a short period of time (e.g because the layout changes from right-to-left) add it again + // TODO: a race can make the icon added twice setTimeout(addIconIfNeeded, 1000); } @@ -328,19 +329,21 @@ document.addEventListener('onInterceptionWorking', function (e) isInterceptionWorking = data.isInterceptionWorking; isMultiDevice = data.isMultiDevice; - var deletedDB = indexedDB.open("deletedMsgs", 1); - deletedDB.onsuccess = () => { - var keys = deletedDB.result.transaction('msgs', "readonly") - .objectStore("msgs").getAll(); + // populate pseudoMsgsIDs + var deletedDBOpenRequest = indexedDB.open("deletedMsgs", 1); + deletedDBOpenRequest.onsuccess = () => + { + var deletedMsgsDB = deletedDBOpenRequest.result; + var keys = deletedMsgsDB.transaction('msgs', "readonly").objectStore("msgs").getAll(); keys.onsuccess = () => { - keys.result.forEach((v) => { - pseudoMsgsIDs.add(v.originalID); + keys.result.forEach((value) => { + pseudoMsgsIDs.add(value.originalID); }); document.addEventListener("pseudoMsgs", (e) => { pseudoMsgsIDs.add(e.detail); }); }; - deletedDB.result.close(); + deletedMsgsDB.close(); }; });