import prettier from "prettier" ; import type { Page , Request , Response } from "puppeteer" ; import cheerio from "cheerio" ; export async function getHtml ( page , selector ? ) { let html = await page . content ( ) ; return prettyHtml ( selector ? selectHtml ( html , selector ) : html ) ; } export function selectHtml ( source , selector ) { let el = cheerio ( selector , source ) ; if ( ! el . length ) { throw new Error ( `No element matches selector "${ selector }"` ) ; } return cheerio . html ( el ) ; } export function prettyHtml ( source ) { return prettier . format ( source , { parser : "html" } ) ; } interface UrlFilter { ( url ) : boolean ; } export function collectResponses ( page , filter ? ) { let responses = [ ] ; page . on ( "response" , res => { if ( ! filter || filter ( new URL ( res . url ( ) ) ) ) { responses . push ( res ) ; } } ) ; return responses ; } export function collectDataResponses ( page , routeId ? ) { return collectResponses ( page , url => url . searchParams . has ( "_data" ) && ( routeId === undefined || url . searchParams . get ( "_data" ) === routeId ) ) ; } export function reactIsHydrated ( page ) { return page . waitForFunction ( "window.reactIsHydrated === true" ) ; } export async function disableJavaScript ( page , extra ? ) { await page . setRequestInterception ( true ) ; page . on ( "request" , ( request ) => { if ( request . resourceType ( ) === "script" ) request . abort ( ) ; else if ( extra ) extra ( request ) ; else request . continue ( ) ; } ) ; }