diff --git a/source/browser.ts b/source/browser.ts index 6a0647622..45e4646ae 100644 --- a/source/browser.ts +++ b/source/browser.ts @@ -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, @@ -51,7 +52,7 @@ async function withSettingsMenu(callback: () => Promise | void): Promise( - `.uiLayer:not(.hidden_elem) ._54nq._2i-c._558b._2n_z li:nth-child(${itemNumber}) a` + `${conversationMenuSelector} > li:nth-child(${itemNumber}) a` ); if (selector) { @@ -483,7 +484,7 @@ function setZoom(zoomFactor: number): void { async function withConversationMenu(callback: () => void): Promise { const menuButton = document.querySelector( - `${selectedConversationSelector} ._5blh._4-0h` + `${selectedConversationSelector} [aria-haspopup=true] [role=button]` ); if (menuButton) { @@ -497,25 +498,29 @@ async function openMuteModal(): Promise { }); } -async function hideSelectedConversation(): Promise { - const groupConversationProfilePicture = document.querySelector( - `${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( + `${conversationMenuSelector} > li:nth-child(6)[role=separator]` ); - const isGroupConversation = Boolean(groupConversationProfilePicture); + return Boolean(separator); +} +async function hideSelectedConversation(): Promise { await withConversationMenu(() => { - selectMenuItem(isGroupConversation ? 4 : 3); + selectMenuItem(isSelectedConversationGroup() ? 4 : 3); }); } async function deleteSelectedConversation(): Promise { - const groupConversationProfilePicture = document.querySelector( - `${selectedConversationSelector} ._55lu` - ); - const isGroupConversation = Boolean(groupConversationProfilePicture); - await withConversationMenu(() => { - selectMenuItem(isGroupConversation ? 5 : 4); + selectMenuItem(isSelectedConversationGroup() ? 5 : 4); }); }