Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/justinwoo/purescript-toppokki
A binding to puppeteer to drive headless Chrome.
https://github.com/justinwoo/purescript-toppokki
headless-chrome puppeteer purescript
Last synced: about 1 month ago
JSON representation
A binding to puppeteer to drive headless Chrome.
- Host: GitHub
- URL: https://github.com/justinwoo/purescript-toppokki
- Owner: justinwoo
- License: mit
- Created: 2018-03-14T21:42:42.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-05-16T11:47:55.000Z (over 2 years ago)
- Last Synced: 2024-09-28T14:03:52.564Z (about 2 months ago)
- Topics: headless-chrome, puppeteer, purescript
- Language: PureScript
- Homepage: https://pursuit.purescript.org/packages/purescript-toppokki
- Size: 123 KB
- Stars: 52
- Watchers: 3
- Forks: 21
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PureScript-Toppokki
A binding to [puppeteer](https://github.com/GoogleChrome/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.
![](https://i.imgur.com/KPSU9lY.png)
## Usage
Make sure [Puppeteer](https://github.com/GoogleChrome/puppeteer) is installed (e.g. `npm i puppeteer`).
```hs
module Main whereimport Toppokki as T
import Prelude (bind, discard, (>))
import Effect.Aff (launchAff_)
import Test.Unit.Assert as Assert
import Data.String as Stringmain = launchAff_ do
browser <- T.launch {}
page <- T.newPage browser
T.goto (T.URL "https://example.com") 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](https://github.com/justinwoo/vidtracker/blob/37c511ed82f209e0236147399e8a91999aaf754c/src/GetIcons.purs) project useful:
```hs
downloadIconIfNotExist :: T.Browser -> Set String -> String -> Aff Unit
downloadIconIfNotExist browser existing name =
unless (member name existing) do
page <- T.newPage browser
let
name' = S.replace (S.Pattern " ") (S.Replacement "+") name
pageURL = "https://duckduckgo.com/?iax=images&ia=images&q=" <> 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 JSON.read 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
```