@@ -16,6 +16,28 @@ let activeTab;
1616const tabsObj = { } ;
1717// Will store Chrome web vital metrics and their corresponding values.
1818const 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+ }
1941
2042// function pruning the chrome ax tree and pulling the relevant properties
2143const pruneAxTree = ( axTree ) => {
@@ -318,7 +340,18 @@ chrome.runtime.onConnect.addListener(async (port) => {
318340 } ) ;
319341 } ) ;
320342 }
343+ if ( port . name === 'keepAlivePort' ) {
344+ console . log ( 'Keep-alive port connected:' , port ) ;
321345
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+ }
322355 if ( portsArr . length > 0 && Object . keys ( tabsObj ) . length > 0 ) {
323356 //if the activeTab is not set during the onActivate API, run a query to get the tabId and set activeTab
324357 if ( ! activeTab ) {
@@ -723,20 +756,15 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
723756 }
724757} ) ;
725758
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
727760chrome . tabs . onActivated . addListener ( ( info ) => {
728- // get info about tab information from tabId
761+ // Get info about the tab information from tabId
729762 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
731764 if ( ! tab . pendingUrl ?. match ( '^chrome-extension' ) ) {
732765 activeTab = tab ;
733766
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
740768 if ( portsArr . length > 0 ) {
741769 portsArr . forEach ( ( bg ) =>
742770 bg . postMessage ( {
@@ -757,8 +785,12 @@ chrome.runtime.onInstalled.addListener(() => {
757785 title : 'Reactime' ,
758786 contexts : [ 'page' , 'selection' , 'image' , 'link' ] ,
759787 } ) ;
788+ setupKeepAlive ( ) ;
760789} ) ;
761790
791+ chrome . runtime . onStartup . addListener ( ( ) => {
792+ setupKeepAlive ( ) ;
793+ } ) ;
762794// when context menu is clicked, listen for the menuItemId,
763795// if user clicked on reactime, open the devtools window
764796
0 commit comments