-
-
Notifications
You must be signed in to change notification settings - Fork 6
Simplify DOM watcher with configuration-level settings and automatic action restart #668
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
Changes from 4 commits
cb64521
fb5c90a
33eb9cf
96e1b61
dfeaf47
849340b
eb75504
5307c51
366a243
08416a7
89d4a6d
2234b28
ac1dc21
7ad38ab
20170c0
f2f86f0
6f236a3
5638e1d
b386572
6258183
83e968b
91fc60b
d09ab17
e085b30
34111f8
23edcb8
d16cbcd
ccc3b1c
4a5f6a4
754885f
5a249b7
bf30a8a
294ce4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { RUNTIME_MESSAGE_ACF } from '@dhruv-techapps/acf-common'; | ||
|
||
// DevTools communication handler for DOM Watcher | ||
export class DomWatcherDevToolsBackground { | ||
private devToolsPorts: Map<number, chrome.runtime.Port> = new Map(); | ||
|
||
constructor() { | ||
// Listen for DevTools connections | ||
chrome.runtime.onConnect.addListener((port) => { | ||
if (port.name === 'dom-watcher-devtools') { | ||
this.handleDevToolsConnection(port); | ||
} | ||
}); | ||
} | ||
|
||
private handleDevToolsConnection(port: chrome.runtime.Port) { | ||
const tabId = port.sender?.tab?.id; | ||
if (!tabId) { | ||
console.error('DomWatcherDevTools: No tab ID in port connection'); | ||
return; | ||
} | ||
|
||
console.log(`DomWatcherDevTools: Connected to tab ${tabId}`); | ||
this.devToolsPorts.set(tabId, port); | ||
|
||
// Handle messages from DevTools | ||
port.onMessage.addListener((message) => { | ||
this.handleDevToolsMessage(message, tabId); | ||
}); | ||
|
||
// Clean up on disconnect | ||
port.onDisconnect.addListener(() => { | ||
console.log(`DomWatcherDevTools: Disconnected from tab ${tabId}`); | ||
this.devToolsPorts.delete(tabId); | ||
Check warning on line 34 in apps/acf-extension/src/background/dom-watcher-devtools.ts
|
||
}); | ||
} | ||
|
||
private async handleDevToolsMessage(message: any, tabId: number) { | ||
Check warning on line 38 in apps/acf-extension/src/background/dom-watcher-devtools.ts
|
||
try { | ||
switch (message.type) { | ||
case 'GET_WATCHER_STATUS': | ||
await this.getWatcherStatus(tabId); | ||
break; | ||
case 'WATCHER_COMMAND': | ||
await this.sendWatcherCommand(tabId, message.command); | ||
break; | ||
default: | ||
console.warn('DomWatcherDevTools: Unknown message type:', message.type); | ||
} | ||
} catch (error) { | ||
console.error('DomWatcherDevTools: Error handling message:', error); | ||
} | ||
} | ||
|
||
private async getWatcherStatus(tabId: number) { | ||
try { | ||
// Send message to content script to get watcher status | ||
const response = await chrome.tabs.sendMessage(tabId, { | ||
action: RUNTIME_MESSAGE_ACF.DOM_WATCHER_GET_STATUS | ||
}); | ||
|
||
// Forward response to DevTools | ||
const port = this.devToolsPorts.get(tabId); | ||
if (port) { | ||
port.postMessage({ | ||
type: 'WATCHER_STATUS', | ||
data: response || { isActive: false, watchedActionsCount: 0, watchedActions: [] } | ||
}); | ||
} | ||
} catch (error) { | ||
console.error('DomWatcherDevTools: Error getting watcher status:', error); | ||
|
||
// Send default status on error | ||
const port = this.devToolsPorts.get(tabId); | ||
if (port) { | ||
port.postMessage({ | ||
type: 'WATCHER_STATUS', | ||
data: { isActive: false, watchedActionsCount: 0, watchedActions: [] } | ||
}); | ||
} | ||
} | ||
} | ||
|
||
private async sendWatcherCommand(tabId: number, command: string) { | ||
try { | ||
await chrome.tabs.sendMessage(tabId, { | ||
action: RUNTIME_MESSAGE_ACF.DOM_WATCHER_COMMAND, | ||
command | ||
}); | ||
|
||
// Get updated status after command | ||
setTimeout(() => this.getWatcherStatus(tabId), 100); | ||
} catch (error) { | ||
console.error('DomWatcherDevTools: Error sending watcher command:', error); | ||
} | ||
} | ||
|
||
// Send log messages to DevTools | ||
public sendLogToDevTools(tabId: number, message: string, level: 'debug' | 'info' | 'warning' | 'error' = 'debug') { | ||
const port = this.devToolsPorts.get(tabId); | ||
if (port) { | ||
port.postMessage({ | ||
type: 'WATCHER_LOG', | ||
data: { message, level } | ||
}); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.