import { findActiveTab , sendMsg , sendMsgToActiveTab } from '@/api' ; import filterSearchData from '@/background/filter-search-data' ; import handleSelectedItem from '@/background/handle-selected-item' ; import bindOmniboxEvent from '@/background/omnibox' ; import { browser } from 'webextension-polyfill-ts' ; browser . commands . onCommand . addListener ( async ( command ) => { if ( command === 'open-in-content' ) { sendMsgToActiveTab ( { from : 'background' , to : 'content-script' , type : 'openExtension' , } ) ; } } ) ; browser . runtime . onMessage . addListener ( async ( request ) => { if ( request . type === 'select' ) { if ( request . from === 'content' ) { const { id } = await findActiveTab ( ) ; browser . tabs . sendMessage ( id as number , { from : 'background' , to : 'content-script' , type : 'closeExtension' , } ) ; } handleSelectedItem ( request . content ) ; return ; } if ( request . type === 'queryRequest' ) { const result = await filterSearchData ( request . content ) ; switch ( request . from ) { case 'popup' : sendMsg ( { from : 'background' , to : 'popup' , type : 'queryResult' , content : result , } ) ; break ; case 'content' : sendMsgToActiveTab ( { from : 'background' , to : 'content' , type : 'queryResult' , content : result , } ) ; break ; default : break ; } return ; } } ) ; bindOmniboxEvent ( ) ;