@@ -37,19 +37,27 @@ try {
3737/**
3838 * Find Discord's X11 window ID.
3939 * Returns the window ID string, or null if Discord is not running / not found.
40+ *
41+ * Strategy: search by window NAME "Discord" first. Discord's main application
42+ * window has a title of "Discord" (or "Discord - #channel"), while the GPU
43+ * process, renderer sub-windows, and utility windows have empty or internal
44+ * titles that do NOT contain "Discord". Taking the first (oldest) result from
45+ * the name search reliably returns the main window across all button presses,
46+ * even after Discord briefly gains and loses focus (which can cause it to
47+ * create additional sub-windows, making a last-ID strategy unreliable).
4048 */
4149function getDiscordWindowId ( ) {
42- // Try by window class first (most reliable)
43- const byClass = spawnSync ( 'xdotool' , [ 'search' , '--class' , 'discord' ] , { stdio : 'pipe' } )
44- if ( byClass . status === 0 ) {
45- const ids = byClass . stdout . toString ( ) . trim ( ) . split ( '\n' ) . filter ( Boolean )
46- if ( ids . length > 0 ) return ids [ ids . length - 1 ]
47- }
48- // Fallback: search by window title
50+ // Primary: name search — matches only the main Discord application window
4951 const byName = spawnSync ( 'xdotool' , [ 'search' , '--name' , 'Discord' ] , { stdio : 'pipe' } )
5052 if ( byName . status === 0 ) {
5153 const ids = byName . stdout . toString ( ) . trim ( ) . split ( '\n' ) . filter ( Boolean )
52- if ( ids . length > 0 ) return ids [ ids . length - 1 ]
54+ if ( ids . length > 0 ) return ids [ 0 ]
55+ }
56+ // Fallback: class search — take the first (oldest/main) window
57+ const byClass = spawnSync ( 'xdotool' , [ 'search' , '--class' , 'discord' ] , { stdio : 'pipe' } )
58+ if ( byClass . status === 0 ) {
59+ const ids = byClass . stdout . toString ( ) . trim ( ) . split ( '\n' ) . filter ( Boolean )
60+ if ( ids . length > 0 ) return ids [ 0 ]
5361 }
5462 console . warn ( `[${ pluginUUID } ] Could not find Discord window — sending key to focused window instead` )
5563 return null
0 commit comments