@@ -8,6 +8,21 @@ fetch("./data.json")
88
99const browserAppData = chrome || browser ;
1010
11+ function generateId ( { stack, command, url, xpath} ) {
12+ if ( stack ) {
13+ for ( const item of Object . keys ( Stack ) ) {
14+ if ( Stack [ item ] && Stack [ item ] [ 0 ] . command === command && Stack [ item ] [ 0 ] . url === url && Stack [ item ] [ 0 ] . xpath === xpath )
15+ return item
16+ }
17+ }
18+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
19+ let id = '' ;
20+ for ( let i = 0 ; i < 8 ; i ++ ) {
21+ id += characters . charAt ( Math . floor ( Math . random ( ) * characters . length ) ) ;
22+ }
23+ return id ;
24+ }
25+
1126var notificationCount = 0 ;
1227function notification ( command ) {
1328 let tempCount = String ( notificationCount ) ;
@@ -39,12 +54,23 @@ var action_name_convert = {
3954}
4055
4156async function fetchAIData ( id , command , value , url , document ) {
42- browserAppData . runtime . sendMessage ( {
43- action : 'record-start' ,
44- data : {
45- id :id
46- } ,
47- } )
57+ if ( command === 'keystroke keys' ) {
58+ let keystroke = {
59+ id : id ,
60+ action : 'keystroke keys' ,
61+ element : "" ,
62+ is_disable : false ,
63+ name : `keystroke: ${ value } ` ,
64+ value : url ,
65+ main : [ [ 'keystroke keys' , 'selenium action' , value ] ] ,
66+ xpath : "" ,
67+ } ;
68+ browserAppData . runtime . sendMessage ( {
69+ action : 'record-finish' ,
70+ data : keystroke ,
71+ } )
72+ return ;
73+ }
4874 if ( command === 'go to link' ) {
4975 let go_to_link = {
5076 id : id ,
@@ -137,24 +163,95 @@ async function fetchAIData(id, command, value, url, document){
137163 } )
138164}
139165
140- async function record_action ( id , command , value , url , document ) {
141- if ( Object . keys ( action_name_convert ) . includes ( command ) ) command = action_name_convert [ command ] ;
142- notification ( command ) ;
143- fetchAIData ( id , command , value , url , document ) ;
166+ var Stack = { }
167+ async function record_action ( id , command , xpath , value , url , tagName , document , stack ) {
168+ let stacked_id = ''
169+ if ( stack ) {
170+ if ( ! ( id in Stack ) ) {
171+ Stack [ id ] = [ {
172+ id : id ,
173+ command : command ,
174+ xpath : xpath ,
175+ value : value ,
176+ url : url ,
177+ tagName : tagName ,
178+ document : document
179+ } ]
180+ browserAppData . runtime . sendMessage ( {
181+ action : 'record-start' ,
182+ data : {
183+ id :id
184+ } ,
185+ } )
186+ }
187+ else if (
188+ Stack [ id ] [ 0 ] . command == command &&
189+ Stack [ id ] [ 0 ] . xpath == xpath &&
190+ Stack [ id ] [ 0 ] . url == url
191+ ) {
192+ Stack [ id ] . push ( {
193+ id : id ,
194+ command : command ,
195+ xpath : xpath ,
196+ value : value ,
197+ url : url ,
198+ tagName : tagName ,
199+ document : document
200+ } )
201+ }
202+ stacked_id = id
203+ }
204+ for ( const item of Object . keys ( Stack ) ) {
205+ if ( item === stacked_id )
206+ continue
207+ let _id = Stack [ item ] [ Stack [ item ] . length - 1 ] . id
208+ let _command = Stack [ item ] [ Stack [ item ] . length - 1 ] . command
209+ let _value = Stack [ item ] [ Stack [ item ] . length - 1 ] . value
210+ let _url = Stack [ item ] [ Stack [ item ] . length - 1 ] . url
211+ let _document = Stack [ item ] [ Stack [ item ] . length - 1 ] . document
212+ if ( Object . keys ( action_name_convert ) . includes ( _command ) )
213+ _command = action_name_convert [ _command ] ;
214+ notification ( _command ) ;
215+ fetchAIData ( _id , _command , _value , _url , _document ) ;
216+ delete Stack [ item ]
217+ }
218+
219+ if ( ! stack ) {
220+ browserAppData . runtime . sendMessage ( {
221+ action : 'record-start' ,
222+ data : {
223+ id :id
224+ } ,
225+ } )
226+ if ( Object . keys ( action_name_convert ) . includes ( command ) ) command = action_name_convert [ command ] ;
227+ notification ( command ) ;
228+ fetchAIData ( id , command , value , url , document ) ;
229+ }
144230}
231+
145232browserAppData . runtime . onMessage . addListener (
146233 function ( request , sender , sendResponse ) {
147234 if ( request . action == 'start_recording' ) {
235+ Stack = { }
148236 notificationCount = 0 ;
149237 }
150238 else if ( request . action == 'record_action' ) {
239+ let id = generateId ( {
240+ stack : request . stack ,
241+ command : request . command ,
242+ url : request . url ,
243+ xpath : request . xpath ,
244+ } )
245+ console . log ( 'action_name =' , request . command )
151246 record_action (
152- request . id ,
247+ id ,
153248 request . command ,
154- // request.target ,
249+ request . xpath ,
155250 request . value ,
156251 request . url ,
252+ request . tagName ,
157253 request . document ,
254+ request . stack ,
158255 ) ;
159256 }
160257 }
0 commit comments