@@ -24,7 +24,10 @@ import {
2424 ExpandedMutationObserver ,
2525} from 'browser/types'
2626import initFindSelect from './find-select'
27- import { PluginPreloadOutputShape , RecordNewCommandInput } from '@seleniumhq/side-api'
27+ import {
28+ PluginPreloadOutputShape ,
29+ RecordNewCommandInput ,
30+ } from '@seleniumhq/side-api'
2831import LocatorBuilders from './locator-builders'
2932
3033export interface RecordingState {
@@ -56,41 +59,21 @@ export default class Recorder {
5659 this . getFrameLocation = this . getFrameLocation . bind ( this )
5760 this . setWindowHandle = this . setWindowHandle . bind ( this )
5861 // @ts -expect-error
59- this . window . addEventListener ( 'message' , this . setWindowHandle )
60- this . window . sideAPI . recorder . onFrameRecalculate . addListener (
61- this . getFrameLocation
62- )
63-
64- this . window . addEventListener ( 'beforeunload' , ( ) => {
65- try {
66- this . window . sideAPI . recorder . onFrameRecalculate . removeListener (
67- this . getFrameLocation
68- )
69- } catch ( e ) {
70- // ignore
71- }
72- } )
73- // @ts -expect-error
7462 this . recordingState = { }
7563 this . addRecorderTracingAttribute ( )
7664 initFindSelect ( )
77- this . window . sideAPI . recorder . onLocatorOrderChanged . addListener (
78- LocatorBuilders . setPreferredOrder
79- )
8065 // e.g., once on load
8166 this . getFrameLocation ( )
8267
8368 this . window . sideAPI . recorder . getWinHandleId ( ) . then ( ( id ) => {
8469 this . winHandleId = id
8570 } )
86-
8771 handlers . forEach ( ( handler ) => {
8872 this . addEventHandler ( ...handler )
8973 } )
9074 observers . forEach ( ( observer ) => {
9175 this . addMutationObserver ( ...observer )
9276 } )
93- this . attach ( )
9477 }
9578
9679 winHandleId : string = ''
@@ -120,7 +103,7 @@ export default class Recorder {
120103 value,
121104 insertBeforeLastCommand,
122105 frameLocation : actualFrameLocation || this . frameLocation ,
123- winHandleId : this . winHandleId
106+ winHandleId : this . winHandleId ,
124107 }
125108 const plugins = this . plugins
126109 for ( let i = 0 , ii = plugins . length ; i !== ii ; i ++ ) {
@@ -172,6 +155,14 @@ export default class Recorder {
172155
173156 attach ( ) {
174157 if ( ! this . attached ) {
158+ // @ts -expect-error
159+ this . window . addEventListener ( 'message' , this . setWindowHandle )
160+ this . window . sideAPI . recorder . onFrameRecalculate . addListener (
161+ this . getFrameLocation
162+ )
163+ this . window . sideAPI . recorder . onLocatorOrderChanged . addListener (
164+ LocatorBuilders . setPreferredOrder
165+ )
175166 for ( let eventKey in this . eventHandlers ) {
176167 const eventInfo = this . parseEventKey ( eventKey )
177168 const eventName = eventInfo . eventName
@@ -213,6 +204,14 @@ export default class Recorder {
213204 }
214205
215206 detach ( ) {
207+ // @ts -expect-error
208+ this . window . removeEventListener ( 'message' , this . setWindowHandle )
209+ this . window . sideAPI . recorder . onFrameRecalculate . removeListener (
210+ this . getFrameLocation
211+ )
212+ this . window . sideAPI . recorder . onLocatorOrderChanged . removeListener (
213+ LocatorBuilders . setPreferredOrder
214+ )
216215 for ( let eventKey in this . eventListeners ) {
217216 const eventInfo = this . parseEventKey ( eventKey )
218217 const eventName = eventInfo . eventName
0 commit comments