Skip to content

Commit 5abfda9

Browse files
committed
many more console.logs. Committing before I try a new approach. Going to add tabsObj instance upon the contextMenu being clicked.
1 parent 69a84ee commit 5abfda9

File tree

5 files changed

+63
-16
lines changed

5 files changed

+63
-16
lines changed

src/app/containers/ErrorContainer.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ function ErrorContainer(): JSX.Element {
2222
const titleTracker = useRef(currentTitle); // useRef returns an object with a property 'initialValue' and a value of whatever was passed in. This allows us to reference a value that's not needed for rendering
2323
const timeout = useRef(null);
2424

25+
console.log(
26+
'ErrorContainer state variables: tabs: ',
27+
tabs,
28+
'currentTab: ',
29+
currentTab,
30+
'currentTitle: ',
31+
);
2532
// function that launches the main app
2633
function launch(): void {
2734
dispatch(launchContentScript(tabs[currentTab]));

src/app/containers/MainContainer.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ function MainContainer(): JSX.Element {
2828

2929
const { currentTab, tabs, port }: MainState = useSelector((state: RootState) => state.main);
3030

31+
//JR: check connection status
32+
const { connectionStatus }: MainState = useSelector((state: RootState) => state.main);
33+
console.log('MainContainer connectionStatus at initialization: ', connectionStatus);
34+
3135
const [actionView, setActionView] = useState(true); // We create a local state 'actionView' and set it to true
3236

3337
// this function handles Time Jump sidebar view
@@ -64,7 +68,14 @@ function MainContainer(): JSX.Element {
6468
}) => {
6569
const { action, payload, sourceTab } = message;
6670
let maxTab: number;
67-
71+
console.log(
72+
'MainContainer messageListener message. action: ',
73+
action,
74+
'payload: ',
75+
payload,
76+
'sourceTab: ',
77+
sourceTab,
78+
);
6879
if (!sourceTab && action !== 'keepAlive') {
6980
// if the sourceTab doesn't exist or is 0 and it is not a 'keepAlive' action
7081
const tabsArray: Array<string> = Object.keys(payload); // we create a tabsArray of strings composed of keys from our payload object
@@ -106,20 +117,29 @@ function MainContainer(): JSX.Element {
106117
};
107118

108119
useEffect(() => {
120+
console.log('MainContainer state view of port at start of useEffect: ', port);
109121
if (port) return; // only open port once so if it exists, do not run useEffect again
110122

111123
// Connect ot port and assign evaluated result (obj) to currentPort
112-
const currentPort = chrome.runtime.connect();
124+
const currentPort = chrome.runtime.connect({ name: 'uiPort1' });
113125

114126
// JR: why are we removing the listener just to readd it? logging here
115127
console.log('messageListener before removing: ', messageListener);
128+
console.log(
129+
'currentPort hasListener? before removing: ',
130+
currentPort.onMessage.hasListener(messageListener),
131+
);
116132
// If messageListener exists on currentPort, remove it
117133
while (currentPort.onMessage.hasListener(messageListener))
118134
currentPort.onMessage.removeListener(messageListener);
119135
console.log('messageListener after removing: ', messageListener);
120136

121137
// Add messageListener to the currentPort
122138
currentPort.onMessage.addListener(messageListener);
139+
console.log(
140+
'currentPort hasListener? after re-adding: ',
141+
currentPort.onMessage.hasListener(messageListener),
142+
);
123143

124144
// If handleDisconnect exists on chrome.runtime, remove it
125145
while (chrome.runtime.onMessage.hasListener(handleDisconnect))

src/app/slices/mainSlice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export const mainSlice = createSlice({
143143
} else if (typeof action.payload === 'object') {
144144
state.currentTab = action.payload.tabId;
145145
if (action.payload?.title) state.currentTitle = action.payload.title;
146-
console.log('mainSlice setTab currentTitle: ', state.currentTitle);
146+
console.log('mainSlice setTab successful! currentTitle: ', state.currentTitle);
147147
return;
148148
}
149149
}

src/extension/background.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,17 @@ chrome.runtime.onConnect.addListener((port) => {
168168

169169
// JR: CONSIDER DELETING
170170
// 12.20.23 commenting out, possible culprit of many in no target bug
171-
// if (portsArr.length > 0) {
172-
// portsArr.forEach((bg) => {
173-
// // go through each port object (each Reactime instance)
174-
// bg.postMessage({
175-
// // 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-
// }
171+
if (portsArr.length > 0) {
172+
portsArr.forEach((bg) => {
173+
console.log('background onConnect. Send changeTab for port ', bg);
174+
// go through each port object (each Reactime instance)
175+
bg.postMessage({
176+
// send passed in action object as a message to the current port
177+
action: 'changeTab',
178+
payload: { tabId: activeTab.id, title: activeTab.title },
179+
});
180+
});
181+
}
181182

182183
// JR: CONSIDER DELETING
183184
if (Object.keys(tabsObj).length > 0) {
@@ -189,11 +190,14 @@ chrome.runtime.onConnect.addListener((port) => {
189190

190191
// every time devtool is closed, remove the port from portsArr
191192
port.onDisconnect.addListener((e) => {
193+
console.log('port onDisconnect triggered, portsArr: ', portsArr);
192194
for (let i = 0; i < portsArr.length; i += 1) {
193195
if (portsArr[i] === e) {
196+
// if (portsArr.length === 1) portsArr[i].sendMessage('portDisconnect'); // JR 12.20.23 try sending message to last remaining port directly prior to it being disconnected
194197
portsArr.splice(i, 1);
195-
chrome.runtime.sendMessage('portDisconnect');
196-
console.log(`port ${e} disconnected. Remaining portsArr: `, portsArr);
198+
chrome.runtime.sendMessage({ action: 'portDisconnect', port: e.name }); // JR 12.20.23 isn't this supposed to be a port.sendMessage? chrome.runtime sends messages between content script and background.js
199+
console.log('spliced portsArr', portsArr);
200+
console.log(`port ${e.name} disconnected. Remaining portsArr: `, portsArr);
197201
break;
198202
}
199203
}
@@ -516,6 +520,7 @@ chrome.contextMenus.onClicked.addListener(({ menuItemId }) => {
516520
// });
517521

518522
chrome.windows.getCurrent((window) => {
523+
console.log('onContext click window properties', window);
519524
const invokedScreenHeight = window.height || 1000;
520525
const invokedScreenTop = window.top || 0;
521526
const invokedScreenLeft = -400;

src/extension/contentScript.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
chrome.tabs.getCurrent((tab) => {
2+
console.log('contentScript loaded on ', tab);
3+
});
4+
15
// Web vital metrics calculated by 'web-vitals' npm package to be displayed
26
// in Web Metrics tab of Reactime app.
37
import { onTTFB, onLCP, onFID, onFCP, onCLS, onINP } from 'web-vitals';
@@ -42,7 +46,13 @@ window.addEventListener('message', (msg) => {
4246
// FROM BACKGROUND TO CONTENT SCRIPT
4347
// Listening for messages from the UI of the Reactime extension.
4448
chrome.runtime.onMessage.addListener((request) => {
45-
const { action }: { action: string } = request;
49+
console.log(
50+
'contentScript received message from background.js. request.action: ',
51+
request.action,
52+
'request.port',
53+
request.port,
54+
);
55+
const { action, port }: { action: string; port?: string } = request;
4656
if (action) {
4757
// Message being sent from background.js
4858
// This is toggling the record button on Reactime when clicked
@@ -53,6 +63,11 @@ chrome.runtime.onMessage.addListener((request) => {
5363
if (action === 'jumpToSnap') {
5464
chrome.runtime.sendMessage(request);
5565
}
66+
67+
// JR: adding a response to a port disconnection message from background.js
68+
if (action === 'portDisconnect') {
69+
console.log(`Port ${port} disconnected!`);
70+
}
5671
// After the jumpToSnap action has been sent back to background js,
5772
// it will send the same action to backend files (index.ts) for it execute the jump feature
5873
// '*' == target window origin required for event to be dispatched, '*' = no preference

0 commit comments

Comments
 (0)