import type { ElementHandle as ElementHandleType } from 'puppeteer' ; import { getDocument , getQueriesForElement } from '.' ; import { IScopedQueryUtils } from './typedefs' ; let Page , ElementHandle ; function requireOrUndefined ( path ) { try { return require ( path ) ; } catch ( err ) { } } try { const libPrefix = requireOrUndefined ( `puppeteer/lib/cjs/puppeteer/common/Page.js` ) ? 'puppeteer/lib/cjs/puppeteer/common' : 'puppeteer/lib' ; Page = requireOrUndefined ( `${ libPrefix }/Page.js` ) ; if ( Page . Page ) Page = Page . Page ; ElementHandle = requireOrUndefined ( `${ libPrefix }/ElementHandle.js` ) ; if ( ElementHandle && ElementHandle . ElementHandle ) ElementHandle = ElementHandle . ElementHandle ; if ( ! ElementHandle ) { const ExecutionContext = requireOrUndefined ( `${ libPrefix }/ExecutionContext.js` ) ; if ( ExecutionContext && ExecutionContext . ElementHandle ) { ElementHandle = ExecutionContext . ElementHandle ; } } if ( ElementHandle && ElementHandle . ElementHandle ) ElementHandle = ElementHandle . ElementHandle ; if ( ! ElementHandle ) { const JSHandle = require ( `${ libPrefix }/JSHandle.js` ) ; if ( JSHandle && JSHandle . ElementHandle ) { ElementHandle = JSHandle . ElementHandle ; } } if ( ElementHandle && ElementHandle . ElementHandle ) ElementHandle = ElementHandle . ElementHandle ; Page . prototype . getDocument = getDocument ; getQueriesForElement ( ElementHandle . prototype , function ( this ) : ElementHandleType { return this ; } ) ; ElementHandle . prototype . getQueriesForElement = function ( this ) : ElementHandleType { return getQueriesForElement ( this ) ; } ; } catch ( err ) { console . error ( 'Could not augment puppeteer functions, do you have a conflicting version?' ) ; throw err ; } declare module 'puppeteer' { interface Page { getDocument ( ) : Promise < ElementHandle > ; } interface ElementHandle extends IScopedQueryUtils { } }