Edits history of script submission #23103 for ' Get Rendered HTML (cloudflare)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get Rendered HTML
     * Load a page (url or raw html) in a headless browser and return the fully rendered HTML. Extra fields go in options (gotoOptions, viewport, waitForSelector, userAgent, ...).
     */
    export async function main(
      auth: RT.Cloudflare,
      url: string | undefined,
      html: string | undefined,
      options: { [key: string]: any } | undefined
    ) {
      if (!url && !html) throw new Error("Either url or html is required")
    
      const response = await fetch(
        `https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/browser-rendering/content`,
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            url: url || undefined,
            html: html || undefined,
            ...options,
          }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 4 hours ago