@@ -16,6 +16,28 @@ let activeTab;
16
16
const tabsObj = { } ;
17
17
// Will store Chrome web vital metrics and their corresponding values.
18
18
const metrics = { } ;
19
+ function setupKeepAlive ( ) {
20
+ //ellie
21
+ // Create an alarm that triggers every 4.9 minutes (under the 5-minute limit)
22
+ chrome . alarms . create ( 'keepAlive' , { periodInMinutes : 4.9 } ) ;
23
+
24
+ chrome . alarms . onAlarm . addListener ( ( alarm ) => {
25
+ if ( alarm . name === 'keepAlive' ) {
26
+ console . log ( 'Keep-alive alarm triggered.' ) ;
27
+ pingServiceWorker ( ) ;
28
+ }
29
+ } ) ;
30
+ }
31
+ // Ping the service worker to keep it alive
32
+ function pingServiceWorker ( ) {
33
+ chrome . runtime . getBackgroundPage ( ( bgPage ) => {
34
+ if ( bgPage ) {
35
+ console . log ( 'Service worker is alive' ) ;
36
+ } else {
37
+ console . warn ( 'Failed to ping the service worker.' ) ;
38
+ }
39
+ } ) ;
40
+ }
19
41
20
42
// function pruning the chrome ax tree and pulling the relevant properties
21
43
const pruneAxTree = ( axTree ) => {
@@ -318,7 +340,18 @@ chrome.runtime.onConnect.addListener(async (port) => {
318
340
} ) ;
319
341
} ) ;
320
342
}
343
+ if ( port . name === 'keepAlivePort' ) {
344
+ console . log ( 'Keep-alive port connected:' , port ) ;
321
345
346
+ // Keep the port open by responding to any message
347
+ port . onMessage . addListener ( ( msg ) => {
348
+ console . log ( 'Received message from content script:' , msg ) ;
349
+ } ) ;
350
+
351
+ port . onDisconnect . addListener ( ( ) => {
352
+ console . warn ( 'Keep-alive port disconnected.' ) ;
353
+ } ) ;
354
+ }
322
355
if ( portsArr . length > 0 && Object . keys ( tabsObj ) . length > 0 ) {
323
356
//if the activeTab is not set during the onActivate API, run a query to get the tabId and set activeTab
324
357
if ( ! activeTab ) {
@@ -723,20 +756,15 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
723
756
}
724
757
} ) ;
725
758
726
- // when tab view is changed, put the tabid as the current tab
759
+ // When tab view is changed, put the tabId as the current tab
727
760
chrome . tabs . onActivated . addListener ( ( info ) => {
728
- // get info about tab information from tabId
761
+ // Get info about the tab information from tabId
729
762
chrome . tabs . get ( info . tabId , ( tab ) => {
730
- // never set a reactime instance to the active tab
763
+ // Never set a reactime instance to the active tab
731
764
if ( ! tab . pendingUrl ?. match ( '^chrome-extension' ) ) {
732
765
activeTab = tab ;
733
766
734
- /**this setKeepAlive is here to make sure that the app does not stop working even
735
- * if chrome pauses to save energy.
736
- */
737
- chrome . runtime . onStartup . addListener ( ( ) => {
738
- chrome . runtime . setKeepAlive ( true ) ;
739
- } ) ;
767
+ // Send messages to active ports about the tab change
740
768
if ( portsArr . length > 0 ) {
741
769
portsArr . forEach ( ( bg ) =>
742
770
bg . postMessage ( {
@@ -757,8 +785,12 @@ chrome.runtime.onInstalled.addListener(() => {
757
785
title : 'Reactime' ,
758
786
contexts : [ 'page' , 'selection' , 'image' , 'link' ] ,
759
787
} ) ;
788
+ setupKeepAlive ( ) ;
760
789
} ) ;
761
790
791
+ chrome . runtime . onStartup . addListener ( ( ) => {
792
+ setupKeepAlive ( ) ;
793
+ } ) ;
762
794
// when context menu is clicked, listen for the menuItemId,
763
795
// if user clicked on reactime, open the devtools window
764
796
0 commit comments