1
- const bgArr = [ ] ;
1
+ const portsArr = [ ] ;
2
+ // store ports in an array
2
3
const tabsObj = {
3
4
sourceTab : null ,
4
5
} ;
@@ -18,22 +19,22 @@ function createTabObj(title) {
18
19
19
20
// establishing connection with devtools
20
21
chrome . runtime . onConnect . addListener ( port => {
21
- bgArr . push ( port ) ;
22
+ // push every port connected to the ports array
23
+ portsArr . push ( port ) ;
22
24
23
- console . log ( 'bgArr' , bgArr ) ;
24
- // send it to devtools as soon as connection to devtools is made
25
+ // send tabs obj to the connected devtools as soon as connection to devtools is made
25
26
if ( Object . keys ( tabsObj ) . length > 0 ) {
26
27
port . postMessage ( {
27
28
action : 'initialConnectSnapshots' ,
28
29
payload : tabsObj ,
29
30
} ) ;
30
31
}
31
32
32
- // every time devtool is closed, remove the port from bgArr
33
+ // every time devtool is closed, remove the port from portsArr
33
34
port . onDisconnect . addListener ( e => {
34
- for ( let i = 0 ; i < bgArr . length ; i += 1 ) {
35
- if ( bgArr [ i ] === e ) {
36
- bgArr . splice ( i , 1 ) ;
35
+ for ( let i = 0 ; i < portsArr . length ; i += 1 ) {
36
+ if ( portsArr [ i ] === e ) {
37
+ portsArr . splice ( i , 1 ) ;
37
38
break ;
38
39
}
39
40
}
@@ -106,8 +107,8 @@ chrome.runtime.onMessage.addListener((request, sender) => {
106
107
tabsObj [ tabId ] . firstSnapshot = false ;
107
108
// don't add anything to snapshot storage if mode is persisting for the initial snapshot
108
109
if ( ! persist ) tabsObj [ tabId ] . snapshots . push ( request . payload ) ;
109
- if ( bgArr . length > 0 ) {
110
- bgArr . forEach ( bg => bg . postMessage ( {
110
+ if ( portsArr . length > 0 ) {
111
+ portsArr . forEach ( bg => bg . postMessage ( {
111
112
action : 'initialConnectSnapshots' ,
112
113
payload : tabsObj ,
113
114
} ) ) ;
@@ -119,8 +120,8 @@ chrome.runtime.onMessage.addListener((request, sender) => {
119
120
tabsObj . sourceTab = tabId ;
120
121
121
122
// send message to devtools
122
- if ( bgArr . length > 0 ) {
123
- bgArr . forEach ( bg => bg . postMessage ( {
123
+ if ( portsArr . length > 0 ) {
124
+ portsArr . forEach ( bg => bg . postMessage ( {
124
125
action : 'sendSnapshots' ,
125
126
payload : tabsObj ,
126
127
} ) ) ;
@@ -133,14 +134,14 @@ chrome.runtime.onMessage.addListener((request, sender) => {
133
134
134
135
// when tab is closed, remove the tabid from the tabsObj
135
136
chrome . tabs . onRemoved . addListener ( tabId => {
136
- // after deleting the tab, send the updated tabs object to devtools
137
- if ( bgArr . length > 0 ) {
138
- console . log ( 'background => delete tab' ) ;
139
- bgArr . forEach ( bg => bg . postMessage ( {
137
+ // tell devtools which tab to delete
138
+ if ( portsArr . length > 0 ) {
139
+ portsArr . forEach ( bg => bg . postMessage ( {
140
140
action : 'deleteTab' ,
141
141
payload : tabId ,
142
142
} ) ) ;
143
143
}
144
144
145
+ // delete the tab from the tabsObj
145
146
delete tabsObj [ tabId ] ;
146
147
} ) ;
0 commit comments