Skip to content

Commit 0e463c5

Browse files
saving most up to date background.js
1 parent 626b30b commit 0e463c5

File tree

1 file changed

+55
-22
lines changed

1 file changed

+55
-22
lines changed

src/extension/background.js

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,39 @@ function changeCurrLocation(tabObj, rootNode, index, name) {
132132
}
133133
}
134134

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+
135140
// Establishing incoming connection with Reactime.
136141
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+
140164
// On Reactime launch: make sure RT's active tab is correct
141165
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
144168
action: 'changeTab',
145169
payload: { tabId: activeTab.id, title: activeTab.title },
146170
}),
@@ -182,39 +206,48 @@ chrome.runtime.onConnect.addListener((port) => {
182206
switch (action) {
183207
case 'import': // create a snapshot property on tabId and set equal to tabs object
184208
// may need do something like filter payload from stateless
185-
tabsObj[tabId].snapshots = payload;
186-
return true;
209+
tabsObj[tabId].snapshots = payload.snapshots; // reset snapshots to page last state recorded
210+
// tabsObj[tabId].hierarchy = savedSnapshot.hierarchy; // why don't we just use hierarchy? Because it breaks everything...
211+
tabsObj[tabId].hierarchy.children = payload.hierarchy.children; // resets hierarchy to last state recorded
212+
tabsObj[tabId].hierarchy.stateSnapshot = payload.hierarchy.stateSnapshot; // resets hierarchy to last state recorded
213+
tabsObj[tabId].currLocation = payload.currLocation; // resets currLocation to last state recorded
214+
tabsObj[tabId].index = payload.index; //reset index to last state recorded
215+
tabsObj[tabId].currParent = payload.currParent; // reset currParent to last state recorded
216+
tabsObj[tabId].currBranch = payload.currBranch; // reset currBranch to last state recorded
217+
218+
return true; // return true so that port remains open
219+
187220
case 'emptySnap':
188-
// reset snapshots to page last state recorded
189-
tabsObj[tabId].snapshots = [tabsObj[tabId].snapshots[tabsObj[tabId].snapshots.length - 1]];
190-
// resets hierarchy
191-
tabsObj[tabId].hierarchy.children = [];
192-
// resets hierarchy to page last state recorded
193-
tabsObj[tabId].hierarchy.stateSnapshot = {
221+
tabsObj[tabId].snapshots = [tabsObj[tabId].snapshots[tabsObj[tabId].snapshots.length - 1]]; // reset snapshots to page last state recorded
222+
tabsObj[tabId].hierarchy.children = []; // resets hierarchy
223+
tabsObj[tabId].hierarchy.stateSnapshot = { // resets hierarchy to page last state recorded
194224
...tabsObj[tabId].snapshots[0],
195225
};
196-
// resets currLocation to page last state recorded
197-
tabsObj[tabId].currLocation = tabsObj[tabId].hierarchy;
198-
tabsObj[tabId].index = 1;
199-
tabsObj[tabId].currParent = 0;
200-
tabsObj[tabId].currBranch = 1;
201-
return true;
202-
// Pause = lock on tab
203-
case 'setPause':
226+
tabsObj[tabId].currLocation = tabsObj[tabId].hierarchy; // resets currLocation to page last state recorded
227+
tabsObj[tabId].index = 1; //reset index
228+
tabsObj[tabId].currParent = 0; // reset currParent
229+
tabsObj[tabId].currBranch = 1; // reset currBranch
230+
return true; // return true so that port remains open
231+
232+
case 'setPause': // Pause = lock on tab
204233
tabsObj[tabId].mode.paused = payload;
205-
return true;
234+
return true; // return true so that port remains open
235+
206236
case 'launchContentScript':
207237
chrome.scripting.executeScript({
208238
target: { tabId },
209239
files: ['bundles/content.bundle.js'],
210240
});
211241
return true;
242+
212243
case 'jumpToSnap':
213244
chrome.tabs.sendMessage(tabId, msg);
214245
return true; // attempt to fix message port closing error, consider return Promise
246+
215247
case 'toggleRecord':
216248
chrome.tabs.sendMessage(tabId, msg);
217249
return true;
250+
218251
default:
219252
return true;
220253
}

0 commit comments

Comments
 (0)