chore: add isPageAgentMessage to message

This commit is contained in:
Simon
2026-01-26 18:55:54 +08:00
parent 969dd31db5
commit f18b756390
5 changed files with 21 additions and 34 deletions

View File

@@ -75,7 +75,7 @@ export default defineConfig([
'react-dom/no-missing-button-type': 'off', 'react-dom/no-missing-button-type': 'off',
'react-x/no-nested-component-definitions': 'off', 'react-x/no-nested-component-definitions': 'off',
'@typescript-eslint/prefer-optional-chain': 'off', '@typescript-eslint/prefer-optional-chain': 'off',
'@typescript-eslint/use-unknown-in-catch-callback-variable': 'warn', '@typescript-eslint/use-unknown-in-catch-callback-variable': 'off',
'@typescript-eslint/no-unnecessary-type-parameters': 'off', '@typescript-eslint/no-unnecessary-type-parameters': 'off',
// 'require-await': 'off', // 'require-await': 'off',

View File

@@ -65,6 +65,7 @@ async function handleRPCCall(msg: RPCCallMessage): Promise<void> {
// Create message for content script // Create message for content script
const csMessage: CSRPCMessage = { const csMessage: CSRPCMessage = {
isPageAgentMessage: true,
type: 'cs:rpc', type: 'cs:rpc',
id, id,
method, method,
@@ -77,6 +78,7 @@ async function handleRPCCall(msg: RPCCallMessage): Promise<void> {
// Forward response back to sidepanel // Forward response back to sidepanel
const response: RPCResponseMessage = { const response: RPCResponseMessage = {
isPageAgentMessage: true,
type: 'rpc:response', type: 'rpc:response',
id, id,
success: true, success: true,
@@ -86,6 +88,7 @@ async function handleRPCCall(msg: RPCCallMessage): Promise<void> {
} catch (error) { } catch (error) {
// Forward error back to sidepanel // Forward error back to sidepanel
const response: RPCResponseMessage = { const response: RPCResponseMessage = {
isPageAgentMessage: true,
type: 'rpc:response', type: 'rpc:response',
id, id,
success: false, success: false,
@@ -117,6 +120,7 @@ async function handleCSQuery(
// Forward response back to content script // Forward response back to content script
if (sender.tab?.id) { if (sender.tab?.id) {
const queryResponse: QueryResponseMessage = { const queryResponse: QueryResponseMessage = {
isPageAgentMessage: true,
type: 'query:response', type: 'query:response',
id, id,
result: response, result: response,
@@ -127,6 +131,7 @@ async function handleCSQuery(
// Sidepanel not open or no response, return default // Sidepanel not open or no response, return default
if (sender.tab?.id) { if (sender.tab?.id) {
const queryResponse: QueryResponseMessage = { const queryResponse: QueryResponseMessage = {
isPageAgentMessage: true,
type: 'query:response', type: 'query:response',
id, id,
result: queryType === 'shouldShowMask' ? false : null, result: queryType === 'shouldShowMask' ? false : null,
@@ -145,6 +150,7 @@ async function handleCSQuery(
*/ */
chrome.tabs.onRemoved.addListener((tabId) => { chrome.tabs.onRemoved.addListener((tabId) => {
const message: TabEventMessage = { const message: TabEventMessage = {
isPageAgentMessage: true,
type: 'tab:event', type: 'tab:event',
id: generateMessageId(), id: generateMessageId(),
eventType: 'removed', eventType: 'removed',
@@ -163,6 +169,7 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
if (!changeInfo.status) return if (!changeInfo.status) return
const message: TabEventMessage = { const message: TabEventMessage = {
isPageAgentMessage: true,
type: 'tab:event', type: 'tab:event',
id: generateMessageId(), id: generateMessageId(),
eventType: 'updated', eventType: 'updated',
@@ -182,6 +189,7 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
*/ */
chrome.tabs.onActivated.addListener((activeInfo) => { chrome.tabs.onActivated.addListener((activeInfo) => {
const message: TabEventMessage = { const message: TabEventMessage = {
isPageAgentMessage: true,
type: 'tab:event', type: 'tab:event',
id: generateMessageId(), id: generateMessageId(),
eventType: 'activated', eventType: 'activated',
@@ -202,6 +210,7 @@ chrome.windows.onFocusChanged.addListener((windowId) => {
// windowId is chrome.windows.WINDOW_ID_NONE (-1) when all windows lose focus // windowId is chrome.windows.WINDOW_ID_NONE (-1) when all windows lose focus
const focused = windowId !== chrome.windows.WINDOW_ID_NONE const focused = windowId !== chrome.windows.WINDOW_ID_NONE
const message: TabEventMessage = { const message: TabEventMessage = {
isPageAgentMessage: true,
type: 'tab:event', type: 'tab:event',
id: generateMessageId(), id: generateMessageId(),
eventType: 'windowFocusChanged', eventType: 'windowFocusChanged',

View File

@@ -11,12 +11,7 @@
*/ */
import { PageController } from '@page-agent/page-controller' import { PageController } from '@page-agent/page-controller'
import type { import type { CSQueryMessage, CSRPCMessage, QueryResponseMessage } from '../messaging/protocol'
CSQueryMessage,
CSRPCMessage,
QueryResponseMessage,
RPCMethod,
} from '../messaging/protocol'
import { generateMessageId, isExtensionMessage } from '../messaging/protocol' import { generateMessageId, isExtensionMessage } from '../messaging/protocol'
const DEBUG_PREFIX = '[ContentScript]' const DEBUG_PREFIX = '[ContentScript]'
@@ -86,6 +81,7 @@ async function queryShouldShowMask(getController: () => PageController): Promise
const queryId = generateMessageId() const queryId = generateMessageId()
const queryMessage: CSQueryMessage = { const queryMessage: CSQueryMessage = {
isPageAgentMessage: true,
type: 'cs:query', type: 'cs:query',
id: queryId, id: queryId,
queryType: 'shouldShowMask', queryType: 'shouldShowMask',
@@ -198,7 +194,7 @@ function registerRPCHandler(
* Handle an RPC call * Handle an RPC call
*/ */
async function handleRPCCall( async function handleRPCCall(
method: RPCMethod, method: string,
args: unknown[], args: unknown[],
getController: () => PageController, getController: () => PageController,
getControllerIfExists: () => PageController | null, getControllerIfExists: () => PageController | null,

View File

@@ -61,6 +61,7 @@ type MessageType =
/** Base message structure */ /** Base message structure */
interface BaseMessage { interface BaseMessage {
isPageAgentMessage: true
type: MessageType type: MessageType
id: string // Unique message ID for request-response matching id: string // Unique message ID for request-response matching
} }
@@ -69,28 +70,11 @@ interface BaseMessage {
// RPC Messages (SidePanel ↔ SW ↔ ContentScript) // RPC Messages (SidePanel ↔ SW ↔ ContentScript)
// ============================================================================ // ============================================================================
/** RPC method names matching PageController interface */
export type RPCMethod =
| 'getCurrentUrl'
| 'getLastUpdateTime'
| 'getBrowserState'
| 'updateTree'
| 'cleanUpHighlights'
| 'clickElement'
| 'inputText'
| 'selectOption'
| 'scroll'
| 'scrollHorizontally'
| 'executeJavascript'
| 'showMask'
| 'hideMask'
| 'dispose'
/** SidePanel → SW: Request to call PageController method */ /** SidePanel → SW: Request to call PageController method */
export interface RPCCallMessage extends BaseMessage { export interface RPCCallMessage extends BaseMessage {
type: 'rpc:call' type: 'rpc:call'
tabId: number tabId: number
method: RPCMethod method: string
args: unknown[] args: unknown[]
} }
@@ -105,7 +89,7 @@ export interface RPCResponseMessage extends BaseMessage {
/** SW → ContentScript: Forwarded RPC call */ /** SW → ContentScript: Forwarded RPC call */
export interface CSRPCMessage extends BaseMessage { export interface CSRPCMessage extends BaseMessage {
type: 'cs:rpc' type: 'cs:rpc'
method: RPCMethod method: string
args: unknown[] args: unknown[]
} }
@@ -179,9 +163,7 @@ export function isExtensionMessage(msg: unknown): msg is ExtensionMessage {
return ( return (
typeof msg === 'object' && typeof msg === 'object' &&
msg !== null && msg !== null &&
'type' in msg && 'isPageAgentMessage' in msg &&
'id' in msg && (msg as any).isPageAgentMessage === true
typeof (msg as ExtensionMessage).type === 'string' &&
typeof (msg as ExtensionMessage).id === 'string'
) )
} }

View File

@@ -10,7 +10,6 @@ import {
type ActionResult, type ActionResult,
type BrowserState, type BrowserState,
type RPCCallMessage, type RPCCallMessage,
type RPCMethod,
type RPCResponseMessage, type RPCResponseMessage,
type ScrollHorizontallyOptions, type ScrollHorizontallyOptions,
type ScrollOptions, type ScrollOptions,
@@ -107,11 +106,12 @@ export class RPCError extends Error {
/** /**
* Make a single RPC call (no retry) * Make a single RPC call (no retry)
*/ */
async function callOnce(tabId: number, method: RPCMethod, args: unknown[]): Promise<unknown> { async function callOnce(tabId: number, method: string, args: unknown[]): Promise<unknown> {
ensureResponseListener() ensureResponseListener()
const id = generateMessageId() const id = generateMessageId()
const message: RPCCallMessage = { const message: RPCCallMessage = {
isPageAgentMessage: true,
type: 'rpc:call', type: 'rpc:call',
id, id,
tabId, tabId,
@@ -138,7 +138,7 @@ async function callOnce(tabId: number, method: RPCMethod, args: unknown[]): Prom
/** /**
* Make an RPC call with retry logic * Make an RPC call with retry logic
*/ */
async function call(tabId: number, method: RPCMethod, args: unknown[]): Promise<unknown> { async function call(tabId: number, method: string, args: unknown[]): Promise<unknown> {
let lastError: Error | null = null let lastError: Error | null = null
for (let attempt = 0; attempt < RPC_CONFIG.maxRetries; attempt++) { for (let attempt = 0; attempt < RPC_CONFIG.maxRetries; attempt++) {