Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: global hook not installing #133

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 21 additions & 31 deletions src/shells/webextension/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ window.addEventListener('message', function listener(message) {
});

// Listen for messages from the background script
chrome.runtime.onMessage.addListener((message, sender) => {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'panel-message') {
// Forward to backend
window.postMessage(
Expand All @@ -131,52 +131,42 @@ chrome.runtime.onMessage.addListener((message, sender) => {
},
'*',
);
// Send immediate response to avoid channel closing
sendResponse({ received: true });
}
// Return false to indicate we won't send an async response
return false;
});

// Handle messages from backend
window.addEventListener('message', evt => {
if (evt.data.source === 'mobx-devtools-backend' && evt.data.contentScriptId === contentScriptId) {
// Forward to panel via background script
chrome.runtime
.sendMessage({
type: 'content-to-panel',
data: evt.data.payload,
})
.catch(err => {
// Ignore errors about receiving end not existing
if (!err.message.includes('receiving end does not exist')) {
console.error('Error sending message:', err);
}
});
}
});

// Add to port message listener
port.onMessage.addListener(message => {
if (message.type === 'panel-message') {
window.postMessage(
{
source: 'mobx-devtools-content-script',
payload: message.data,
contentScriptId: contentScriptId,
backendId: backendId,
},
'*',
);
try {
chrome.runtime
.sendMessage({
type: 'content-to-panel',
data: evt.data.payload,
})
.catch(() => {
// Ignore "receiving end does not exist" errors silently
});
} catch (err) {
// Ignore chrome.runtime not available errors
console.debug('Failed to send message:', err);
}
}
});

// Add this message handler for panel messages
// Consolidate duplicate port message listeners into one
port.onMessage.addListener(message => {
if (message.type === 'panel-message') {
// Add these specific properties needed by the backend
window.postMessage(
{
source: 'mobx-devtools-content-script',
payload: message.data,
contentScriptId: contentScriptId, // Make sure this matches the initial handshake
backendId: backendId, // Make sure this is available from the handshake
contentScriptId,
backendId,
},
'*',
);
Expand Down
14 changes: 8 additions & 6 deletions src/shells/webextension/injectGlobalHook.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Simply importing this file will install the global hook here
import installGlobalHook from '../../backend/utils/installGlobalHook';

// if (__DEV__) {
window.addEventListener('test-open-mobx-devtools-window', () => {
console.log('test-open-mobx-devtools-window'); // eslint-disable-line no-console
chrome.runtime.sendMessage({ eventName: 'open-mobx-devtools-window' });
});
// }
if (__DEV__) {
window.addEventListener('test-open-mobx-devtools-window', () => {
console.log('test-open-mobx-devtools-window'); // eslint-disable-line no-console
chrome.runtime.sendMessage({ eventName: 'open-mobx-devtools-window' });
});
}

installGlobalHook(window);
12 changes: 2 additions & 10 deletions src/shells/webextension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,12 @@
"permissions": ["contextMenus", "storage", "scripting"],
"host_permissions": ["<all_urls>"],

"browser_action": {
"default_icon": {
"16": "icons/toolbar-chrome.png",
"32": "icons/toolbar-chrome@2x.png"
},
"default_title": "Open"
},

"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["injectGlobalHook.js"],
"run_at": "document_start"
"run_at": "document_start",
"world": "MAIN"
}
]

}
2 changes: 1 addition & 1 deletion src/shells/webextension/panel-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function createPanelIfMobxLoaded() {
world: 'MAIN',
})
.then(([result]) => {
const pageHasMobx = result?.result; // Access the result property
const pageHasMobx = result?.result;

if (!pageHasMobx || panelCreated) {
return;
Expand Down
Loading