feat(ext): improve token validation and error handling
This commit is contained in:
@@ -35,6 +35,9 @@ export class MultiPageAgent extends PageAgentCore {
|
|||||||
chrome.storage.local.set({
|
chrome.storage.local.set({
|
||||||
isAgentRunning: false,
|
isAgentRunning: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// no need to dispose tabsController and pageController
|
||||||
|
// as they do not keep references
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,13 +13,22 @@ export default defineContentScript({
|
|||||||
|
|
||||||
// if auth token matches, expose agent to page
|
// if auth token matches, expose agent to page
|
||||||
chrome.storage.local.get('PageAgentExtUserAuthToken').then((result) => {
|
chrome.storage.local.get('PageAgentExtUserAuthToken').then((result) => {
|
||||||
if (!result.PageAgentExtUserAuthToken) return
|
// extension side token.
|
||||||
if (!localStorage.getItem('PageAgentExtUserAuthToken')) return
|
// @note this is isolated world. it is safe to assume user script cannot access it
|
||||||
if (localStorage.getItem('PageAgentExtUserAuthToken') !== result.PageAgentExtUserAuthToken)
|
const extToken = result.PageAgentExtUserAuthToken
|
||||||
return
|
if (!extToken) return
|
||||||
|
|
||||||
exposeAgentToPage()
|
// page side token
|
||||||
injectScript('/main-world.js')
|
const pageToken = localStorage.getItem('PageAgentExtUserAuthToken')
|
||||||
|
if (!pageToken) return
|
||||||
|
|
||||||
|
if (pageToken !== extToken) return
|
||||||
|
|
||||||
|
// add isolated world script
|
||||||
|
exposeAgentToPage().then(
|
||||||
|
// add main-world script
|
||||||
|
() => injectScript('/main-world.js')
|
||||||
|
)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -42,18 +51,45 @@ async function exposeAgentToPage() {
|
|||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'execute': {
|
case 'execute': {
|
||||||
if (!multiPageAgent) multiPageAgent = new MultiPageAgent(DEMO_CONFIG)
|
if (multiPageAgent && multiPageAgent.status === 'running') {
|
||||||
|
window.postMessage(
|
||||||
|
{
|
||||||
|
channel: 'PAGE_AGENT_EXT_RESPONSE',
|
||||||
|
id,
|
||||||
|
action: 'execute_result',
|
||||||
|
error: 'Agent is already running a task. Please wait until it finishes.',
|
||||||
|
},
|
||||||
|
'*'
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
multiPageAgent = new MultiPageAgent(DEMO_CONFIG)
|
||||||
|
|
||||||
|
const result = await multiPageAgent.execute(payload)
|
||||||
|
|
||||||
|
window.postMessage(
|
||||||
|
{
|
||||||
|
channel: 'PAGE_AGENT_EXT_RESPONSE',
|
||||||
|
id,
|
||||||
|
action: 'execute_result',
|
||||||
|
payload: result,
|
||||||
|
},
|
||||||
|
'*'
|
||||||
|
)
|
||||||
|
} catch (error) {
|
||||||
|
window.postMessage(
|
||||||
|
{
|
||||||
|
channel: 'PAGE_AGENT_EXT_RESPONSE',
|
||||||
|
id,
|
||||||
|
action: 'execute_result',
|
||||||
|
error: (error as Error).message,
|
||||||
|
},
|
||||||
|
'*'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const result = await multiPageAgent.execute(payload)
|
|
||||||
window.postMessage(
|
|
||||||
{
|
|
||||||
channel: 'PAGE_AGENT_EXT_RESPONSE',
|
|
||||||
id,
|
|
||||||
action: 'execute_result',
|
|
||||||
payload: result,
|
|
||||||
},
|
|
||||||
'*'
|
|
||||||
)
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export default defineUnlistedScript(() => {
|
|||||||
w.execute = async (task: string) => {
|
w.execute = async (task: string) => {
|
||||||
const id = getId()
|
const id = getId()
|
||||||
|
|
||||||
const promise = new Promise((resolve) => {
|
const promise = new Promise((resolve, reject) => {
|
||||||
function handleMessage(e: MessageEvent) {
|
function handleMessage(e: MessageEvent) {
|
||||||
const data = e.data
|
const data = e.data
|
||||||
if (typeof data !== 'object' || data === null) return
|
if (typeof data !== 'object' || data === null) return
|
||||||
@@ -19,7 +19,12 @@ export default defineUnlistedScript(() => {
|
|||||||
if (data.id !== id) return
|
if (data.id !== id) return
|
||||||
|
|
||||||
window.removeEventListener('message', handleMessage)
|
window.removeEventListener('message', handleMessage)
|
||||||
resolve(data.payload)
|
|
||||||
|
if (data.error) {
|
||||||
|
reject(new Error(data.error))
|
||||||
|
} else {
|
||||||
|
resolve(data.payload)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener('message', handleMessage)
|
window.addEventListener('message', handleMessage)
|
||||||
|
|||||||
Reference in New Issue
Block a user