Skip to content

Commit

Permalink
Fix “Hide conversation” menu item (#1202)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
yatharth and sindresorhus committed Jan 16, 2020
1 parent 602c8d1 commit 0b92e74
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions source/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {createConversationList} from './browser/conversation-list';
const selectedConversationSelector = '._5l-3._1ht1._1ht2';
const preferencesSelector = '._10._4ebx.uiLayer._4-hy';
const messengerSoundsSelector = `${preferencesSelector} ._374d ._6bkz`;
const conversationMenuSelector = '.uiLayer:not(.hidden_elem) [role=menu]';

async function withMenu(
menuButtonElement: HTMLElement,
Expand Down Expand Up @@ -51,7 +52,7 @@ async function withSettingsMenu(callback: () => Promise<void> | void): Promise<v

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`
`${conversationMenuSelector} > li:nth-child(${itemNumber}) a`
);

if (selector) {
Expand Down Expand Up @@ -483,7 +484,7 @@ function setZoom(zoomFactor: number): void {

async function withConversationMenu(callback: () => void): Promise<void> {
const menuButton = document.querySelector<HTMLElement>(
`${selectedConversationSelector} ._5blh._4-0h`
`${selectedConversationSelector} [aria-haspopup=true] [role=button]`
);

if (menuButton) {
Expand All @@ -497,25 +498,29 @@ async function openMuteModal(): Promise<void> {
});
}

async function hideSelectedConversation(): Promise<void> {
const groupConversationProfilePicture = document.querySelector<HTMLElement>(
`${selectedConversationSelector} ._55lu`
/*
This function assumes:
- There is a selected conversation.
- That the conversation already has its conversation menu open.
In other words, you should only use this function within a callback that is provided to `withConversationMenu()`, because `withConversationMenu()` makes sure to have the conversation menu open before executing the callback and closes the conversation menu afterwards.
*/
function isSelectedConversationGroup(): boolean {
const separator = document.querySelector<HTMLElement>(
`${conversationMenuSelector} > li:nth-child(6)[role=separator]`
);
const isGroupConversation = Boolean(groupConversationProfilePicture);
return Boolean(separator);
}

async function hideSelectedConversation(): Promise<void> {
await withConversationMenu(() => {
selectMenuItem(isGroupConversation ? 4 : 3);
selectMenuItem(isSelectedConversationGroup() ? 4 : 3);
});
}

async function deleteSelectedConversation(): Promise<void> {
const groupConversationProfilePicture = document.querySelector<HTMLElement>(
`${selectedConversationSelector} ._55lu`
);
const isGroupConversation = Boolean(groupConversationProfilePicture);

await withConversationMenu(() => {
selectMenuItem(isGroupConversation ? 5 : 4);
selectMenuItem(isSelectedConversationGroup() ? 5 : 4);
});
}

Expand Down

0 comments on commit 0b92e74

Please sign in to comment.