44 heartbeatAlarmListener ,
55 sendInitialHeartbeat ,
66 tabActivatedListener ,
7+ setupMessageListener ,
78} from './heartbeat'
89import { getClient , detectHostname } from './client'
910import {
@@ -24,7 +25,7 @@ async function getIsConsentRequired() {
2425 . catch ( ( ) => true )
2526}
2627
27- async function autodetectHostname ( ) {
28+ async function autodetectHostname ( client : any ) {
2829 const hostname = await getHostname ( )
2930 if ( hostname === undefined ) {
3031 const detectedHostname = await detectHostname ( client )
@@ -34,72 +35,55 @@ async function autodetectHostname() {
3435 }
3536}
3637
37- /** Init */
38- console . info ( 'Starting...' )
38+ async function start ( ) {
39+ console . info ( 'Starting...' )
3940
40- console . debug ( 'Creating client' )
41- const client = getClient ( )
41+ const client = getClient ( )
4242
43- browser . runtime . onInstalled . addListener ( async ( ) => {
44- const { consent } = await getConsentStatus ( )
45- const isConsentRequired = await getIsConsentRequired ( )
46- if ( ! isConsentRequired || consent ) {
47- if ( ! isConsentRequired ) console . info ( 'Consent is not required' )
48- else if ( consent ) console . info ( 'Consent required but already accepted' )
49- console . debug ( 'Enabling the extension' )
50- await setEnabled ( true )
51- } else {
52- console . info ( 'Consent is required...opening consent tab' )
53- await setConsentStatus ( { consent, required : true } )
54- await browser . tabs . create ( {
55- active : true ,
56- url : browser . runtime . getURL ( 'src/consent/index.html' ) ,
57- } )
58- }
59-
60- await autodetectHostname ( )
61- } )
43+ browser . runtime . onInstalled . addListener ( async ( ) => {
44+ const { consent } = await getConsentStatus ( )
45+ const isConsentRequired = await getIsConsentRequired ( )
46+ if ( ! isConsentRequired || consent ) {
47+ if ( ! isConsentRequired ) console . info ( 'Consent is not required' )
48+ else if ( consent ) console . info ( 'Consent required but already accepted' )
49+ console . debug ( 'Enabling the extension' )
50+ await setEnabled ( true )
51+ } else {
52+ console . info ( 'Consent is required...opening consent tab' )
53+ await setConsentStatus ( { consent, required : true } )
54+ await browser . tabs . create ( {
55+ active : true ,
56+ url : browser . runtime . getURL ( 'src/consent/index.html' ) ,
57+ } )
58+ }
6259
63- console . debug ( 'Creating alarms and tab listeners' )
64- browser . alarms . create ( config . heartbeat . alarmName , {
65- periodInMinutes : Math . floor ( config . heartbeat . intervalInSeconds / 60 ) ,
66- } )
67- browser . alarms . onAlarm . addListener ( heartbeatAlarmListener ( client ) )
68- browser . tabs . onActivated . addListener ( tabActivatedListener ( client ) )
60+ await autodetectHostname ( client )
61+ } )
6962
70- console . debug ( 'Setting base url' )
71- setBaseUrl ( client . baseURL )
72- . then ( ( ) =>
73- console . debug ( 'Waiting for enable before sending initial heartbeat' ) ,
74- )
75- . then ( waitForEnabled )
76- . then ( ( ) => sendInitialHeartbeat ( client ) )
77- . then ( ( ) => console . info ( 'Started successfully' ) )
63+ console . debug ( 'Creating alarms and tab listeners' )
64+ browser . alarms . create ( config . heartbeat . alarmName , {
65+ periodInMinutes : Math . floor ( config . heartbeat . intervalInSeconds / 60 ) ,
66+ } )
67+
68+ // Set up listeners
69+ setupMessageListener ( client )
70+ browser . alarms . onAlarm . addListener ( heartbeatAlarmListener ( client ) )
71+ browser . tabs . onActivated . addListener ( tabActivatedListener ( client ) )
7872
79- /**
80- * Keep the service worker alive to prevent Chrome's 5-minute inactivity termination
81- * This is a workaround for Chrome's behavior of terminating inactive service workers
82- * https://stackoverflow.com/questions/66618136
83- */
84- if ( import . meta. env . VITE_TARGET_BROWSER === 'chrome' ) {
85- function setupKeepAlive ( ) : void {
86- console . debug (
87- 'Setting up keep-alive ping to prevent service worker termination' ,
73+ console . debug ( 'Setting base url' )
74+ setBaseUrl ( client . baseURL )
75+ . then ( ( ) =>
76+ console . debug ( 'Waiting for enable before sending initial heartbeat' ) ,
8877 )
78+ . then ( waitForEnabled )
79+ . then ( ( ) => sendInitialHeartbeat ( client ) )
80+ . then ( ( ) => console . info ( 'Started successfully' ) )
8981
90- setInterval (
91- ( ) => {
92- console . debug ( 'Keep-alive ping' )
93- // Force some minimal activity
94- browser . alarms
95- . get ( config . heartbeat . alarmName )
96- . then ( ( ) => console . debug ( 'Keep-alive ping completed' ) )
97- . catch ( ( err ) => console . error ( 'Keep-alive ping failed:' , err ) )
98- } ,
99- 4 * 60 * 1000 ,
100- ) // 4 minutes (less than Chrome's ~5 minute timeout)
82+ if ( import . meta. env . VITE_TARGET_BROWSER === 'chrome' ) {
83+ setInterval ( ( ) => {
84+ browser . alarms . get ( config . heartbeat . alarmName ) . catch ( ( ) => { } )
85+ } , 4 * 60 * 1000 )
10186 }
102-
103- // Start the keep-alive mechanism
104- setupKeepAlive ( )
10587}
88+
89+ start ( )
0 commit comments