Skip to content

Commit 7055370

Browse files
Christopher StamperChristopher Stamper
authored andcommitted
Fixed the lost functionality this week caused by the chrome update
1 parent 62e3497 commit 7055370

File tree

3 files changed

+45
-69
lines changed

3 files changed

+45
-69
lines changed

src/app/containers/MainContainer.tsx

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -143,41 +143,27 @@ function MainContainer(): JSX.Element {
143143
}
144144

145145
useEffect(() => {
146-
if (!connectRequested) return; // only open port once so if it exists, do not run useEffect again
147-
148-
// chrome.runtime allows our application to retrieve our service worker (our eventual bundles/background.bundle.js after running npm run build), details about the manifest, and allows us to listen and respond to events in our application lifecycle.
149-
150-
handleConnect()
151-
.then((port: chrome.runtime.Port | null) => {
152-
if (port) {
153-
console.log('PORT SUCCESS: ', port)
146+
if (port) return; // only open port once so if it exists, do not run useEffect again
154147

155-
const currentPort = port;
156-
157-
if (chrome.runtime.onMessage.hasListener(messageListener))
158-
chrome.runtime.onMessage.removeListener(messageListener);
159-
160-
// listen for a message containing snapshots from the /extension/build/background.js service worker
161-
currentPort.onMessage.addListener(messageListener);
162-
163-
currentPort.postMessage({ initialized: true });
164-
console.log('posted message');
165-
166-
if (chrome.runtime.onMessage.hasListener(handleDisconnect))
167-
chrome.runtime.onMessage.removeListener(handleDisconnect);
168-
169-
// used to track when the above connection closes unexpectedly. Remember that it should persist throughout the application lifecycle
170-
chrome.runtime.onMessage.addListener(handleDisconnect);
171-
172-
// assign port to state so it could be used by other components
173-
dispatch(setPort(currentPort));
148+
const currentPort = chrome.runtime.connect();
149+
150+
while (chrome.runtime.onMessage.hasListener(messageListener))
151+
chrome.runtime.onMessage.removeListener(messageListener);
152+
153+
// listen for a message containing snapshots from the /extension/build/background.js service worker
154+
currentPort.onMessage.addListener(messageListener);
155+
156+
while (chrome.runtime.onMessage.hasListener(handleDisconnect))
157+
chrome.runtime.onMessage.removeListener(handleDisconnect);
158+
159+
// used to track when the above connection closes unexpectedly. Remember that it should persist throughout the application lifecycle
160+
chrome.runtime.onMessage.addListener(handleDisconnect);
161+
162+
// assign port to state so it could be used by other components
163+
dispatch(setPort(currentPort));
174164

175-
dispatch(endConnect());
176-
} else {
177-
console.log('PORT FAIL: ', port);
178-
}
179-
});
180-
}, [connectRequested]);
165+
dispatch(endConnect());
166+
});
181167

182168
// Error Page launch IF(Content script not launched OR RDT not installed OR Target not React app)
183169
if (

src/backend/models/routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class Routes {
108108
this.current += delta;
109109
// If the difference is not 0, navigate to the target route using window.history.go() method
110110
if (delta !== 0) {
111-
window.history.go(delta);
111+
window.history.go(this.current);
112112
// Return true to indicate that the navigation was successful
113113
return true;
114114
}

src/extension/background.js

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ function changeCurrLocation(tabObj, rootNode, index, name) {
137137
This allows us to set up listener's for when we connect, message, and disconnect the script.
138138
*/
139139

140-
// let initialized = false;
141-
let portSuccessfullyConnected = false;
142-
143140
// Establishing incoming connection with Reactime.
144141
chrome.runtime.onConnect.addListener((port) => {
145142
/*
@@ -161,31 +158,24 @@ chrome.runtime.onConnect.addListener((port) => {
161158
162159
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.
163160
*/
164-
165-
console.log('Port: ', port);
166-
portSuccessfullyConnected = port ? true : false;
167161

168162
portsArr.push(port); // push each Reactime communication channel object to the portsArr
169163

170-
port.onMessage.addListener((msg) => {
171-
console.log('background message: ', msg);
172-
if (msg.initialized && portsArr.length > 0) {
173-
console.log('sending changeTab message!!!!');
174-
portsArr.forEach((bg) => {// go through each port object (each Reactime instance)
175-
bg.postMessage({ // send passed in action object as a message to the current port
176-
action: 'changeTab',
177-
payload: { tabId: activeTab.id, title: activeTab.title },
178-
})
179-
});
180-
}
181-
if (msg.initialized && Object.keys(tabsObj).length > 0) {
182-
console.log('sending initialConnectSnapshots message!!!!!')
183-
port.postMessage({
184-
action: 'initialConnectSnapshots',
185-
payload: tabsObj,
186-
});
187-
}
188-
});
164+
if (portsArr.length > 0) {
165+
portsArr.forEach((bg) => {// go through each port object (each Reactime instance)
166+
bg.postMessage({ // send passed in action object as a message to the current port
167+
action: 'changeTab',
168+
payload: { tabId: activeTab.id, title: activeTab.title },
169+
})
170+
});
171+
}
172+
173+
if (Object.keys(tabsObj).length > 0) {
174+
port.postMessage({
175+
action: 'initialConnectSnapshots',
176+
payload: tabsObj,
177+
});
178+
}
189179

190180
// every time devtool is closed, remove the port from portsArr
191181
port.onDisconnect.addListener((e) => {
@@ -225,7 +215,7 @@ chrome.runtime.onConnect.addListener((port) => {
225215
tabsObj[tabId].currParent = payload.currParent; // reset currParent to last state recorded
226216
tabsObj[tabId].currBranch = payload.currBranch; // reset currBranch to last state recorded
227217

228-
// return true; // return true so that port remains open
218+
return true; // return true so that port remains open
229219

230220
case 'emptySnap':
231221
tabsObj[tabId].snapshots = [tabsObj[tabId].snapshots[tabsObj[tabId].snapshots.length - 1]]; // reset snapshots to page last state recorded
@@ -237,29 +227,29 @@ chrome.runtime.onConnect.addListener((port) => {
237227
tabsObj[tabId].index = 1; //reset index
238228
tabsObj[tabId].currParent = 0; // reset currParent
239229
tabsObj[tabId].currBranch = 1; // reset currBranch
240-
// return true; // return true so that port remains open
230+
return true; // return true so that port remains open
241231

242232
case 'setPause': // Pause = lock on tab
243233
tabsObj[tabId].mode.paused = payload;
244-
// return true; // return true so that port remains open
234+
return true; // return true so that port remains open
245235

246236
case 'launchContentScript':
247237
chrome.scripting.executeScript({
248238
target: { tabId },
249239
files: ['bundles/content.bundle.js'],
250240
});
251-
// return true;
241+
return true;
252242

253243
case 'jumpToSnap':
254244
chrome.tabs.sendMessage(tabId, msg);
255-
// return true; // attempt to fix message port closing error, consider return Promise
245+
return true; // attempt to fix message port closing error, consider return Promise
256246

257247
case 'toggleRecord':
258248
chrome.tabs.sendMessage(tabId, msg);
259-
// return true;
249+
return true;
260250

261251
default:
262-
// return true;
252+
return true;
263253
}
264254
});
265255
});
@@ -268,7 +258,7 @@ chrome.runtime.onConnect.addListener((port) => {
268258
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
269259
// AUTOMATIC MESSAGE SENT BY CHROME WHEN CONTENT SCRIPT IS FIRST LOADED: set Content
270260
if (request.type === 'SIGN_CONNECT') {
271-
// return true;
261+
return true;
272262
}
273263
const tabTitle = sender.tab.title;
274264
const tabId = sender.tab.id;
@@ -290,7 +280,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
290280
) {
291281
isReactTimeTravel = true;
292282
} else {
293-
// return true;
283+
return true;
294284
}
295285
// everytime we get a new tabId, add it to the object
296286
if (isReactTimeTravel && !(tabId in tabsObj)) {
@@ -406,7 +396,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
406396
default:
407397
break;
408398
}
409-
// return true; // attempt to fix close port error
399+
return true; // attempt to fix close port error
410400
});
411401

412402
// when tab is closed, remove the tabId from the tabsObj

0 commit comments

Comments
 (0)