16
16
// under the License.
17
17
18
18
const logInspector = require ( '../bidi/logInspector' )
19
+ const scriptManager = require ( '../bidi//scriptManager' )
20
+ const { ArgumentValue } = require ( '../bidi/argumentValue' )
21
+ const { LocalValue, ChannelValue } = require ( '../bidi/protocolValue' )
22
+ const fs = require ( 'node:fs' )
23
+ const path = require ( 'node:path' )
24
+ const by = require ( './by' )
19
25
20
26
class Script {
21
27
#driver
22
28
#logInspector
29
+ #script
23
30
24
31
constructor ( driver ) {
25
32
this . #driver = driver
@@ -38,6 +45,13 @@ class Script {
38
45
this . #logInspector = await logInspector ( this . #driver)
39
46
}
40
47
48
+ async #initScript( ) {
49
+ if ( this . #script !== undefined ) {
50
+ return
51
+ }
52
+ this . #script = await scriptManager ( [ ] , this . #driver)
53
+ }
54
+
41
55
async addJavaScriptErrorHandler ( callback ) {
42
56
await this . #init( )
43
57
return await this . #logInspector. onJavascriptException ( callback )
@@ -58,6 +72,46 @@ class Script {
58
72
59
73
await this . #logInspector. removeCallback ( id )
60
74
}
75
+
76
+ async addDomMutationHandler ( callback ) {
77
+ await this . #initScript( )
78
+
79
+ let argumentValues = [ ]
80
+ let value = new ArgumentValue ( LocalValue . createChannelValue ( new ChannelValue ( 'channel_name' ) ) )
81
+ argumentValues . push ( value )
82
+
83
+ const filePath = path . join ( __dirname , 'atoms' , 'bidi-mutation-listener.js' )
84
+
85
+ let mutationListener = fs . readFileSync ( filePath , 'utf-8' ) . toString ( )
86
+ await this . #script. addPreloadScript ( mutationListener , argumentValues )
87
+
88
+ let id = await this . #script. onMessage ( async ( message ) => {
89
+ let payload = JSON . parse ( message [ 'data' ] [ 'value' ] )
90
+ let elements = await this . #driver. findElements ( {
91
+ css : '*[data-__webdriver_id=' + by . escapeCss ( payload [ 'target' ] ) + ']' ,
92
+ } )
93
+
94
+ if ( elements . length === 0 ) {
95
+ return
96
+ }
97
+
98
+ let event = {
99
+ element : elements [ 0 ] ,
100
+ attribute_name : payload [ 'name' ] ,
101
+ current_value : payload [ 'value' ] ,
102
+ old_value : payload [ 'oldValue' ] ,
103
+ }
104
+ callback ( event )
105
+ } )
106
+
107
+ return id
108
+ }
109
+
110
+ async removeDomMutationHandler ( id ) {
111
+ await this . #initScript( )
112
+
113
+ await this . #script. removeCallback ( id )
114
+ }
61
115
}
62
116
63
117
module . exports = Script
0 commit comments