Skip to content

Commit

Permalink
Update selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Andrei committed Jul 1, 2019
1 parent 3182b84 commit a02e226
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions source/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -51,7 +51,10 @@ function selectMenuItem(itemNumber: number): void {
const selector = document.querySelector<HTMLElement>(
`.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<void> {
Expand Down Expand Up @@ -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<boolean> {
if (!isPreferencesOpen()) {
const style = document.createElement('style');
// Hide both the backdrop and the preferences dialog
style.textContent = `${preferencesSelector} ._3ixn, ${preferencesSelector} ._59s7 { opacity: 0 !important }`;
Expand All @@ -171,7 +170,28 @@ ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus:

// Will clean up itself after the preferences are closed
document.querySelector<HTMLElement>(preferencesSelector)!.append(style);

return true;
}

return false;
}

ipc.on('toggle-sounds', async (_event: ElectronEvent, checked: boolean): Promise<void> => {
const shouldClosePreferences = await openHiddenPreferences();

const soundsCheckbox = document.querySelector<HTMLInputElement>(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<HTMLInputElement>(
'._374b:nth-of-type(4) ._4ng2 input'
Expand All @@ -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();
}
});
Expand Down Expand Up @@ -430,20 +450,6 @@ function closePreferences(): void {
doneButton.click();
}

async function toggleSounds(_event: ElectronEvent, checked: boolean): Promise<void> {
if (isPreferencesOpen()) {
return;
}

await openPreferences();
const soundsCheckbox = document.querySelector<HTMLInputElement>(messengerSoundsSelector)!;
if (typeof checked === 'undefined' || checked !== soundsCheckbox.checked) {
soundsCheckbox.click();
}

closePreferences();
}

async function insertionListener(event: AnimationEvent): Promise<void> {
if (event.animationName === 'nodeInserted' && event.target) {
event.target.dispatchEvent(new Event('mouseover', {bubbles: true}));
Expand Down

0 comments on commit a02e226

Please sign in to comment.