@@ -21,7 +21,8 @@ async function withMenu(
21
21
menuButtonElement . click ( ) ;
22
22
23
23
// Wait for the menu to close before removing the 'hide-dropdowns' class
24
- const menuLayer = document . querySelector ( '.j83agx80.cbu4d94t.l9j0dhe7.jgljxmt5.be9z9djy > div:nth-child(2) > div' ) ;
24
+ await elementReady ( '.x78zum5.xdt5ytf.x1n2onr6.xat3117.xxzkxad > div:nth-child(2) > div' , { stopOnDomReady : false } ) ;
25
+ const menuLayer = document . querySelector ( '.x78zum5.xdt5ytf.x1n2onr6.xat3117.xxzkxad > div:nth-child(2) > div' ) ;
25
26
26
27
if ( menuLayer ) {
27
28
const observer = new MutationObserver ( ( ) => {
@@ -149,45 +150,18 @@ ipc.answerMain('find', () => {
149
150
} ) ;
150
151
151
152
async function openSearchInConversation ( ) {
152
- const mainView = document . querySelector ( '.rq0escxv.l9j0dhe7.du4w35lb.j83agx80.rj1gh0hx.buofh1pr.g5gj957u.hpfvmrgz.i1fnvgqd.gs1a9yip.owycx6da.btwxx1t3.jb3vyjys.gitj76qy ' ) ! ;
153
- const rightSidebarIsClosed = Boolean ( mainView . querySelector < HTMLElement > ( 'div:only-child' ) ) ;
153
+ const mainView = document . querySelector ( '.x9f619.x1ja2u2z.x78zum5.x1n2onr6.x1r8uery.x1iyjqo2.xs83m0k.xeuugli.x1qughib.x1qjc9v5.xozqiw3.x1q0g3np.xexx8yu.x85a59c ' ) ! ;
154
+ const rightSidebarIsClosed = Boolean ( mainView . querySelector < HTMLElement > ( ':scope > div:only-child' ) ) ;
154
155
155
156
if ( rightSidebarIsClosed ) {
156
- document . documentElement . classList . add ( 'hide-r-sidebar' ) ;
157
- document . querySelector < HTMLElement > ( '.j9ispegn.pmk7jnqg.k4urcfbm.datstx6m.b5wmifdl.kr520xx4.mdpwds66.b2cqd1jy.n13yt9zj.eh67sqbx' ) ?. click ( ) ;
157
+ document . querySelector < HTMLElement > ( selectors . rightSidebarMenu ) ?. click ( ) ;
158
158
}
159
159
160
- await elementReady ( selectors . rightSidebarSegments , { stopOnDomReady : false } ) ;
161
- const segments = document . querySelectorAll < HTMLElement > ( selectors . rightSidebarSegments ) . length ;
162
- // If there are three segmetns in right sidebar (two users chat) then button index is 4
163
- // If there are not three segments (usually four, it's a group chat) then button index is 6
164
- const buttonIndex = segments === 3 ? 4 : 6 ;
165
-
166
160
await elementReady ( selectors . rightSidebarButtons , { stopOnDomReady : false } ) ;
167
161
const buttonList = document . querySelectorAll < HTMLElement > ( selectors . rightSidebarButtons ) ;
168
162
169
- if ( buttonList . length > buttonIndex ) {
170
- buttonList [ buttonIndex ] . click ( ) ;
171
- }
172
-
173
- // If right sidebar was closed when shortcut was clicked, then close it back.
174
- if ( rightSidebarIsClosed ) {
175
- document . querySelector < HTMLElement > ( '.j9ispegn.pmk7jnqg.k4urcfbm.datstx6m.b5wmifdl.kr520xx4.mdpwds66.b2cqd1jy.n13yt9zj.eh67sqbx' ) ?. click ( ) ;
176
-
177
- // Observe sidebar so when it's hidden, remove the utility class. This prevents split
178
- // display of sidebar.
179
- const sidebarObserver = new MutationObserver ( records => {
180
- const removedRecords = records . filter ( ( { removedNodes} ) => removedNodes . length > 0 && ( removedNodes [ 0 ] as HTMLElement ) . tagName === 'DIV' ) ;
181
-
182
- // In case there is a div removed, hide utility class and stop observing
183
- if ( removedRecords . length > 0 ) {
184
- document . documentElement . classList . remove ( 'hide-r-sidebar' ) ;
185
- sidebarObserver . disconnect ( ) ;
186
- }
187
- } ) ;
188
-
189
- sidebarObserver . observe ( mainView , { childList : true , subtree : true } ) ;
190
- }
163
+ // Search in conversation is the last button
164
+ buttonList [ buttonList . length - 1 ] . click ( ) ;
191
165
}
192
166
193
167
ipc . answerMain ( 'search' , ( ) => {
@@ -223,14 +197,21 @@ ipc.answerMain('mute-conversation', async () => {
223
197
} ) ;
224
198
225
199
ipc . answerMain ( 'delete-conversation' , async ( ) => {
226
- await deleteSelectedConversation ( ) ;
200
+ const index = selectedConversationIndex ( ) ;
201
+
202
+ if ( index !== - 1 ) {
203
+ await deleteSelectedConversation ( ) ;
204
+
205
+ const key = index + 1 ;
206
+ await jumpToConversation ( key ) ;
207
+ }
227
208
} ) ;
228
209
229
- ipc . answerMain ( 'hide -conversation' , async ( ) => {
210
+ ipc . answerMain ( 'archive -conversation' , async ( ) => {
230
211
const index = selectedConversationIndex ( ) ;
231
212
232
213
if ( index !== - 1 ) {
233
- await hideSelectedConversation ( ) ;
214
+ await archiveSelectedConversation ( ) ;
234
215
235
216
const key = index + 1 ;
236
217
await jumpToConversation ( key ) ;
@@ -588,7 +569,7 @@ function selectedConversationIndex(offset = 0): number {
588
569
return - 1 ;
589
570
}
590
571
591
- const newSelected = selected . parentNode ! . parentNode ! . parentNode ! as HTMLElement ;
572
+ const newSelected = selected . closest ( ` ${ selectors . conversationList } > div` ) ! ;
592
573
593
574
const list = [ ...newSelected . parentNode ! . children ] ;
594
575
const index = list . indexOf ( newSelected ) + offset ;
@@ -605,7 +586,7 @@ async function setZoom(zoomFactor: number): Promise<void> {
605
586
async function withConversationMenu ( callback : ( ) => void ) : Promise < void > {
606
587
// eslint-disable-next-line @typescript-eslint/ban-types
607
588
let menuButton : HTMLElement | null = null ;
608
- const conversation = document . querySelector < HTMLElement > ( `${ selectors . selectedConversation } ` ) ?. parentElement ?. parentElement ?. parentElement ?. parentElement ;
589
+ const conversation = document . querySelector < HTMLElement > ( selectors . selectedConversation ) ! . closest ( `${ selectors . conversationList } > div` ) ;
609
590
610
591
menuButton = conversation ?. querySelector ( '[aria-label=Menu][role=button]' ) ?? null ;
611
592
@@ -621,27 +602,53 @@ async function openMuteModal(): Promise<void> {
621
602
}
622
603
623
604
/*
624
- This function assumes :
605
+ These functions assume :
625
606
- There is a selected conversation.
626
607
- That the conversation already has its conversation menu open.
627
608
628
609
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.
629
610
*/
630
611
function isSelectedConversationGroup ( ) : boolean {
631
- return Boolean ( document . querySelector < HTMLElement > ( `${ selectors . conversationMenuSelectorNewDesign } [role=menuitem]:nth-child(4)` ) ) ;
612
+ // Individual conversations include an entry for "View Profile", which is type `a`
613
+ return ! document . querySelector < HTMLElement > ( `${ selectors . conversationMenuSelectorNewDesign } a[role=menuitem]` ) ;
632
614
}
633
615
634
- async function hideSelectedConversation ( ) : Promise < void > {
616
+ function isSelectedConversationMetaAI ( ) : boolean {
617
+ // Meta AI menu only has 1 separator of type `hr`
618
+ return ! document . querySelector < HTMLElement > ( `${ selectors . conversationMenuSelectorNewDesign } hr:nth-of-type(2)` ) ;
619
+ }
620
+
621
+ async function archiveSelectedConversation ( ) : Promise < void > {
635
622
await withConversationMenu ( ( ) => {
636
- const [ isGroup , isNotGroup ] = [ 5 , 6 ] ;
637
- selectMenuItem ( isSelectedConversationGroup ( ) ? isGroup : isNotGroup ) ;
623
+ const [ isGroup , isNotGroup , isMetaAI ] = [ - 4 , - 3 , - 2 ] ;
624
+
625
+ let archiveMenuIndex ;
626
+ if ( isSelectedConversationMetaAI ( ) ) {
627
+ archiveMenuIndex = isMetaAI ;
628
+ } else if ( isSelectedConversationGroup ( ) ) {
629
+ archiveMenuIndex = isGroup ;
630
+ } else {
631
+ archiveMenuIndex = isNotGroup ;
632
+ }
633
+
634
+ selectMenuItem ( archiveMenuIndex ) ;
638
635
} ) ;
639
636
}
640
637
641
638
async function deleteSelectedConversation ( ) : Promise < void > {
642
639
await withConversationMenu ( ( ) => {
643
- const [ isGroup , isNotGroup ] = [ 6 , 7 ] ;
644
- selectMenuItem ( isSelectedConversationGroup ( ) ? isGroup : isNotGroup ) ;
640
+ const [ isGroup , isNotGroup , isMetaAI ] = [ - 3 , - 2 , - 1 ] ;
641
+
642
+ let deleteMenuIndex ;
643
+ if ( isSelectedConversationMetaAI ( ) ) {
644
+ deleteMenuIndex = isMetaAI ;
645
+ } else if ( isSelectedConversationGroup ( ) ) {
646
+ deleteMenuIndex = isGroup ;
647
+ } else {
648
+ deleteMenuIndex = isNotGroup ;
649
+ }
650
+
651
+ selectMenuItem ( deleteMenuIndex ) ;
645
652
} ) ;
646
653
}
647
654
0 commit comments