Skip to content

Commit 7b8b70c

Browse files
committed
adjusted component map size and added port disconenct listerner logic to content script
1 parent e804515 commit 7b8b70c

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

src/app/components/StateRoute/ComponentMap/ComponentMap.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const veryHeavy = '#475569'; // Darker gray for medium load
3232
const defaultMargin: DefaultMargin = {
3333
top: 30,
3434
left: 20,
35-
right: 20,
36-
bottom: 70,
35+
right: 50,
36+
bottom: 20,
3737
};
3838

3939
const nodeCoords: object = {};

src/app/styles/layout/_stateContainer.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
}
1313

1414
.app-content {
15-
flex: 1;
15+
height: 100%;
1616
min-height: 0;
17-
position: relative;
1817
}
1918

2019
.main-navbar-container--structural {

src/extension/background.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,10 @@ chrome.runtime.onConnect.addListener(async (port) => {
360360
}
361361

362362
if (Object.keys(tabsObj).length > 0) {
363-
console.log('Sending initial snapshots to devtools:', tabsObj);
364363
port.postMessage({
365364
action: 'initialConnectSnapshots',
366365
payload: tabsObj,
367366
});
368-
} else {
369-
console.log('No snapshots to send to devtools on reconnect.');
370367
}
371368

372369
// Handles port disconnection by removing the disconnected port -ellie
@@ -375,6 +372,17 @@ chrome.runtime.onConnect.addListener(async (port) => {
375372
if (index !== -1) {
376373
console.warn(`Port at index ${index} disconnected. Removing it.`);
377374
portsArr.splice(index, 1);
375+
376+
// Notify remaining ports about the disconnection
377+
portsArr.forEach((remainingPort) => {
378+
try {
379+
remainingPort.postMessage({
380+
action: 'portDisconnect',
381+
});
382+
} catch (error) {
383+
console.warn('Failed to notify port of disconnection:', error);
384+
}
385+
});
378386
}
379387
});
380388

@@ -425,13 +433,42 @@ chrome.runtime.onConnect.addListener(async (port) => {
425433
tabsObj[tabId].mode.paused = payload;
426434
return true; // return true so that port remains open
427435

428-
case 'launchContentScript':
429-
//if (tab.url?.startsWith("chrome://")) return undefined;
430-
chrome.scripting.executeScript({
431-
target: { tabId },
432-
files: ['bundles/content.bundle.js'],
433-
});
436+
// Handling the launchContentScript case with proper validation
437+
case 'launchContentScript': {
438+
if (!tabId) {
439+
console.error('No tabId provided for content script injection');
440+
return false;
441+
}
442+
443+
try {
444+
// Validate the tab exists before injecting
445+
chrome.tabs.get(tabId, async (tab) => {
446+
if (chrome.runtime.lastError) {
447+
console.error('Error getting tab:', chrome.runtime.lastError);
448+
return;
449+
}
450+
451+
// Skip injection for chrome:// URLs
452+
if (tab.url?.startsWith('chrome://')) {
453+
console.warn('Cannot inject scripts into chrome:// URLs');
454+
return;
455+
}
456+
457+
try {
458+
await chrome.scripting.executeScript({
459+
target: { tabId: tab.id },
460+
files: ['bundles/content.bundle.js'],
461+
});
462+
console.log('Content script injected successfully');
463+
} catch (err) {
464+
console.error('Error injecting content script:', err);
465+
}
466+
});
467+
} catch (err) {
468+
console.error('Error in launchContentScript:', err);
469+
}
434470
return true;
471+
}
435472

436473
case 'jumpToSnap':
437474
chrome.tabs.sendMessage(tabId, msg);

src/extension/contentScript.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,17 @@ chrome.runtime.onMessage.addListener((request) => {
7373
// '*' == target window origin required for event to be dispatched, '*' = no preference
7474
window.postMessage(request, '*');
7575
}
76-
77-
// JR: adding a response to a port disconnection message from background.js
7876
if (action === 'portDisconnect') {
77+
// When we receive a port disconnection message, relay it to the window
78+
window.postMessage(
79+
{
80+
action: 'portDisconnect',
81+
},
82+
'*',
83+
);
84+
85+
// Attempt to re-establish connection
86+
establishConnection();
7987
}
8088

8189
if (action === 'reinitialize') {

0 commit comments

Comments
 (0)