import { Device } from 'last-hit-types' ; import { Browser , Target } from 'puppeteer' ; import uuidv4 from 'uuid/v4' ; import PageHelper from './page-helper' ; import AllPagesCache from './pages-cache' ; import { createPageWindowEventRecorder , PageWindowEventRecorder } from './window-event-recorder-creator' ; export default class BrowserHelper { private browser : Browser ; private flowKey : string ; private allPages : AllPagesCache ; private device : Device ; private eventRecorder : PageWindowEventRecorder ; constructor ( browser , allPages , device , flowKey ) { this . browser = browser ; this . flowKey = flowKey ; this . allPages = allPages ; this . device = device ; this . eventRecorder = createPageWindowEventRecorder ( this . flowKey ) ; } monitorDisconnected ( ) { this . getBrowser ( ) . on ( 'disconnected' , ( ) => this . recordPageWindowEvent ( { type : 'end' } ) ) ; } monitorTargetCreated ( ) { this . getBrowser ( ) . on ( 'targetcreated' , async ( newTarget ) => { if ( newTarget . type ( ) === 'page' ) { console . log ( 'browser event target created caught' ) ; const newPage = await newTarget . page ( ) ; const allPages = this . getAllPages ( ) ; if ( ! allPages . exists ( newPage ) ) { const uuid = uuidv4 ( ) ; allPages . add ( uuid , newPage ) ; await PageHelper . control ( this , newPage , false ) ; const base64 = await PageHelper . captureScreenshot ( newPage ) ; this . recordPageWindowEvent ( { type : 'page-created' , url : newPage . url ( ) , image : base64 , uuid } ) ; } } } ) ; } monitorTargetChanged ( ) { this . getBrowser ( ) . on ( 'targetchanged' , async ( target ) => { if ( target . type ( ) === 'page' ) { console . log ( 'browser event target changed caught' ) ; const page = await target . page ( ) ; const uuid = this . getAllPages ( ) . findUuidByPage ( page ) ; const url = page . url ( ) ; this . recordPageWindowEvent ( { type : 'page-switched' , url , uuid } ) ; let times = 0 ; const handle = ( ) => { setTimeout ( ( ) => { times ++ ; const anUrl = page . url ( ) ; if ( url === anUrl ) { if ( times < 10 ) { handle ( ) ; } } else { this . recordPageWindowEvent ( { type : 'page-switched' , url : anUrl , uuid } ) ; } } , 100 ) ; } ; handle ( ) ; } } ) ; } monitorTargetDestoryed ( ) { this . getBrowser ( ) . on ( 'targetdestroyed' , async ( target ) => { if ( target . type ( ) === 'page' ) { console . log ( 'browser event target destroyed caught' ) ; const page = await target . page ( ) ; const allClosed = await PageHelper . isAllRelatedPagesClosed ( page ) ; const uuid = this . getAllPages ( ) . removeByPage ( page ) ; if ( uuid ) { this . recordPageWindowEvent ( { type : 'page-closed' , url : page . url ( ) , uuid , allClosed } ) ; } } } ) ; } monitorAll ( ) { this . monitorDisconnected ( ) ; this . monitorTargetCreated ( ) ; this . monitorTargetChanged ( ) ; this . monitorTargetDestoryed ( ) ; } recordPageWindowEvent ( event ) { this . getEventRecorder ( ) . record ( JSON . stringify ( event ) ) ; } getBrowser ( ) { return this . browser ; } getFlowKey ( ) { return this . flowKey ; } getDevice ( ) { return this . device ; } getAllPages ( ) { return this . allPages ; } getEventRecorder ( ) { return this . eventRecorder ; } }