@@ -12,24 +12,54 @@ let activeTab;
12
12
const tabsObj = { } ;
13
13
// Will store Chrome web vital metrics and their corresponding values.
14
14
const metrics = { } ;
15
+
16
+ //keep alive functionality to address port disconnection issues
15
17
function 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
18
26
chrome . alarms . create ( 'keepAlive' , { periodInMinutes : 0.5 } ) ;
19
27
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
20
37
chrome . alarms . onAlarm . addListener ( ( alarm ) => {
21
38
if ( alarm . name === 'keepAlive' ) {
22
39
console . log ( 'Keep-alive alarm triggered.' ) ;
23
40
pingServiceWorker ( ) ;
24
41
}
25
42
} ) ;
26
43
}
44
+
27
45
// Ping the service worker to keep it alive
28
46
function 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
+ }
33
63
}
34
64
35
65
// function pruning the chrome ax tree and pulling the relevant properties
@@ -366,7 +396,7 @@ chrome.runtime.onConnect.addListener(async (port) => {
366
396
} ) ;
367
397
}
368
398
369
- // Handles port disconnection by removing the disconnected port -ellie
399
+ // Handles port disconnection by removing the disconnected port
370
400
port . onDisconnect . addListener ( ( ) => {
371
401
const index = portsArr . indexOf ( port ) ;
372
402
if ( index !== - 1 ) {
@@ -656,7 +686,7 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
656
686
break ;
657
687
}
658
688
659
- // DUPLICATE SNAPSHOT CHECK -ellie
689
+ // DUPLICATE SNAPSHOT CHECK
660
690
const isDuplicateSnapshot = ( previous , incoming ) => {
661
691
if ( ! previous || ! incoming ) return false ;
662
692
const prevData = previous ?. componentData ;
@@ -792,6 +822,23 @@ chrome.tabs.onActivated.addListener((info) => {
792
822
} ) ;
793
823
} ) ;
794
824
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
795
832
chrome . runtime . onStartup . addListener ( ( ) => {
833
+ console . log ( 'Browser started. Setting up keep-alive...' ) ;
796
834
setupKeepAlive ( ) ;
797
835
} ) ;
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