11/* ZeuZ Start the recording function */
2+ browserAppData = chrome || browser ;
23class Recorder {
34
45 /* exq initial time */
@@ -11,6 +12,14 @@ class Recorder {
1112 frameLocation : this . frameLocation
1213 } ) . catch ( function ( reason ) {
1314 } ) ;
15+ this . recorded_actions = [ ] ;
16+ this . idx = 0 ;
17+ // Convert to the zeuz defined action names
18+ this . action_name_convert = {
19+ type : "text" ,
20+ open : "go to link" ,
21+ doubleClick : "double click" ,
22+ }
1423 }
1524
1625 /* Parse the key */
@@ -40,8 +49,81 @@ class Recorder {
4049 }
4150
4251 /* Recorder */
52+ async fetchAIData ( target , idx , command , value ) {
53+ var html = document . createElement ( 'html' ) ;
54+ html . innerHTML = document . documentElement . outerHTML ;
55+
56+ for ( let each of target ) if ( each [ 1 ] == 'xpath:position' ) { var xpath = each [ 0 ] ; break ; }
57+ var xPathResult = document . evaluate ( xpath , html ) ;
58+ if ( xPathResult ) var main_elem = xPathResult . iterateNext ( ) ;
59+ else return ;
60+
61+ main_elem . setAttribute ( 'zeuz' , 'aiplugin' ) ;
62+ console . log ( main_elem . hasAttribute ( 'zeuz' ) , main_elem ) ;
63+
64+ // get all <head> elements from html
65+ var elements = html . getElementsByTagName ( 'head' ) ;
66+ while ( elements [ 0 ] )
67+ elements [ 0 ] . parentNode . removeChild ( elements [ 0 ] )
68+
69+ // get all <script> elements from html
70+ var elements = html . getElementsByTagName ( 'script' ) ;
71+ while ( elements [ 0 ] )
72+ elements [ 0 ] . parentNode . removeChild ( elements [ 0 ] )
73+
74+ // get all <style> elements from html
75+ var elements = html . getElementsByTagName ( 'style' ) ;
76+ while ( elements [ 0 ] )
77+ elements [ 0 ] . parentNode . removeChild ( elements [ 0 ] )
78+
79+ var xPathResult = document . evaluate ( xpath , document ) ;
80+ if ( xPathResult ) console . log ( 'doc true' )
81+ else console . log ( 'doc false' ) ;
82+
83+ var xPathResult = document . evaluate ( xpath , html ) ;
84+ if ( xPathResult ) console . log ( 'html true' )
85+ else console . log ( 'html false' ) ;
86+
87+ var dataj = {
88+ "page_src" : html . outerHTML ,
89+ "action_name" : command ,
90+ "action_type" : "selenium" ,
91+ "action_value" : value ,
92+ "source" : "web" ,
93+ } ;
94+ var data = JSON . stringify ( dataj ) ;
95+
96+ browserAppData . runtime . sendMessage ( {
97+ apiName : 'ai_single_action' ,
98+ data : data ,
99+ dataj : dataj ,
100+ } ,
101+ response => {
102+ response [ 0 ] . short . value = value ;
103+ if ( value ) response [ 0 ] . data_set [ response [ 0 ] . data_set . length - 1 ] [ response [ 0 ] . data_set [ 0 ] . length - 1 ] = value ;
104+ this . recorded_actions [ idx ] = {
105+ action : response [ 0 ] . short . action ,
106+ data_list : [ response [ 0 ] . short . value ] ,
107+ element : response [ 0 ] . short . element ,
108+ is_disable : false ,
109+ name : response [ 0 ] . name ,
110+ value : response [ 0 ] . short . value ,
111+ main : response [ 0 ] . data_set ,
112+ xpath : response [ 0 ] . xpath ,
113+ } ;
114+ console . log ( idx ) ;
115+ console . log ( this . recorded_actions ) ;
116+ browserAppData . storage . local . set ( {
117+ recorded_actions : this . recorded_actions ,
118+ } )
119+ }
120+ ) ;
121+ }
43122 record ( command , target , value , insertBeforeLastCommand , actualFrameLocation ) {
44123 let self = this ;
124+ if ( command == 'doubleClick' )
125+ console . log ( 'doubleClick' )
126+ if ( Object . keys ( this . action_name_convert ) . includes ( command ) ) command = this . action_name_convert [ command ]
45127 console . log ( "... Action recorder start" ) ;
46128 let signal = {
47129 command : command ,
@@ -50,6 +132,34 @@ class Recorder {
50132 insertBeforeLastCommand : insertBeforeLastCommand ,
51133 frameLocation : ( actualFrameLocation != undefined ) ? actualFrameLocation : this . frameLocation ,
52134 } ;
135+ this . idx += 1 ;
136+ if ( this . recorded_actions . length === 0 ) {
137+ this . recorded_actions [ 0 ] = {
138+ action : 'go to link' ,
139+ data_list : [ window . location . href ] ,
140+ element : "" ,
141+ is_disable : false ,
142+ name : `Open ${ ( window . location . href . length > 30 ) ? window . location . href . slice ( 0 , 30 ) + '...' : window . location . href } ` ,
143+ value : window . location . href ,
144+ main : [ [ 'go to link' , 'selenium action' , window . location . href ] ] ,
145+ xpath : "" ,
146+ } ;
147+ this . idx += 1 ;
148+ }
149+ if ( this . recorded_actions . length > 0 && this . recorded_actions [ 0 ] . action != 'go to link' ) {
150+ this . recorded_actions . unshift ( {
151+ action : 'go to link' ,
152+ data_list : [ window . location . href ] ,
153+ element : "" ,
154+ is_disable : false ,
155+ name : `Open ${ ( window . location . href . length > 30 ) ? window . location . href . slice ( 0 , 30 ) + '...' : window . location . href } ` ,
156+ value : window . location . href ,
157+ main : [ [ 'go to link' , 'selenium action' , window . location . href ] ] ,
158+ xpath : "" ,
159+ } ) ;
160+ this . idx += 1 ;
161+ }
162+ this . fetchAIData ( target , this . idx - 1 , command , value )
53163 console . log ( signal ) ;
54164 browser . runtime . sendMessage ( signal ) . catch ( function ( reason ) {
55165 console . log ( reason ) ;
@@ -58,10 +168,21 @@ class Recorder {
58168
59169 /* attach */
60170 attach ( ) {
171+
61172 console . log ( 'attach2' ) ;
62- if ( this . attached ) {
63- return ;
64- }
173+ if ( this . attached ) return ;
174+ browserAppData . storage . local . get ( 'recorded_actions' )
175+ . then ( res => {
176+ if ( res . recorded_actions ) {
177+ console . log ( res ) ;
178+ this . idx = res . recorded_actions . length ;
179+ this . recorded_actions = res . recorded_actions ;
180+ }
181+ else {
182+ this . idx = 0 ;
183+ this . recorded_actions = [ ] ;
184+ }
185+ } ) ;
65186 this . attached = true ;
66187 this . eventListeners = { } ;
67188 var self = this ;
@@ -84,10 +205,18 @@ class Recorder {
84205 }
85206 /*detach */
86207 detach ( ) {
208+ console . log ( 'detach2' ) ;
87209 if ( ! this . attached ) {
88210 return ;
89211 }
90212 this . attached = false ;
213+ browserAppData . storage . local . set ( {
214+ recorded_actions : this . recorded_actions ,
215+ } ) . then ( ( ) => {
216+ this . idx = 0 ;
217+ this . recorded_actions = [ ] ;
218+ } ) ;
219+
91220 for ( let event_key in this . eventListeners ) {
92221 var event_info = this . parse_the_event_key ( event_key ) ;
93222 var event_name = event_info . event_name ;
0 commit comments