diff --git a/devtools/background.js b/devtools/background.js new file mode 100644 index 0000000..6c8c76f --- /dev/null +++ b/devtools/background.js @@ -0,0 +1,24 @@ +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 + 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); + } + }); + return true; + } +}); diff --git a/devtools/content.js b/devtools/content.js new file mode 100644 index 0000000..b935d84 --- /dev/null +++ b/devtools/content.js @@ -0,0 +1,27 @@ +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(); + + sendResponse({ contextStack }); + } else if (message.type === "UPDATE_CONTEXT_VALUE") { + const { key, value, depth } = message; + if (glowindow.updateContextValue) { + updateContextValue(key, value, depth); + sendResponse({ success: true }); + } else { + sendResponse({ + success: false, + error: "updateContextValue not defined", + }); + } + } +}); +// }, 0); diff --git a/devtools/devtools.html b/devtools/devtools.html new file mode 100644 index 0000000..89884ef --- /dev/null +++ b/devtools/devtools.html @@ -0,0 +1,12 @@ + + + +
+ + + + + + + + \ No newline at end of file diff --git a/devtools/devtools.js b/devtools/devtools.js new file mode 100644 index 0000000..cc71299 --- /dev/null +++ b/devtools/devtools.js @@ -0,0 +1,7 @@ +// Create a new devtools panel named "Context Stack" +browser.devtools.panels.create( + "Context Stack", // Tab title + "train icon.png", // Icon for your panel (optional) + "panel.html", // HTML page for your panel content +); +console.log("devtools loaded"); diff --git a/devtools/manifest.json b/devtools/manifest.json new file mode 100644 index 0000000..24558ae --- /dev/null +++ b/devtools/manifest.json @@ -0,0 +1,27 @@ +{ + "manifest_version": 2, + "name": "Context Stack DevTools", + "version": "1.0", + "description": "A devtools panel to view and edit context stack values.", + "devtools_page": "devtools.html", + "background": { + "scripts": [ + "background.js" + ] + }, + "content_scripts": [ + { + "matches": [ + "No context stack found.
"; + return; + } + + const contextStack = response.contextStack; + for (const key in contextStack) { + const row = document.createElement("div"); + row.classList.add("context-row"); + + const keyDiv = document.createElement("div"); + keyDiv.classList.add("context-key"); + keyDiv.textContent = key; + + const valueDiv = document.createElement("div"); + valueDiv.classList.add("context-value"); + + const input = document.createElement("input"); + input.value = contextStack[key]; + input.addEventListener("change", () => { + browser.runtime.sendMessage({ + type: "UPDATE_CONTEXT_VALUE", + key, + value: input.value, + }).then((updateResponse) => { + if (updateResponse && updateResponse.success) { + console.log(`Updated ${key} to ${input.value}`); + } else { + console.error(`Failed to update ${key}:`, updateResponse.error); + } + }); + }); + + valueDiv.appendChild(input); + row.appendChild(keyDiv); + row.appendChild(valueDiv); + container.appendChild(row); + } + }, + ); +} + +// Initial load when the panel opens. +loadContextStack(); diff --git a/devtools/train icon.png b/devtools/train icon.png new file mode 100644 index 0000000..b4a3826 Binary files /dev/null and b/devtools/train icon.png differ diff --git a/lib/context.ts b/lib/context.ts new file mode 100644 index 0000000..9856aa5 --- /dev/null +++ b/lib/context.ts @@ -0,0 +1,115 @@ +type ContextStore = Record