4e87940127
> If multiple listeners are registered for onMessage, > only the first listener to respond, reject, or throw > an error will affect the sender; all other listeners > will run, but their results will be ignored.
36 lines
846 B
TypeScript
36 lines
846 B
TypeScript
/**
|
|
* background logics for RemotePageController
|
|
* - redirect messages from RemotePageController(Agent, extension pages) to ContentScript
|
|
*/
|
|
|
|
export function handlePageControlMessage(
|
|
message: { type: 'PAGE_CONTROL'; action: string; payload: any; targetTabId: number },
|
|
sender: chrome.runtime.MessageSender,
|
|
sendResponse: (response: unknown) => void
|
|
): true | undefined {
|
|
const { action, payload, targetTabId } = message
|
|
|
|
if (action === 'get_my_tab_id') {
|
|
sendResponse({ tabId: sender.tab?.id || null })
|
|
return
|
|
}
|
|
|
|
chrome.tabs
|
|
.sendMessage(targetTabId, {
|
|
type: 'PAGE_CONTROL',
|
|
action,
|
|
payload,
|
|
})
|
|
.then((result) => {
|
|
sendResponse(result)
|
|
})
|
|
.catch((error) => {
|
|
sendResponse({
|
|
success: false,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
})
|
|
})
|
|
|
|
return true // async response
|
|
}
|