diff --git a/devtools/background.js b/devtools/background.js index 6c8c76f..31c33df 100644 --- a/devtools/background.js +++ b/devtools/background.js @@ -1,24 +1,54 @@ console.log("background.js loaded"); -// Listen for messages from the devtools panel browser.runtime.onMessage.addListener((message, sender, sendResponse) => { - // You could add a check to see if the message is from the devtools panel. if (message.type === "GET_CONTEXT_STACK") { - // Forward the message to the content script running in the active tab. - browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { - if (tabs.length) { - browser.tabs.sendMessage(tabs[0].id, message).then(sendResponse); - } - }); - // Return true to indicate you wish to send a response asynchronously + browser.tabs.sendMessage(message.tabId, message) + .then((res) => { + console.log("RESPONSE", res); + sendResponse(res); + }) + .catch((err) => { + console.log("Error sending message to content script:", err); + sendResponse({ error: err }); + }); + // browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { + // if (tabs.length) { + // } + // }); return true; } else if (message.type === "UPDATE_CONTEXT_VALUE") { - // Forward update messages similarly - browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { - if (tabs.length) { - browser.tabs.sendMessage(tabs[0].id, message).then(sendResponse); - } - }); + browser.tabs.sendMessage(message.tabId, message).then(sendResponse); + // browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { + // if (tabs.length) { + // } + // }); return true; } }); + +let devtoolsPort = null; + +browser.runtime.onConnect.addListener((port) => { + if (port.name === "devtools") { + devtoolsPort = port; + console.log("Devtools panel connected."); + + // port.onMessage.addListener((msg) => { + // console.log("Received message from devtools panel:", msg); + // }); + + port.onDisconnect.addListener(() => { + devtoolsPort = null; + }); + } +}); + +// Relay messages from content scripts to the devtools panel. +browser.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.type === "PAGE_LOADED") { + console.log("Background received PAGE_LOADED message from content script."); + if (devtoolsPort) { + devtoolsPort.postMessage({ type: "PAGE_LOADED" }); + } + } +}); diff --git a/devtools/content.js b/devtools/content.js index b935d84..d6662ba 100644 --- a/devtools/content.js +++ b/devtools/content.js @@ -1,20 +1,12 @@ -console.log("content.js loaded", window.wrappedJSObject); - -// setTimeout(() => { -const getContextStack = () => window.wrappedJSObject.getContextStack(); -const updateContextValue = (key, value, depth) => - window.wrappedJSObject.updateContextValue(key, value, depth); - -console.log(getContextStack()); browser.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.type === "GET_CONTEXT_STACK") { - const contextStack = getContextStack(); + const contextStack = window.wrappedJSObject.getContextStack(); sendResponse({ contextStack }); } else if (message.type === "UPDATE_CONTEXT_VALUE") { const { key, value, depth } = message; - if (glowindow.updateContextValue) { - updateContextValue(key, value, depth); + if (window.wrappedJSObject.updateContextValue) { + window.wrappedJSObject.updateContextValue(key, value, depth); sendResponse({ success: true }); } else { sendResponse({ @@ -24,4 +16,5 @@ browser.runtime.onMessage.addListener((message, sender, sendResponse) => { } } }); -// }, 0); + +browser.runtime.sendMessage({ type: "PAGE_LOADED" }); diff --git a/devtools/manifest.json b/devtools/manifest.json index 24558ae..89caac5 100644 --- a/devtools/manifest.json +++ b/devtools/manifest.json @@ -16,7 +16,8 @@ ], "js": [ "content.js" - ] + ], + "all_frames": true } ], "permissions": [ diff --git a/devtools/panel.html b/devtools/panel.html index c193569..fa87a46 100644 --- a/devtools/panel.html +++ b/devtools/panel.html @@ -1,39 +1,48 @@ +
+ +