From a02e22671d265d51a5c7678ea0fccb8a17dd1611 Mon Sep 17 00:00:00 2001 From: Daniel Andrei Date: Tue, 2 Jul 2019 00:24:13 +0300 Subject: [PATCH] Update selector --- source/browser.ts | 52 ++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/source/browser.ts b/source/browser.ts index f32ce701b..b9d6c6dd5 100644 --- a/source/browser.ts +++ b/source/browser.ts @@ -10,7 +10,7 @@ import './browser/conversation-list'; // eslint-disable-line import/no-unassigne const selectedConversationSelector = '._5l-3._1ht1._1ht2'; const preferencesSelector = '._10._4ebx.uiLayer._4-hy'; -const messengerSoundsSelector = `${preferencesSelector} ._374d input`; +const messengerSoundsSelector = `${preferencesSelector} ._374d ._6bkz`; async function withMenu( menuButtonElement: HTMLElement, @@ -51,7 +51,10 @@ function selectMenuItem(itemNumber: number): void { const selector = document.querySelector( `.uiLayer:not(.hidden_elem) ._54nq._2i-c._558b._2n_z li:nth-child(${itemNumber}) a` )!; - selector.click(); + + if (selector) { + selector.click(); + } } async function selectOtherListViews(itemNumber: number): Promise { @@ -156,12 +159,8 @@ function setSidebarVisibility(): void { ipc.send('set-sidebar-visibility'); } -ipc.on('toggle-sounds', toggleSounds); - -ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus: boolean) => { - const preferencesAreOpen = isPreferencesOpen(); - - if (!preferencesAreOpen) { +async function openHiddenPreferences(): Promise { + if (!isPreferencesOpen()) { const style = document.createElement('style'); // Hide both the backdrop and the preferences dialog style.textContent = `${preferencesSelector} ._3ixn, ${preferencesSelector} ._59s7 { opacity: 0 !important }`; @@ -171,7 +170,28 @@ ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus: // Will clean up itself after the preferences are closed document.querySelector(preferencesSelector)!.append(style); + + return true; + } + + return false; +} + +ipc.on('toggle-sounds', async (_event: ElectronEvent, checked: boolean): Promise => { + const shouldClosePreferences = await openHiddenPreferences(); + + const soundsCheckbox = document.querySelector(messengerSoundsSelector)!; + if (typeof checked === 'undefined' || checked !== soundsCheckbox.checked) { + soundsCheckbox.click(); + } + + if (shouldClosePreferences) { + closePreferences(); } +}); + +ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus: boolean) => { + const shouldClosePreferences = await openHiddenPreferences(); const notificationCheckbox = document.querySelector( '._374b:nth-of-type(4) ._4ng2 input' @@ -188,7 +208,7 @@ ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus: ipc.send('mute-notifications-toggled', !notificationCheckbox.checked); - if (!preferencesAreOpen) { + if (shouldClosePreferences) { closePreferences(); } }); @@ -430,20 +450,6 @@ function closePreferences(): void { doneButton.click(); } -async function toggleSounds(_event: ElectronEvent, checked: boolean): Promise { - if (isPreferencesOpen()) { - return; - } - - await openPreferences(); - const soundsCheckbox = document.querySelector(messengerSoundsSelector)!; - if (typeof checked === 'undefined' || checked !== soundsCheckbox.checked) { - soundsCheckbox.click(); - } - - closePreferences(); -} - async function insertionListener(event: AnimationEvent): Promise { if (event.animationName === 'nodeInserted' && event.target) { event.target.dispatchEvent(new Event('mouseover', {bubbles: true}));