import { isEachEligible } from '@/common/utils' ; import { browser } from 'webextension-polyfill-ts' ; /**
 * To get some bookmark that meets the requirements
 */ async function queryBM ( queryQueue ) { /**
     * Multiple queries only need to be deduplicated on the results of the first query,
     * without having to query the second pass
     */ const len = queryQueue . length ; if ( ! len ) { return [ ] ; } const result = await browser . bookmarks . search ( queryQueue [ 0 ] ) ; const temp = [ ] ; for ( const item of result ) { if ( item . dateGroupModified ) { break ; } if ( len === 1 || isEachEligible ( queryQueue , `${ item . url } ${ item . title }` ) ) { temp . push ( { type : 'bookmark' , title : item . title || item . url , subtitle : item . url , id : item . id , } ) ; } } return temp ; } /**
 * To get some tab that meets the requirements
 */ async function queryTab ( queryQueue ) { const result = await browser . tabs . query ( { windowId : browser . windows . WINDOW_ID_CURRENT , windowType : 'normal' , } ) ; const temp = [ ] ; for ( const item of result ) { if ( queryQueue . length === 0 || isEachEligible ( queryQueue , `${ item . url } ${ item . title }` ) ) { temp . push ( { type : 'tab' , title : item . title , subtitle : item . url , active : item . highlighted && item . active , id : item . id as number , } ) ; } } return temp ; } link async function sendMsgToActiveTab ( data ) { try { const { id } = await findActiveTab ( ) ; await browser . tabs . sendMessage ( id as number , data ) ; } catch ( error ) { alert ( browser . i18n . getMessage ( 'contentScript_unavailable' ) ) ; } } /**
 * send message with using browser.runtime
 */ function sendMsg ( data ) { browser . runtime . sendMessage ( data ) ; } /**
 * find active tab which you are focusing
 */ async function findActiveTab ( ) { const result = await browser . tabs . query ( { active : true , currentWindow : true , } ) ; return result [ 0 ] ; } /**
 * update the specified tab status
 */ function updateTabStatus ( id , status = { highlighted : true , active : true , } ) { browser . tabs . update ( id , status ) ; } /**
 * open a new tab with the specified url
 */ function openNewTab ( url ) { browser . tabs . create ( { url } ) ; } /**
 * query recently cloed tab
 */ async function queryRecentLyClosed ( queryQueue ) { const result = await browser . sessions . getRecentlyClosed ( { maxResults : 25 } ) ; const temp = [ ] ; for ( const item of result ) { if ( ! item . tab ) { break ; } if ( queryQueue . length === 0 || isEachEligible ( queryQueue , `${ item . tab . url } ${ item . tab . title }` ) ) { temp . push ( { type : 'RCT' , title : item . tab . title , subtitle : item . tab . url , id : item . tab . sessionId as string , } ) ; } } return temp ; } /**
 * reopen the tab which recently closed
 */ function restoreRecentTab ( sessionId ) { browser . sessions . restore ( sessionId ) ; } export { findActiveTab , openNewTab , queryBM , queryRecentLyClosed , queryTab , restoreRecentTab , sendMsg , sendMsgToActiveTab , updateTabStatus , } ;