You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/extension/background.js
+53-36Lines changed: 53 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -132,15 +132,39 @@ function changeCurrLocation(tabObj, rootNode, index, name) {
132
132
}
133
133
}
134
134
135
+
/*
136
+
The 'chrome.runtime' API allows a connection to the background service worker (background.js).
137
+
This allows us to set up listener's for when we connect, message, and disconnect the script.
138
+
*/
139
+
135
140
// Establishing incoming connection with Reactime.
136
141
chrome.runtime.onConnect.addListener((port)=>{
137
-
// port is one end of the connection - an object
138
-
// push every port connected to the ports array
139
-
portsArr.push(port);
142
+
/*
143
+
On initial connection, there is an onConnect event emitted. The 'addlistener' method provides a communication channel object ('port') when we connect to the service worker ('background.js') and applies it as the argument to it's 1st callback parameter.
144
+
145
+
the 'port' (type: communication channel object) is the communication channel between different components within our Chrome extension, not to a port on the Chrome browser tab or the extension's port on the Chrome browser.
146
+
147
+
The port object facilitates communication between the Reactime front-end and this 'background.js' script. This allows you to:
148
+
1. send messages and data
149
+
(look for 'onMessage'/'postMessage' methods within this page)
150
+
2. receive messages and data
151
+
(look for 'addListener' methods within this page)
152
+
between the front-end and the background.
153
+
154
+
To establish communication between different parts of your extension:
155
+
for the connecting end: use chrome.runtime.connect()
156
+
for the listening end: use chrome.runtime.onConnect.
157
+
Once the connection is established, a port object is passed to the addListener callback function, allowing you to start exchanging data.
158
+
159
+
Again, this port object is used for communication within your extension, not for communication with external ports or tabs in the Chrome browser. If you need to interact with specific tabs or external ports, you would use other APIs or methods, such as chrome.tabs or other Chrome Extension APIs.
160
+
*/
161
+
162
+
portsArr.push(port);// push each Reactime communication channel object to the portsArr
163
+
140
164
// On Reactime launch: make sure RT's active tab is correct
141
165
if(portsArr.length>0){
142
-
portsArr.forEach((bg)=>
143
-
bg.postMessage({
166
+
portsArr.forEach((bg)=>// go through each port object (each Reactime instance)
167
+
bg.postMessage({// send passed in action object as a message to the current port
0 commit comments