@@ -12,24 +12,54 @@ let activeTab;
1212const tabsObj = { } ;
1313// Will store Chrome web vital metrics and their corresponding values.
1414const metrics = { } ;
15+
16+ //keep alive functionality to address port disconnection issues
1517function setupKeepAlive ( ) {
16- //ellie
17- // Create an alarm that triggers every 4.9 minutes (under the 5-minute limit)
18+ // Clear any existing keep-alive alarms to prevent duplicates
19+ chrome . alarms . clear ( 'keepAlive' , ( wasCleared ) => {
20+ if ( wasCleared ) {
21+ console . log ( 'Cleared existing keep-alive alarm.' ) ;
22+ }
23+ } ) ;
24+
25+ // Create a new keep-alive alarm, we found .5 min to resolve the idle time port disconnection
1826 chrome . alarms . create ( 'keepAlive' , { periodInMinutes : 0.5 } ) ;
1927
28+ // Log active alarms for debugging
29+ chrome . alarms . getAll ( ( alarms ) => {
30+ console . log (
31+ 'Active alarms:' ,
32+ alarms . map ( ( alarm ) => alarm . name ) ,
33+ ) ;
34+ } ) ;
35+
36+ // Listen for the keep-alive alarm
2037 chrome . alarms . onAlarm . addListener ( ( alarm ) => {
2138 if ( alarm . name === 'keepAlive' ) {
2239 console . log ( 'Keep-alive alarm triggered.' ) ;
2340 pingServiceWorker ( ) ;
2441 }
2542 } ) ;
2643}
44+
2745// Ping the service worker to keep it alive
2846function pingServiceWorker ( ) {
29- // Use a lightweight API call to keep the service worker active
30- chrome . runtime . getPlatformInfo ( ( ) => {
31- console . log ( 'Service worker pinged successfully' ) ;
32- } ) ;
47+ try {
48+ chrome . runtime . getPlatformInfo ( ( ) => {
49+ console . log ( 'Service worker pinged successfully.' ) ;
50+ } ) ;
51+ } catch ( error ) {
52+ console . error ( 'Failed to ping service worker:' , error ) ;
53+
54+ // Fallback: Trigger an empty event to wake up the service worker
55+ chrome . runtime . sendMessage ( { type : 'ping' } , ( response ) => {
56+ if ( chrome . runtime . lastError ) {
57+ console . error ( 'Fallback message failed:' , chrome . runtime . lastError . message ) ;
58+ } else {
59+ console . log ( 'Fallback message sent successfully:' , response ) ;
60+ }
61+ } ) ;
62+ }
3363}
3464
3565// function pruning the chrome ax tree and pulling the relevant properties
@@ -366,7 +396,7 @@ chrome.runtime.onConnect.addListener(async (port) => {
366396 } ) ;
367397 }
368398
369- // Handles port disconnection by removing the disconnected port -ellie
399+ // Handles port disconnection by removing the disconnected port
370400 port . onDisconnect . addListener ( ( ) => {
371401 const index = portsArr . indexOf ( port ) ;
372402 if ( index !== - 1 ) {
@@ -656,7 +686,7 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
656686 break ;
657687 }
658688
659- // DUPLICATE SNAPSHOT CHECK -ellie
689+ // DUPLICATE SNAPSHOT CHECK
660690 const isDuplicateSnapshot = ( previous , incoming ) => {
661691 if ( ! previous || ! incoming ) return false ;
662692 const prevData = previous ?. componentData ;
@@ -792,6 +822,23 @@ chrome.tabs.onActivated.addListener((info) => {
792822 } ) ;
793823} ) ;
794824
825+ // Ensure keep-alive is set up when the extension is installed
826+ chrome . runtime . onInstalled . addListener ( ( ) => {
827+ console . log ( 'Extension installed. Setting up keep-alive...' ) ;
828+ setupKeepAlive ( ) ;
829+ } ) ;
830+
831+ // Ensure keep-alive is set up when the browser starts
795832chrome . runtime . onStartup . addListener ( ( ) => {
833+ console . log ( 'Browser started. Setting up keep-alive...' ) ;
796834 setupKeepAlive ( ) ;
797835} ) ;
836+
837+ // Optional: Reset keep-alive when a message is received (to cover edge cases)
838+ chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => {
839+ if ( message === 'resetKeepAlive' ) {
840+ console . log ( 'Resetting keep-alive as requested.' ) ;
841+ setupKeepAlive ( ) ;
842+ sendResponse ( { success : true } ) ;
843+ }
844+ } ) ;
0 commit comments