feat: add error handling for content scripts
This commit is contained in:
@@ -244,5 +244,16 @@ function registerContentScriptHandlers(): void {
|
||||
return shouldShow
|
||||
})
|
||||
|
||||
// Handle content script errors - broadcast to sidepanel for user visibility
|
||||
contentScriptQuery.onMessage('content:error', async ({ data }) => {
|
||||
console.error('[PageAgentExt] Content script error:', data.message, 'on', data.url)
|
||||
// Broadcast error to sidepanel
|
||||
const errorEvent: HistoricalEvent = {
|
||||
type: 'error',
|
||||
message: `Content script error on ${data.url}: ${data.message}`,
|
||||
}
|
||||
eventBroadcaster.history([errorEvent])
|
||||
})
|
||||
|
||||
console.log('[PageAgentExt] Content script handlers registered')
|
||||
}
|
||||
|
||||
@@ -16,15 +16,28 @@ export default defineContentScript({
|
||||
runAt: 'document_idle',
|
||||
|
||||
async main() {
|
||||
console.log('[PageAgentExt] Content script loaded')
|
||||
console.log('[PageAgentExt] Content script loaded on', window.location.href)
|
||||
|
||||
// Lazy-initialized controller - created on demand, disposed between tasks
|
||||
let controller: PageController | null = null
|
||||
let initError: Error | null = null
|
||||
|
||||
function getController(): PageController {
|
||||
// Re-throw init error if controller creation previously failed
|
||||
if (initError) {
|
||||
throw initError
|
||||
}
|
||||
if (!controller) {
|
||||
controller = new PageController({ enableMask: true })
|
||||
console.log('[PageAgentExt] PageController created')
|
||||
try {
|
||||
controller = new PageController({ enableMask: true })
|
||||
console.log('[PageAgentExt] PageController created')
|
||||
} catch (error) {
|
||||
initError = error instanceof Error ? error : new Error(String(error))
|
||||
console.error('[PageAgentExt] Failed to create PageController:', initError)
|
||||
// Report error to background
|
||||
reportError(initError.message)
|
||||
throw initError
|
||||
}
|
||||
}
|
||||
return controller
|
||||
}
|
||||
@@ -36,6 +49,7 @@ export default defineContentScript({
|
||||
() => {
|
||||
controller?.dispose()
|
||||
controller = null
|
||||
initError = null // Clear error on dispose to allow retry
|
||||
console.log('[PageAgentExt] PageController disposed')
|
||||
}
|
||||
)
|
||||
@@ -66,6 +80,17 @@ export default defineContentScript({
|
||||
},
|
||||
})
|
||||
|
||||
/**
|
||||
* Report content script error to background for user visibility
|
||||
*/
|
||||
function reportError(message: string): void {
|
||||
contentScriptQuery
|
||||
.sendMessage('content:error', { message, url: window.location.href })
|
||||
.catch(() => {
|
||||
// Silently ignore if background is not available
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all RPC message handlers for PageController methods
|
||||
*/
|
||||
|
||||
@@ -140,6 +140,8 @@ export interface AgentCommandProtocol {
|
||||
export interface ContentScriptQueryProtocol {
|
||||
/** Check if there's an active task for this tab, returns true if mask should be shown */
|
||||
'content:shouldShowMask': () => boolean
|
||||
/** Report content script initialization error to background */
|
||||
'content:error': (error: { message: string; url: string }) => void
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
@@ -40,11 +40,5 @@ export default defineConfig({
|
||||
side_panel: {
|
||||
default_path: 'sidepanel/index.html',
|
||||
},
|
||||
web_accessible_resources: [
|
||||
{
|
||||
resources: ['main-world.js'],
|
||||
matches: ['<all_urls>'],
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user