A binding to puppeteer to drive headless Chrome.

headless-chrome, puppeteer, purescript
psc-package install toppokki



A binding to puppeteer to drive headless Chrome.

This module is "incomplete" (but useful for regular work projects), and you can help by submitting PRs. You may find that goto, pageWaitForSelector, click, and unsafeEvaluateStringFunction already provide the functionality you need.

Named for glorious Tteok-bokki.


Make sure Puppeteer is installed (e.g. npm i puppeteer).

module Main where

import Toppokki as T
import Prelude (bind, discard, (>))
import Effect.Aff (launchAff_)
import Test.Unit.Assert as Assert
import Data.String as String

main = launchAff_ do
  browser <- T.launch {}
  page <- T.newPage browser
  T.goto (T.URL "") page
  content <- T.content page
  Assert.assert "content is non-empty string" (String.length content > 0)
  _ <- T.screenshot {path: "./test/test.png"} page
  _ <- T.pdf {path: "./test/test.pdf"} page
  T.close browser

More examples

You might also find this example from the vidtracker project useful:

downloadIconIfNotExist :: T.Browser -> Set String -> String -> Aff Unit
downloadIconIfNotExist browser existing name =
  unless (member name existing) do
    page <- T.newPage browser
      name' = S.replace (S.Pattern " ") (S.Replacement "+") name
      pageURL = "" <> name' <> "+anime+wiki"
    T.goto (T.URL pageURL) page
    _ <- T.pageWaitForSelector (T.Selector ".tile--img__img") {} page
    result <- T.unsafeEvaluateStringFunction "document.querySelector('.tile--img__img').src" page
    case result of
      Right (url :: String) -> do
        log $ "downloading from " <> url
        curl url (iconsPath <> "/" <> name)
        pure unit
      Left e -> do
        log $ "could not handle " <> name <> " with url " <> pageURL