Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix “Hide conversation” menu item #1202

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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