feat(controller): fix SimulatorMast mem leak; add passthrough events
This commit is contained in:
@@ -5,7 +5,7 @@ import { isPageDark } from './checkDarkMode'
|
||||
import styles from './SimulatorMask.module.css'
|
||||
import cursorStyles from './cursor.module.css'
|
||||
|
||||
export class SimulatorMask {
|
||||
export class SimulatorMask extends EventTarget {
|
||||
shown: boolean = false
|
||||
wrapper = document.createElement('div')
|
||||
motion: Motion | null = null
|
||||
@@ -19,6 +19,8 @@ export class SimulatorMask {
|
||||
#targetCursorY = 0
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
|
||||
this.wrapper.id = 'page-agent-runtime_simulator-mask'
|
||||
this.wrapper.className = styles.wrapper
|
||||
this.wrapper.setAttribute('data-browser-use-ignore', 'true')
|
||||
@@ -74,13 +76,34 @@ export class SimulatorMask {
|
||||
|
||||
this.#moveCursorToTarget()
|
||||
|
||||
window.addEventListener('PageAgent::MovePointerTo', (event: Event) => {
|
||||
// global events
|
||||
// @note Mask should be isolated from the rest of the code.
|
||||
// Global events are easier to manage and cleanup.
|
||||
|
||||
const movePointerToListener = (event: Event) => {
|
||||
const { x, y } = (event as CustomEvent).detail
|
||||
this.setCursorPosition(x, y)
|
||||
})
|
||||
|
||||
window.addEventListener('PageAgent::ClickPointer', (event: Event) => {
|
||||
}
|
||||
const clickPointerListener = () => {
|
||||
this.triggerClickAnimation()
|
||||
}
|
||||
const enablePassThroughListener = () => {
|
||||
this.wrapper.style.pointerEvents = 'none'
|
||||
}
|
||||
const disablePassThroughListener = () => {
|
||||
this.wrapper.style.pointerEvents = 'auto'
|
||||
}
|
||||
|
||||
window.addEventListener('PageAgent::MovePointerTo', movePointerToListener)
|
||||
window.addEventListener('PageAgent::ClickPointer', clickPointerListener)
|
||||
window.addEventListener('PageAgent::EnablePassThrough', enablePassThroughListener)
|
||||
window.addEventListener('PageAgent::DisablePassThrough', disablePassThroughListener)
|
||||
|
||||
this.addEventListener('dispose', () => {
|
||||
window.removeEventListener('PageAgent::MovePointerTo', movePointerToListener)
|
||||
window.removeEventListener('PageAgent::ClickPointer', clickPointerListener)
|
||||
window.removeEventListener('PageAgent::EnablePassThrough', enablePassThroughListener)
|
||||
window.removeEventListener('PageAgent::DisablePassThrough', disablePassThroughListener)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -177,7 +200,9 @@ export class SimulatorMask {
|
||||
}
|
||||
|
||||
dispose() {
|
||||
console.log('dispose SimulatorMask')
|
||||
this.motion?.dispose()
|
||||
this.wrapper.remove()
|
||||
this.dispatchEvent(new Event('dispose'))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user