Skip to content

Commit 8a461e9

Browse files
fix: global hook not installing (#133)
1 parent 13fe82c commit 8a461e9

File tree

4 files changed

+32
-48
lines changed

4 files changed

+32
-48
lines changed

src/shells/webextension/contentScript.js

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ window.addEventListener('message', function listener(message) {
120120
});
121121

122122
// Listen for messages from the background script
123-
chrome.runtime.onMessage.addListener((message, sender) => {
123+
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
124124
if (message.type === 'panel-message') {
125125
// Forward to backend
126126
window.postMessage(
@@ -131,52 +131,42 @@ chrome.runtime.onMessage.addListener((message, sender) => {
131131
},
132132
'*',
133133
);
134+
// Send immediate response to avoid channel closing
135+
sendResponse({ received: true });
134136
}
137+
// Return false to indicate we won't send an async response
138+
return false;
135139
});
136140

137141
// Handle messages from backend
138142
window.addEventListener('message', evt => {
139143
if (evt.data.source === 'mobx-devtools-backend' && evt.data.contentScriptId === contentScriptId) {
140144
// Forward to panel via background script
141-
chrome.runtime
142-
.sendMessage({
143-
type: 'content-to-panel',
144-
data: evt.data.payload,
145-
})
146-
.catch(err => {
147-
// Ignore errors about receiving end not existing
148-
if (!err.message.includes('receiving end does not exist')) {
149-
console.error('Error sending message:', err);
150-
}
151-
});
152-
}
153-
});
154-
155-
// Add to port message listener
156-
port.onMessage.addListener(message => {
157-
if (message.type === 'panel-message') {
158-
window.postMessage(
159-
{
160-
source: 'mobx-devtools-content-script',
161-
payload: message.data,
162-
contentScriptId: contentScriptId,
163-
backendId: backendId,
164-
},
165-
'*',
166-
);
145+
try {
146+
chrome.runtime
147+
.sendMessage({
148+
type: 'content-to-panel',
149+
data: evt.data.payload,
150+
})
151+
.catch(() => {
152+
// Ignore "receiving end does not exist" errors silently
153+
});
154+
} catch (err) {
155+
// Ignore chrome.runtime not available errors
156+
console.debug('Failed to send message:', err);
157+
}
167158
}
168159
});
169160

170-
// Add this message handler for panel messages
161+
// Consolidate duplicate port message listeners into one
171162
port.onMessage.addListener(message => {
172163
if (message.type === 'panel-message') {
173-
// Add these specific properties needed by the backend
174164
window.postMessage(
175165
{
176166
source: 'mobx-devtools-content-script',
177167
payload: message.data,
178-
contentScriptId: contentScriptId, // Make sure this matches the initial handshake
179-
backendId: backendId, // Make sure this is available from the handshake
168+
contentScriptId,
169+
backendId,
180170
},
181171
'*',
182172
);
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Simply importing this file will install the global hook here
22
import installGlobalHook from '../../backend/utils/installGlobalHook';
33

4-
// if (__DEV__) {
5-
window.addEventListener('test-open-mobx-devtools-window', () => {
6-
console.log('test-open-mobx-devtools-window'); // eslint-disable-line no-console
7-
chrome.runtime.sendMessage({ eventName: 'open-mobx-devtools-window' });
8-
});
9-
// }
4+
if (__DEV__) {
5+
window.addEventListener('test-open-mobx-devtools-window', () => {
6+
console.log('test-open-mobx-devtools-window'); // eslint-disable-line no-console
7+
chrome.runtime.sendMessage({ eventName: 'open-mobx-devtools-window' });
8+
});
9+
}
10+
11+
installGlobalHook(window);

src/shells/webextension/manifest.json

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,12 @@
4141
"permissions": ["contextMenus", "storage", "scripting"],
4242
"host_permissions": ["<all_urls>"],
4343

44-
"browser_action": {
45-
"default_icon": {
46-
"16": "icons/toolbar-chrome.png",
47-
"32": "icons/[email protected]"
48-
},
49-
"default_title": "Open"
50-
},
51-
5244
"content_scripts": [
5345
{
5446
"matches": ["<all_urls>"],
5547
"js": ["injectGlobalHook.js"],
56-
"run_at": "document_start"
48+
"run_at": "document_start",
49+
"world": "MAIN"
5750
}
5851
]
59-
6052
}

src/shells/webextension/panel-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function createPanelIfMobxLoaded() {
1616
world: 'MAIN',
1717
})
1818
.then(([result]) => {
19-
const pageHasMobx = result?.result; // Access the result property
19+
const pageHasMobx = result?.result;
2020

2121
if (!pageHasMobx || panelCreated) {
2222
return;

0 commit comments

Comments
 (0)