Skip to content

Commit bf67b20

Browse files
committed
added ellie's fix for sending snapshots to reactime after re-opening
1 parent f2d5dd8 commit bf67b20

File tree

1 file changed

+23
-98
lines changed

1 file changed

+23
-98
lines changed

src/extension/background.js

Lines changed: 23 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// import 'core-js';
2-
3-
import { invoke } from 'lodash';
4-
51
// Store ports in an array.
62
const portsArr = [];
73
const reloaded = {};
@@ -364,52 +360,28 @@ chrome.runtime.onConnect.addListener(async (port) => {
364360
}
365361

366362
if (Object.keys(tabsObj).length > 0) {
363+
console.log('Sending initial snapshots to devtools:', tabsObj);
367364
port.postMessage({
368365
action: 'initialConnectSnapshots',
369366
payload: tabsObj,
370367
});
368+
} else {
369+
console.log('No snapshots to send to devtools on reconnect.');
371370
}
372371

373-
// Handles port disconnection by removing the disconnected port and attempting reconnection after 1s delay
374-
// This prevents permanent connection loss during idle periods or temporary disconnects -ellie
375-
port.onDisconnect.addListener((e) => {
376-
for (let i = 0; i < portsArr.length; i += 1) {
377-
if (portsArr[i] === e) {
378-
portsArr.splice(i, 1);
379-
setTimeout(async () => {
380-
try {
381-
const newPort = chrome.runtime.connect({ name: 'reconnected' }); // Attempt to reconnect
382-
if (newPort) {
383-
portsArr.push(newPort); // Add the new port to the array
384-
newPort.onMessage.addListener((msg) => {
385-
console.log('Message received on reconnected port:', msg);
386-
});
387-
console.log('Port successfully reconnected');
388-
} else {
389-
console.warn('Failed to reconnect port');
390-
}
391-
} catch (error) {
392-
console.warn('Port reconnection attempt failed:', error);
393-
}
394-
}, 1000);
395-
break;
396-
}
372+
// Handles port disconnection by removing the disconnected port -ellie
373+
port.onDisconnect.addListener(() => {
374+
const index = portsArr.indexOf(port);
375+
if (index !== -1) {
376+
console.warn(`Port at index ${index} disconnected. Removing it.`);
377+
portsArr.splice(index, 1);
397378
}
398379
});
399380

400381
// INCOMING MESSAGE FROM FRONTEND (MainContainer) TO BACKGROUND.js
401382
// listen for message containing a snapshot from devtools and send it to contentScript -
402383
// (i.e. they're all related to the button actions on Reactime)
403384
port.onMessage.addListener(async (msg) => {
404-
// msg is action denoting a time jump in devtools
405-
// ---------------------------------------------------------------
406-
// message incoming from devTools should look like this:
407-
// {
408-
// action: 'emptySnap',
409-
// payload: tabsObj,
410-
// tabId: 101
411-
// }
412-
// ---------------------------------------------------------------
413385
const { action, payload, tabId } = msg;
414386

415387
switch (action) {
@@ -692,15 +664,21 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
692664

693665
// sends new tabs obj to devtools
694666
if (portsArr.length > 0) {
695-
portsArr.forEach((bg) =>
696-
bg.postMessage({
697-
action: 'sendSnapshots',
698-
payload: tabsObj,
699-
sourceTab,
700-
}),
701-
);
667+
portsArr.forEach((bg, index) => {
668+
try {
669+
bg.postMessage({
670+
action: 'sendSnapshots',
671+
payload: tabsObj,
672+
sourceTab,
673+
});
674+
console.log(`Sent snapshots to port at index ${index}`);
675+
} catch (error) {
676+
console.warn(`Failed to send snapshots to port at index ${index}:`, error);
677+
}
678+
});
679+
} else {
680+
console.warn('No active ports to send snapshots to.');
702681
}
703-
break;
704682
}
705683
default:
706684
break;
@@ -774,59 +752,6 @@ chrome.tabs.onActivated.addListener((info) => {
774752
});
775753
});
776754

777-
// when reactime is installed
778-
// create a context menu that will open our devtools in a new window
779-
chrome.runtime.onInstalled.addListener(() => {
780-
chrome.contextMenus.create({
781-
id: 'reactime',
782-
title: 'Reactime',
783-
contexts: ['page', 'selection', 'image', 'link'],
784-
});
785-
setupKeepAlive();
786-
});
787-
788755
chrome.runtime.onStartup.addListener(() => {
789756
setupKeepAlive();
790757
});
791-
// when context menu is clicked, listen for the menuItemId,
792-
// if user clicked on reactime, open the devtools window
793-
794-
// JR 12.19.23
795-
// As of V22, if multiple monitors are used, it would open the reactime panel on the other screen, which was inconvenient when opening repeatedly for debugging.
796-
// V23 fixes this by making use of chrome.windows.getCurrent to get the top and left of the screen which invoked the extension.
797-
// As of chrome manifest V3, background.js is a 'service worker', which does not have access to the DOM or to the native 'window' method, so we use chrome.windows.getCurrent(callback)
798-
// chrome.windows.getCurrent returns a promise (asynchronous), so all resulting functionality must happen in the callback function, or it will run before 'invokedScreen' variables have been captured.
799-
chrome.contextMenus.onClicked.addListener(({ menuItemId }) => {
800-
// // this was a test to see if I could dynamically set the left property to be the 0 origin of the invoked DISPLAY (as opposed to invoked window).
801-
// // this would allow you to split your screen, keep the browser open on the right side, and reactime always opens at the top left corner.
802-
// // however it does not tell you which display is the one that invoked it, just gives the array of all available displays. Depending on your monitor setup, it may differ. Leaving for future iterators
803-
// chrome.system.display.getInfo((displayUnitInfo) => {
804-
// console.log(displayUnitInfo);
805-
// });
806-
chrome.windows.getCurrent((window) => {
807-
const invokedScreenTop = 75; // window.top || 0;
808-
const invokedScreenLeft = window.width < 1000 ? window.left + window.width - 1000 : window.left;
809-
const invokedScreenWidth = 1000;
810-
const invokedScreenHeight = window.height - invokedScreenTop || 1000;
811-
const options = {
812-
type: 'panel',
813-
left: invokedScreenLeft,
814-
top: invokedScreenTop,
815-
width: invokedScreenWidth,
816-
height: invokedScreenHeight,
817-
url: chrome.runtime.getURL('panel.html'),
818-
};
819-
if (menuItemId === 'reactime') chrome.windows.create(options);
820-
});
821-
822-
// JR 1.9.23: this code fixes the no target error on load by triggering chrome tab reload before the panel spins up.
823-
// It does not solve the root issue, which was deeply researched during v23 but we ran out of time to solve. Please see the readme for more information.
824-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
825-
if (tabs.length) {
826-
const invokedTab = tabs[0];
827-
const invokedTabId = invokedTab.id;
828-
const invokedTabTitle = invokedTab.title;
829-
chrome.tabs.reload(invokedTabId);
830-
}
831-
});
832-
});

0 commit comments

Comments
 (0)