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 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 {
|
interface State {
|
||||||
isConnected: boolean
|
isConnected: boolean
|
||||||
page: Page | null
|
page: Page | null
|
||||||
@@ -35,6 +52,8 @@ interface VMContext {
|
|||||||
import: (specifier: string) => Promise<any>
|
import: (specifier: string) => Promise<any>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type VMContextWithGlobals = VMContext & typeof usefulGlobals
|
||||||
|
|
||||||
const state: State = {
|
const state: State = {
|
||||||
isConnected: false,
|
isConnected: false,
|
||||||
page: null,
|
page: null,
|
||||||
@@ -215,7 +234,7 @@ server.tool(
|
|||||||
throw new Error('accessibilitySnapshot is not available on this page')
|
throw new Error('accessibilitySnapshot is not available on this page')
|
||||||
}
|
}
|
||||||
|
|
||||||
let vmContextObj: VMContext = {
|
let vmContextObj: VMContextWithGlobals = {
|
||||||
page,
|
page,
|
||||||
context,
|
context,
|
||||||
state,
|
state,
|
||||||
@@ -226,7 +245,7 @@ server.tool(
|
|||||||
|
|
||||||
Object.keys(state).forEach(key => delete state[key])
|
Object.keys(state).forEach(key => delete state[key])
|
||||||
|
|
||||||
const resetObj: VMContext = {
|
const resetObj: VMContextWithGlobals = {
|
||||||
page: newPage,
|
page: newPage,
|
||||||
context: newContext,
|
context: newContext,
|
||||||
state,
|
state,
|
||||||
@@ -234,14 +253,16 @@ server.tool(
|
|||||||
accessibilitySnapshot,
|
accessibilitySnapshot,
|
||||||
resetPlaywright: vmContextObj.resetPlaywright,
|
resetPlaywright: vmContextObj.resetPlaywright,
|
||||||
require,
|
require,
|
||||||
import: vmContextObj.import
|
import: vmContextObj.import,
|
||||||
|
...usefulGlobals
|
||||||
}
|
}
|
||||||
Object.keys(vmContextObj).forEach(key => delete (vmContextObj as any)[key])
|
Object.keys(vmContextObj).forEach(key => delete (vmContextObj as any)[key])
|
||||||
Object.assign(vmContextObj, resetObj)
|
Object.assign(vmContextObj, resetObj)
|
||||||
return { page: newPage, context: newContext }
|
return { page: newPage, context: newContext }
|
||||||
},
|
},
|
||||||
require,
|
require,
|
||||||
import: (specifier: string) => import(specifier)
|
import: (specifier: string) => import(specifier),
|
||||||
|
...usefulGlobals
|
||||||
}
|
}
|
||||||
|
|
||||||
const vmContext = vm.createContext(vmContextObj)
|
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
|
- 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
|
- 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')`
|
- 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
|
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