import { defaultWaitUntilOptions , sleep , toPage } from '../../../utils' ; import { ClickOptions } from '../../handle-actions' ; import { Frame , Page } from 'playwright' ; declare const window ; export interface PasteTextOptions { type { number } memberof delay : number ; type { boolean } memberof handlePasteEvent : boolean ; type { boolean } memberof clearExistingContent : boolean ; } export const defaultPasteTextOptions = { delay : 50 , handlePasteEvent : false , clearExistingContent : true , } ; export async function pasteText ( text , page , options ) { if ( ! page ) { throw new Error ( `Cannot paste text '${ text }' because no browser has been launched` ) ; } const focusedElement = await page . evaluateHandle ( ( ) => window . document . activeElement ) ; if ( focusedElement === null ) { throw new Error ( `You must first click on an editable element before pasting text.` ) ; } const handle = focusedElement . asElement ( ) ; if ( handle === null ) { throw new Error ( `You must first click on an editable element before pasting text.` ) ; } const currentTagName = await handle . evaluate ( ( node ) => ( node as HTMLElement ) . tagName ) ; const isContentEditable = await handle . evaluate ( ( node ) => ( node as HTMLElement ) . isContentEditable ) ; if ( currentTagName === 'BODY' ) { throw new Error ( `You must first click on an editable element before pasting text.` ) ; } if ( currentTagName === 'P' && ! isContentEditable ) { throw new Error ( `You must first click on an editable element before pasting text.` ) ; } if ( currentTagName === 'DIV' && ! isContentEditable ) { throw new Error ( `You must first click on an editable element before clearing text.` ) ; } if ( options . clearExistingContent ) { const tripleClickOptions = { ... defaultWaitUntilOptions , button : 'left' , delay : options . delay , clickCount : 3 , } ; await handle . click ( tripleClickOptions ) ; await sleep ( options . delay * 5 ) ; await toPage ( page ) . keyboard . press ( 'Backspace' , { delay : options . delay } ) ; } await handle . evaluate ( ( node , { content , handlePasteEvent } ) => { function attachPasteEvent ( el ) { el . addEventListener ( 'paste' , ( event ) => { const content = ( ( event as any ) . clipboardData || ( window as any ) . clipboardData ) . getData ( 'text' ) as string ; const input = event . target as HTMLInputElement ; if ( event . target && input && input . tagName === 'INPUT' ) { input . value += content ; event . preventDefault ( ) ; return ; } ( event . target as HTMLElement ) . innerText += content ; event . preventDefault ( ) ; } ) ; } if ( handlePasteEvent ) { attachPasteEvent ( node ) ; } ( window as any ) . clipboardData = { getData : ( ) : string => content } ; const event = new CustomEvent ( 'paste' , { bubbles : true , } ) ; ( event as any ) . clipboardData = { getData : ( ) : string => content } ; node . dispatchEvent ( event ) ; ( window as any ) . clipboardData = undefined ; } , { content : text , handlePasteEvent : options . handlePasteEvent } ) ; }