more globals
This commit is contained in:
+25
-4
@@ -11,6 +11,23 @@ import { getCdpUrl } from './utils.js'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
const usefulGlobals = {
|
||||
setTimeout,
|
||||
setInterval,
|
||||
clearTimeout,
|
||||
clearInterval,
|
||||
URL,
|
||||
URLSearchParams,
|
||||
fetch,
|
||||
Buffer,
|
||||
TextEncoder,
|
||||
TextDecoder,
|
||||
crypto,
|
||||
AbortController,
|
||||
AbortSignal,
|
||||
structuredClone,
|
||||
} as const
|
||||
|
||||
interface State {
|
||||
isConnected: boolean
|
||||
page: Page | null
|
||||
@@ -35,6 +52,8 @@ interface VMContext {
|
||||
import: (specifier: string) => Promise<any>
|
||||
}
|
||||
|
||||
type VMContextWithGlobals = VMContext & typeof usefulGlobals
|
||||
|
||||
const state: State = {
|
||||
isConnected: false,
|
||||
page: null,
|
||||
@@ -215,7 +234,7 @@ server.tool(
|
||||
throw new Error('accessibilitySnapshot is not available on this page')
|
||||
}
|
||||
|
||||
let vmContextObj: VMContext = {
|
||||
let vmContextObj: VMContextWithGlobals = {
|
||||
page,
|
||||
context,
|
||||
state,
|
||||
@@ -226,7 +245,7 @@ server.tool(
|
||||
|
||||
Object.keys(state).forEach(key => delete state[key])
|
||||
|
||||
const resetObj: VMContext = {
|
||||
const resetObj: VMContextWithGlobals = {
|
||||
page: newPage,
|
||||
context: newContext,
|
||||
state,
|
||||
@@ -234,14 +253,16 @@ server.tool(
|
||||
accessibilitySnapshot,
|
||||
resetPlaywright: vmContextObj.resetPlaywright,
|
||||
require,
|
||||
import: vmContextObj.import
|
||||
import: vmContextObj.import,
|
||||
...usefulGlobals
|
||||
}
|
||||
Object.keys(vmContextObj).forEach(key => delete (vmContextObj as any)[key])
|
||||
Object.assign(vmContextObj, resetObj)
|
||||
return { page: newPage, context: newContext }
|
||||
},
|
||||
require,
|
||||
import: (specifier: string) => import(specifier)
|
||||
import: (specifier: string) => import(specifier),
|
||||
...usefulGlobals
|
||||
}
|
||||
|
||||
const vmContext = vm.createContext(vmContextObj)
|
||||
|
||||
@@ -15,6 +15,7 @@ it will control an existing user Chrome window. The js code will be run in a sa
|
||||
- page, the first page the user opened and made it accessible to this MCP. do things like `page.url()` to see current url. assume the user wants you to use this page for your playwright code
|
||||
- require: node's require function to load CommonJS modules
|
||||
- import: async import function to dynamically import ES modules. for example `const fs = await import('node:fs')`
|
||||
- all standard Node.js globals: setTimeout, setInterval, clearTimeout, clearInterval, URL, URLSearchParams, fetch, Buffer, TextEncoder, TextDecoder, crypto, AbortController, AbortSignal, structuredClone
|
||||
|
||||
the chrome window can have more than one page. you can see other pages with `context.pages().find((p) => p.url().includes('localhost'))`. you can also open and close pages: `state.newPage = await context.newPage()`. store the page in state so that you can reuse it later
|
||||
|
||||
|
||||
Reference in New Issue
Block a user