@@ -435,17 +435,13 @@ export class WorkflowGenerator {
435435 }
436436
437437 if ( ( elementInfo ?. tagName === 'INPUT' || elementInfo ?. tagName === 'TEXTAREA' ) && selector ) {
438- // Calculate the exact position within the element
439438 const positionAndCursor = await page . evaluate (
440439 ( { selector, coords } ) => {
441440 const getCursorPosition = ( element : any , clickX : any ) => {
442- // Get the input's text content
443441 const text = element . value ;
444442
445- // Create a temporary hidden div to measure text
446443 const mirror = document . createElement ( 'div' ) ;
447444
448- // Copy ALL relevant styles that could affect text measurement
449445 const style = window . getComputedStyle ( element ) ;
450446 mirror . style . cssText = `
451447 font: ${ style . font } ;
@@ -463,51 +459,33 @@ export class WorkflowGenerator {
463459
464460 document . body . appendChild ( mirror ) ;
465461
466- // Get the element's padding and border widths
467462 const paddingLeft = parseFloat ( style . paddingLeft ) ;
468463 const borderLeft = parseFloat ( style . borderLeftWidth ) ;
469464
470- // Adjust clickX to account for padding and border
471465 const adjustedClickX = clickX - ( paddingLeft + borderLeft ) ;
472466
473467 let bestIndex = 0 ;
474468 let bestDiff = Infinity ;
475469
476- // Try each possible cursor position
477470 for ( let i = 0 ; i <= text . length ; i ++ ) {
478- // Create a span for the text before cursor
479471 const textBeforeCursor = text . substring ( 0 , i ) ;
480472 const span = document . createElement ( 'span' ) ;
481473 span . textContent = textBeforeCursor ;
482474 mirror . innerHTML = '' ;
483475 mirror . appendChild ( span ) ;
484476
485- // Get the x-position where this character would end
486477 const textWidth = span . getBoundingClientRect ( ) . width ;
487478
488- // Calculate distance from adjusted click to this position
489479 const diff = Math . abs ( adjustedClickX - textWidth ) ;
490480
491- // If this position is closer to the click, update bestIndex
492481 if ( diff < bestDiff ) {
493482 bestIndex = i ;
494483 bestDiff = diff ;
495484 }
496485 }
497486
498- // Clean up
499487 document . body . removeChild ( mirror ) ;
500488
501- // Add debug logging
502- console . log ( {
503- text,
504- clickX,
505- adjustedClickX,
506- bestIndex,
507- value : text . substring ( 0 , bestIndex ) ,
508- nextChar : text [ bestIndex ] || 'EOL'
509- } ) ;
510-
511489 return bestIndex ;
512490 } ;
513491
@@ -1133,17 +1111,14 @@ export class WorkflowGenerator {
11331111 * @param workflow The workflow to be optimized.
11341112 */
11351113 private optimizeWorkflow = ( workflow : WorkflowFile ) => {
1136- // Track state for each input field
11371114 const inputStates = new Map < string , InputState > ( ) ;
11381115
1139- // First pass: Process all actions and build final states
11401116 for ( const pair of workflow . workflow ) {
11411117 let currentIndex = 0 ;
11421118
11431119 while ( currentIndex < pair . what . length ) {
11441120 const condition = pair . what [ currentIndex ] ;
11451121
1146- // Handle click actions with cursor positioning
11471122 if ( condition . action === 'click' && condition . args ?. [ 2 ] ?. cursorIndex !== undefined ) {
11481123 const selector = condition . args [ 0 ] ;
11491124 const cursorIndex = condition . args [ 2 ] . cursorIndex ;
@@ -1158,12 +1133,10 @@ export class WorkflowGenerator {
11581133 state . cursorPosition = cursorIndex ;
11591134 inputStates . set ( selector , state ) ;
11601135
1161- // Remove the click action
11621136 pair . what . splice ( currentIndex , 1 ) ;
11631137 continue ;
11641138 }
11651139
1166- // Handle text input and editing
11671140 if ( condition . action === 'press' && condition . args ?. [ 1 ] ) {
11681141 const [ selector , encryptedKey , type ] = condition . args ;
11691142 const key = decrypt ( encryptedKey ) ;
@@ -1173,11 +1146,10 @@ export class WorkflowGenerator {
11731146 state = {
11741147 selector,
11751148 value : '' ,
1176- type : type || 'text' , // Use the type from the press action
1149+ type : type || 'text' ,
11771150 cursorPosition : - 1
11781151 } ;
11791152 } else {
1180- // Update type from the press action if it exists
11811153 state . type = type || state . type ;
11821154 }
11831155
@@ -1206,14 +1178,12 @@ export class WorkflowGenerator {
12061178 state . value . slice ( 0 , state . cursorPosition ) +
12071179 state . value . slice ( state . cursorPosition + 1 ) ;
12081180 } else if ( state . cursorPosition === - 1 && state . value . length > 0 ) {
1209- // If no cursor position set, delete at the end
12101181 state . value = state . value . slice ( 0 , - 1 ) ;
12111182 }
12121183 }
12131184
12141185 inputStates . set ( selector , state ) ;
12151186
1216- // Remove the press action
12171187 pair . what . splice ( currentIndex , 1 ) ;
12181188 continue ;
12191189 }
@@ -1222,14 +1192,11 @@ export class WorkflowGenerator {
12221192 }
12231193 }
12241194
1225- // Second pass: Add one type action per selector in the last pair where that selector was used
12261195 for ( const [ selector , state ] of inputStates . entries ( ) ) {
12271196 if ( state . value ) {
1228- // Find the last pair that used this selector
12291197 for ( let i = workflow . workflow . length - 1 ; i >= 0 ; i -- ) {
12301198 const pair = workflow . workflow [ i ] ;
12311199
1232- // Add type action to the end of the pair
12331200 pair . what . push ( {
12341201 action : 'type' ,
12351202 args : [ selector , encrypt ( state . value ) , state . type ]
@@ -1238,7 +1205,7 @@ export class WorkflowGenerator {
12381205 args : [ 'networkidle' ]
12391206 } ) ;
12401207
1241- break ; // Stop after adding to the first pair we find
1208+ break ;
12421209 }
12431210 }
12441211 }
0 commit comments