2026-01-27 17:21:32 +08:00
|
|
|
import { handlePageControlMessage } from '@/agent/RemotePageController.background'
|
|
|
|
|
import { handleTabControlMessage } from '@/agent/TabsController.background'
|
|
|
|
|
|
|
|
|
|
function handleUtilsMessage(
|
|
|
|
|
message: { type: 'UTILS'; action: string; payload: any },
|
|
|
|
|
sender: chrome.runtime.MessageSender,
|
|
|
|
|
sendResponse: (response: unknown) => void
|
|
|
|
|
): boolean {
|
|
|
|
|
const { action, payload } = message
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
|
case 'get_tab_info': {
|
|
|
|
|
chrome.tabs
|
|
|
|
|
.get(payload.tabId)
|
|
|
|
|
.then((tab) => {
|
|
|
|
|
const result = { title: tab.title || '', url: tab.url || '' }
|
|
|
|
|
sendResponse(result)
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
sendResponse({ error: error instanceof Error ? error.message : String(error) })
|
|
|
|
|
})
|
|
|
|
|
return true // async response
|
2026-01-24 19:29:27 +08:00
|
|
|
}
|
2026-01-26 21:03:51 +08:00
|
|
|
|
2026-01-27 17:21:32 +08:00
|
|
|
default:
|
|
|
|
|
sendResponse({ error: `Unknown TAB_CONTROL action: ${action}` })
|
|
|
|
|
return false
|
2026-01-20 17:36:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 17:21:32 +08:00
|
|
|
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|
|
|
|
if (message.type === 'TAB_CONTROL') {
|
|
|
|
|
return handleTabControlMessage(message, sender, sendResponse)
|
|
|
|
|
} else if (message.type === 'PAGE_CONTROL') {
|
|
|
|
|
return handlePageControlMessage(message, sender, sendResponse)
|
|
|
|
|
} else if (message.type !== 'UTILS') {
|
|
|
|
|
return handleUtilsMessage(message, sender, sendResponse)
|
|
|
|
|
} else {
|
|
|
|
|
sendResponse({ error: 'Unknown message type' })
|
|
|
|
|
return false
|
|
|
|
|
}
|
2026-01-24 19:37:46 +08:00
|
|
|
})
|
|
|
|
|
|
2026-01-24 19:29:27 +08:00
|
|
|
// ============================================================================
|
|
|
|
|
// Extension Setup
|
|
|
|
|
// ============================================================================
|
2026-01-21 18:46:50 +08:00
|
|
|
|
2026-01-24 19:29:27 +08:00
|
|
|
export default defineBackground(() => {
|
2026-01-26 21:03:51 +08:00
|
|
|
console.log('[Background] Service Worker started')
|
2026-01-21 18:46:50 +08:00
|
|
|
|
2026-01-26 21:03:51 +08:00
|
|
|
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }).catch(() => {})
|
2026-01-24 19:29:27 +08:00
|
|
|
})
|