@@ -302,19 +302,38 @@ export class InputController extends EventEmitter {
302302 preeditEmpty : boolean ;
303303 setComposition ( param : chrome . input . ime . CompositionParameters ) : Promise < void > {
304304 return new Promise ( ( res , rej ) => {
305+ // Check if param and param.text are valid
306+ if ( ! param || ( typeof param . text !== 'string' ) ) {
307+ rej ( new Error ( 'Invalid param or param.text is not a string' ) ) ;
308+ return ;
309+ }
310+ // Replace all spaces in param.text with underscore
311+ // Using spaces may cause problems with Chinese character input within some Linux applications (such as QQ).
312+ param . text = param . text . replace ( / \s + / g, '_' ) ;
305313 if ( param . text . length == 0 && this . preeditEmpty ) {
306- // If preedit is already empty, and new preedit is also empty, then do not call
314+ // If preedit is already empty, and new preedit is also empty, then do not call
307315 // setComposition. This will mostly happen in ASCII mode (e.g. input method is
308316 // switched off by pressing Shift). If we still call setComposition in this case,
309317 // Chrome omnibar autofill text will disappear, resulting in bad user experience
310318 res ( null ) ;
311319 } else {
312320 this . preeditEmpty = param . text . length == 0 ;
313- chrome . input . ime . setComposition ( param , ( ok ) => ok ? res ( null ) : rej ( ) ) ;
321+ try {
322+ chrome . input . ime . setComposition ( param , ( ok ) => {
323+ if ( chrome . runtime . lastError ) {
324+ rej ( new Error ( chrome . runtime . lastError . message ) ) ;
325+ } else {
326+ ok ? res ( null ) : rej ( ) ;
327+ }
328+ } ) ;
329+ } catch ( err ) {
330+ rej ( err ) ;
331+ }
314332 }
315- } )
333+ } ) ;
316334 }
317335
336+
318337 sendCandidatesToInputView ( candidates : Array < { candidate : string , ix : number } > ) {
319338 this . emit ( "candidatesBack" , candidates ) ;
320339 }
0 commit comments