refactor(ext): tidy up background code
This commit is contained in:
@@ -105,3 +105,19 @@ export function handleTabControlMessage(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setupTabChangeEvents() {
|
||||||
|
chrome.tabs.onCreated.addListener((tab) => {
|
||||||
|
console.debug('[Background] Tab created', tab)
|
||||||
|
chrome.runtime.sendMessage({ type: 'TAB_CHANGE', action: 'created', payload: { tab } })
|
||||||
|
})
|
||||||
|
|
||||||
|
chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
|
||||||
|
console.debug('[Background] Tab removed', tabId, removeInfo)
|
||||||
|
chrome.runtime.sendMessage({
|
||||||
|
type: 'TAB_CHANGE',
|
||||||
|
action: 'removed',
|
||||||
|
payload: { tabId, removeInfo },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,20 +1,5 @@
|
|||||||
import { handlePageControlMessage } from '@/agent/RemotePageController.background'
|
import { handlePageControlMessage } from '@/agent/RemotePageController.background'
|
||||||
import { handleTabControlMessage } from '@/agent/TabsController.background'
|
import { handleTabControlMessage, setupTabChangeEvents } from '@/agent/TabsController.background'
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener((message, sender, sendResponse): true | undefined => {
|
|
||||||
if (message.type === 'TAB_CONTROL') {
|
|
||||||
return handleTabControlMessage(message, sender, sendResponse)
|
|
||||||
} else if (message.type === 'PAGE_CONTROL') {
|
|
||||||
return handlePageControlMessage(message, sender, sendResponse)
|
|
||||||
} else {
|
|
||||||
sendResponse({ error: 'Unknown message type' })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// Extension Setup
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
export default defineBackground(() => {
|
export default defineBackground(() => {
|
||||||
console.log('[Background] Service Worker started')
|
console.log('[Background] Service Worker started')
|
||||||
@@ -32,17 +17,20 @@ export default defineBackground(() => {
|
|||||||
|
|
||||||
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }).catch(() => {})
|
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }).catch(() => {})
|
||||||
|
|
||||||
// Tab change events
|
// message proxy
|
||||||
|
|
||||||
chrome.tabs.onCreated.addListener((tab) => {
|
chrome.runtime.onMessage.addListener((message, sender, sendResponse): true | undefined => {
|
||||||
chrome.runtime.sendMessage({ type: 'TAB_CHANGE', action: 'created', payload: { tab } })
|
if (message.type === 'TAB_CONTROL') {
|
||||||
|
return handleTabControlMessage(message, sender, sendResponse)
|
||||||
|
} else if (message.type === 'PAGE_CONTROL') {
|
||||||
|
return handlePageControlMessage(message, sender, sendResponse)
|
||||||
|
} else {
|
||||||
|
sendResponse({ error: 'Unknown message type' })
|
||||||
|
return
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
|
// tab change events
|
||||||
chrome.runtime.sendMessage({
|
|
||||||
type: 'TAB_CHANGE',
|
setupTabChangeEvents()
|
||||||
action: 'removed',
|
|
||||||
payload: { tabId, removeInfo },
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user