import filterSearchData from '@/background/filter-search-data' ; import handleSelectedItem from '@/background/handle-selected-item' ; import { encodeXml } from '@/common/utils' ; import { browser } from 'webextension-polyfill-ts' ; const searchResults = [ ] ; function setSuggestion ( describe ) { example browser . omnibox . setDefaultSuggestion ( { description : describe , } ) ; } function bindOmniboxEvent ( ) { browser . omnibox . onInputStarted . addListener ( ( ) => { setSuggestion ( '<match>My lord</match> <dim>...</dim>' ) ; } ) ; browser . omnibox . onInputChanged . addListener ( async ( text , suggest ) => { if ( ! text ) { return ; } const result = await filterSearchData ( text ) ; const temp = [ ] ; const regex = new RegExp ( `${ text . replace ( /\s+/g , '|' ) }` , 'gi' ) ; searchResults . length = 0 ; result . some ( item => { if ( item . type !== 'keyword' ) { temp . push ( { content : `${ item . title } @index=${ temp . length + 1 }` , description : `<match>${ item . type }</match>: ${ `${ encodeXml ( item . title as string ) } - <url>${ encodeXml ( item . subtitle as string ) }</url>` . replace ( regex , '<match>$&</match>' ) }` , } ) ; searchResults . push ( item ) ; return temp . length > 4 ; } return false ; } ) ; suggest ( temp ) ; } ) ; browser . omnibox . onInputEntered . addListener ( ( ... args ) => { param str const index = + args [ 0 ] . split ( '@index=' ) . slice ( - 1 ) ; handleSelectedItem ( searchResults [ index - 1 ] ) ; } ) ; } export default bindOmniboxEvent ;