fix(ext): sw calling robust

This commit is contained in:
Simon
2026-01-28 14:13:28 +08:00
parent 0cad8a1159
commit c80de93d3b
3 changed files with 5 additions and 35 deletions

View File

@@ -53,6 +53,10 @@ export function initPageController() {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type !== 'PAGE_CONTROL') { if (message.type !== 'PAGE_CONTROL') {
sendResponse({
success: false,
error: `[RemotePageController.ContentScript]: Invalid message type: ${message.type}`,
})
return return
} }

View File

@@ -8,11 +8,6 @@ export function handleTabControlMessage(
sender: chrome.runtime.MessageSender, sender: chrome.runtime.MessageSender,
sendResponse: (response: unknown) => void sendResponse: (response: unknown) => void
): boolean { ): boolean {
if (message.type !== 'TAB_CONTROL') {
sendResponse({ error: 'Invalid message type' })
return false
}
const { action, payload } = message const { action, payload } = message
switch (action as TabAction) { switch (action as TabAction) {

View File

@@ -1,43 +1,14 @@
import { handlePageControlMessage } from '@/agent/RemotePageController.background' import { handlePageControlMessage } from '@/agent/RemotePageController.background'
import { handleTabControlMessage } from '@/agent/TabsController.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
}
default:
sendResponse({ error: `Unknown TAB_CONTROL action: ${action}` })
return false
}
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'TAB_CONTROL') { if (message.type === 'TAB_CONTROL') {
return handleTabControlMessage(message, sender, sendResponse) return handleTabControlMessage(message, sender, sendResponse)
} else if (message.type === 'PAGE_CONTROL') { } else if (message.type === 'PAGE_CONTROL') {
return handlePageControlMessage(message, sender, sendResponse) return handlePageControlMessage(message, sender, sendResponse)
} else if (message.type !== 'UTILS') {
return handleUtilsMessage(message, sender, sendResponse)
} else { } else {
sendResponse({ error: 'Unknown message type' }) sendResponse({ error: 'Unknown message type' })
return false return
} }
}) })