From f9729f5648fdf70df8ed858e7a0ba37fa8484df1 Mon Sep 17 00:00:00 2001 From: Daffa Mumtaz Date: Sun, 3 Sep 2023 15:22:37 +0700 Subject: [PATCH] fix: dashboard icon inject and refactor some init code --- src/Renderer/App.js | 21 +++++++++++++++------ src/Renderer/AppLock.js | 3 +++ src/Renderer/Deferred.js | 8 ++++++++ src/Renderer/Instance.js | 3 ++- 4 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 src/Renderer/Deferred.js diff --git a/src/Renderer/App.js b/src/Renderer/App.js index 0ca04cb..f439edf 100644 --- a/src/Renderer/App.js +++ b/src/Renderer/App.js @@ -3,6 +3,7 @@ const Settings = require('./Settings'); const Instance = require('./Instance'); const AppLock = require('./AppLock'); const { whatsappReady } = require('./whatsapp-inject'); +const Deferred = require('./Deferred'); class App { constructor() { @@ -10,7 +11,6 @@ class App { this.initialized = false; this.awaitApp(); ipcRenderer.on('renderTray', () => this.renderTray()); - ipcRenderer.on('ready', () => this.init()); ipcRenderer.on('setFullWidth', (e, status) => this.setFullWidth(status)); window.WALC = { @@ -19,12 +19,12 @@ class App { }; } - awaitApp() { + async awaitApp() { + const pageReady = new Deferred(); const observer = new MutationObserver(() => { - const sidebar = document.querySelector('#app [data-testid=chatlist-header]'); - if(sidebar) { - setTimeout(() => this.init(), 1000); - observer.disconnect(); + const isReady = document.querySelector('#app [data-icon=\'chat\']'); + if(isReady) { + setTimeout(() => pageReady.resolve(), 2000); } }); @@ -34,6 +34,15 @@ class App { subtree: true, }); }, 2000); + + await Promise.race([ + pageReady.promise, + new Promise((resolve) => { + ipcRenderer.once('ready', () => resolve()); + }), + ]); + this.init(); + observer.disconnect(); } async init() { diff --git a/src/Renderer/AppLock.js b/src/Renderer/AppLock.js index f8d9a5d..82cc239 100644 --- a/src/Renderer/AppLock.js +++ b/src/Renderer/AppLock.js @@ -59,6 +59,9 @@ class AppLock { } async _initWhatsapp() { + if (!window.Store) { + return; + } const user = window.Store.User.getMeUser() || {}; const myContact = await window.Store.Contact.find(user._serialized); this.overlay.querySelector('.app-lock-user h1').textContent = myContact.displayName; diff --git a/src/Renderer/Deferred.js b/src/Renderer/Deferred.js new file mode 100644 index 0000000..c53a1ee --- /dev/null +++ b/src/Renderer/Deferred.js @@ -0,0 +1,8 @@ +module.exports = class Deferred { + constructor() { + this.promise = new Promise((resolve, reject)=> { + this.reject = reject + this.resolve = resolve + }) + } +} diff --git a/src/Renderer/Instance.js b/src/Renderer/Instance.js index 87afb15..556af1f 100644 --- a/src/Renderer/Instance.js +++ b/src/Renderer/Instance.js @@ -34,7 +34,8 @@ class Instance { } installDashboardIcon(icon) { - const container = document.querySelector('[data-testid=chatlist-header] div:first-child'); + const profilePhoto = document.querySelector('[aria-label="profile photo"]'); + const container = profilePhoto.parentElement; this.image = document.createElement('div'); container.style.display = 'flex'; container.style.alignItems = 'center';