import { RequestType } from './common' ; import { browser } from 'webextension-polyfill-ts' ; import LRU from 'lru-cache' ; declare const DEV ; declare const SERVER_URL ; const serverCall = async ( args ) : Promise < any > => { const response = await ( await fetch ( SERVER_URL , { method : 'POST' , body : JSON . stringify ( args ) , } ) ) . json ( ) ; if ( 'error' in response ) throw new Error ( response . error ) ; else return response . data ; } ; const handlers = { serverCall : async ( request ) => serverCall ( request . args ) , } ; const unrecognized = async ( ) => { throw new Error ( 'unrecognized background script command' ) ; } ; async function serverRouter ( ) { browser . runtime . onMessage . addListener ( async ( request , sender ) => await ( handlers [ request . kind ] ?? unrecognized ) ( request , sender ) ) ; } const onInstalled = async ( ) => { browser . runtime . onInstalled . addListener ( async ( object ) => { if ( object . reason === 'install' && ! DEV ) { await browser . tabs . create ( { url : 'https://codewing.dev/installed_extension' } ) ; await browser . runtime . setUninstallURL ( 'https://codewing.dev/uninstalled_extension' ) ; } } ) ; } ; const background = async ( ) => { await onInstalled ( ) ; await serverRouter ( ) ; } ; background ( ) ;