|
| 1 | +// Background service worker for the extension |
| 2 | + |
| 3 | +// Maps to store connections by tab ID |
| 4 | +const contentConnections = new Map(); // tab ID -> content script port |
| 5 | +const devtoolsConnections = new Map(); // tab ID -> devtools port |
| 6 | + |
| 7 | +chrome.runtime.onConnect.addListener(port => { |
| 8 | + if (port.name === "content-to-background") { |
| 9 | + handleContentScriptConnection(port); |
| 10 | + } else if (port.name === "devtools-to-background") { |
| 11 | + handleDevToolsConnection(port); |
| 12 | + } else { |
| 13 | + console.warn("Unknown connection type:", port.name); |
| 14 | + port.disconnect(); |
| 15 | + } |
| 16 | +}); |
| 17 | + |
| 18 | +function handleContentScriptConnection(port) { |
| 19 | + const tabId = port.sender?.tab?.id; |
| 20 | + |
| 21 | + if (!tabId) { |
| 22 | + console.error("Content script connection missing tab ID"); |
| 23 | + port.disconnect(); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + contentConnections.set(tabId, port); |
| 28 | + |
| 29 | + port.onMessage.addListener(message => { |
| 30 | + // Forward message to devtools if connected |
| 31 | + const devtoolsPort = devtoolsConnections.get(tabId); |
| 32 | + if (devtoolsPort) { |
| 33 | + try { |
| 34 | + devtoolsPort.postMessage(message); |
| 35 | + } catch (error) { |
| 36 | + console.error("Failed to forward message to devtools:", error); |
| 37 | + devtoolsConnections.delete(tabId); |
| 38 | + } |
| 39 | + } else { |
| 40 | + console.log( |
| 41 | + `No devtools connection for tab ${tabId}, message queued:`, |
| 42 | + message.type |
| 43 | + ); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + port.onDisconnect.addListener(() => { |
| 48 | + contentConnections.delete(tabId); |
| 49 | + |
| 50 | + // Notify devtools if connected |
| 51 | + const devtoolsPort = devtoolsConnections.get(tabId); |
| 52 | + if (devtoolsPort) { |
| 53 | + try { |
| 54 | + devtoolsPort.postMessage({ type: "CONTENT_SCRIPT_DISCONNECTED" }); |
| 55 | + } catch (error) { |
| 56 | + console.error( |
| 57 | + "Failed to notify devtools of content script disconnect:", |
| 58 | + error |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +function handleDevToolsConnection(port) { |
| 66 | + let tabId = null; |
| 67 | + let isInitialized = false; |
| 68 | + |
| 69 | + // Listen for the initial tab ID message |
| 70 | + const tabIdListener = message => { |
| 71 | + if (message.type === "DEVTOOLS_TAB_ID" && !isInitialized) { |
| 72 | + tabId = message.tabId; |
| 73 | + isInitialized = true; |
| 74 | + |
| 75 | + devtoolsConnections.set(tabId, port); |
| 76 | + |
| 77 | + // Remove the tab ID listener |
| 78 | + port.onMessage.removeListener(tabIdListener); |
| 79 | + |
| 80 | + // Set up the main message listener |
| 81 | + port.onMessage.addListener(message => { |
| 82 | + // Forward message to content script if connected |
| 83 | + const contentPort = contentConnections.get(tabId); |
| 84 | + if (contentPort) { |
| 85 | + try { |
| 86 | + contentPort.postMessage(message); |
| 87 | + } catch (error) { |
| 88 | + console.error( |
| 89 | + "Failed to forward message to content script:", |
| 90 | + error |
| 91 | + ); |
| 92 | + contentConnections.delete(tabId); |
| 93 | + } |
| 94 | + } else { |
| 95 | + console.log(`No content script connection for tab ${tabId}`); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + // Send initial status to devtools |
| 100 | + const contentPort = contentConnections.get(tabId); |
| 101 | + try { |
| 102 | + port.postMessage({ |
| 103 | + type: "BACKGROUND_READY", |
| 104 | + contentScriptConnected: !!contentPort, |
| 105 | + }); |
| 106 | + } catch (error) { |
| 107 | + console.error("Failed to send initial status to devtools:", error); |
| 108 | + } |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + port.onMessage.addListener(tabIdListener); |
| 113 | + |
| 114 | + port.onDisconnect.addListener(() => { |
| 115 | + if (tabId) { |
| 116 | + devtoolsConnections.delete(tabId); |
| 117 | + } |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +chrome.action.onClicked.addListener(tab => { |
| 122 | + chrome.tabs.sendMessage(tab.id, { type: "OPEN_DEVTOOLS_HINT" }); |
| 123 | +}); |
| 124 | + |
| 125 | +// Clean up connections when tabs are closed |
| 126 | +chrome.tabs.onRemoved.addListener(tabId => { |
| 127 | + contentConnections.delete(tabId); |
| 128 | + devtoolsConnections.delete(tabId); |
| 129 | +}); |
0 commit comments