cdp: A library for the Chrome Devtools Protocol (CDP)

[ bsd3, chrome, library, package.category, program ] [ Propose Tags ]

Modules

[Last Documentation]

  • CDP
    • CDP.Definition
    • CDP.Domains
      • CDP.Domains.Accessibility
      • CDP.Domains.Animation
      • CDP.Domains.Audits
      • CDP.Domains.BackgroundService
      • CDP.Domains.BrowserTarget
      • CDP.Domains.CSS
      • CDP.Domains.CacheStorage
      • CDP.Domains.Cast
      • CDP.Domains.DOMDebugger
      • CDP.Domains.DOMPageNetworkEmulationSecurity
      • CDP.Domains.DOMSnapshot
      • CDP.Domains.DOMStorage
      • CDP.Domains.Database
      • CDP.Domains.Debugger
      • CDP.Domains.DeviceOrientation
      • CDP.Domains.EventBreakpoints
      • CDP.Domains.Fetch
      • CDP.Domains.HeadlessExperimental
      • CDP.Domains.HeapProfiler
      • CDP.Domains.IO
      • CDP.Domains.IndexedDB
      • CDP.Domains.Input
      • CDP.Domains.Inspector
      • CDP.Domains.LayerTree
      • CDP.Domains.Log
      • CDP.Domains.Media
      • CDP.Domains.Memory
      • CDP.Domains.Overlay
      • CDP.Domains.Performance
      • CDP.Domains.PerformanceTimeline
      • CDP.Domains.Profiler
      • CDP.Domains.Runtime
      • CDP.Domains.ServiceWorker
      • CDP.Domains.Storage
      • CDP.Domains.SystemInfo
      • CDP.Domains.Tethering
      • CDP.Domains.Tracing
      • CDP.Domains.WebAudio
      • CDP.Domains.WebAuthn
    • CDP.Endpoints
    • Gen
      • CDP.Gen.Deprecated
      • CDP.Gen.Program
      • CDP.Gen.Snippets
    • Internal
      • CDP.Internal.Utils
    • CDP.Runtime
  • Main

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.0.1.0, 0.0.1.1, 0.0.2.0
Dependencies aeson (==1.5.6.0), base (==4.14.3.0), base64-bytestring, blaze-html, blaze-markup, bytestring (==0.10.12.0), cdp, containers (==0.6.5.1), data-default (==0.7.1.1), directory (==1.3.6.0), extra (==1.7.9), filepath (==1.4.2.1), http-conduit (==2.3.8), monad-loops (==0.4.3), mtl (==2.2.2), network-uri (==2.6.4.1), process (==1.6.13.2), random (==1.2.0), text (==1.2.4.1), utf8-string, vector (==0.12.3.1), websockets (==0.12.7.3) [details]
License BSD-3-Clause
Author Arsalan Cheema
Maintainer Arsalan Cheema
Category Package.Category
Home page https://github.com/arsalan0c/cdp-hs#readme
Bug tracker https://github.com/arsalan0c/cdp-hs/issues
Source repo head: git clone https://github.com/arsalan0c/cdp-hs
Uploaded by arsalan0c at 2022-12-22T14:33:21Z
Distributions
Executables cdp-gen, cdp-example-subscribe, cdp-example-sessions, cdp-example-print-page, cdp-example-open-twitter, cdp-example-endpoints
Downloads 136 total (9 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
All reported builds failed as of 2022-12-22 [all 2 reports]

Readme for cdp-0.0.1.0

[back to package description]

build

cdp-hs

A Haskell library for the Chrome Devtools Protocol (CDP), generated from the protocol's definition files.

Example usage

Ensure Chrome is running with the remote debugging port enabled:

$ chromium --headless --remote-debugging-port=9222 https://wikipedia.com

The following program can be used to print a page to PDF, with Base64 encoded data being read in chunks:

{-# LANGUAGE OverloadedStrings   #-}

module Main where

import Data.Maybe
import Data.Default
import qualified Data.ByteString.Base64.Lazy as Base64
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL

import qualified CDP as CDP

main :: IO ()
main = do
    let cfg = def
    CDP.runClient cfg printPDF

printPDF :: CDP.Handle -> IO ()
printPDF handle = do
    -- send the Page.printToPDF command
    r <- CDP.sendCommandWait handle $ CDP.pPagePrintToPDF
        { CDP.pPagePrintToPDFTransferMode = Just CDP.PPagePrintToPDFTransferModeReturnAsStream
        }

    -- obtain stream handle from which to read pdf data
    let streamHandle = fromJust . CDP.pagePrintToPDFStream $ r

    -- read pdf data 24000 bytes at a time
    let params = CDP.PIORead streamHandle Nothing $ Just 24000
    reads <- whileTrue (not . CDP.iOReadEof) $ CDP.sendCommandWait handle params
    let dat = map decode reads
    BL.writeFile "mypdf.pdf" $ BL.concat dat

decode :: CDP.IORead -> BL.ByteString
decode ior = if (CDP.iOReadBase64Encoded ior == Just True)
    then Base64.decodeLenient lbs
    else lbs
  where
    lbs = TL.encodeUtf8 . TL.fromStrict . CDP.iOReadData $ ior

whileTrue :: Monad m => (a -> Bool) -> m a -> m [a]
whileTrue f act = do
    a <- act
    if f a
        then pure . (a :) =<< whileTrue f act
        else pure [a]

Generating the CDP library

cabal run cdp-gen

Current state

Project board

Commands and events for all non-deprecated domains are supported. The following session functionalities are supported:

  • creating a session: obtain a session id by using the pTargetAttachToTarget function to send a Target.attachToTarget command, passing True for the flatten argument
  • send a command for a particular session: use the sendCommandForSession function with a session id
  • subscribe to events for a particular session:
    1. register a handler with a session id
    2. send the command to enable events for the domain, with the same session id

Contributing

PRs are welcome! If you would like to discuss changes or have any feedback, feel free to open an issue.

Acknowledgements

This began as a Summer of Haskell / GSoC project. Albert Krewinkel (@tarleb), Jasper Van der Jeugt (@jaspervdj) and Romain Lesur (@RLesur) provided valuable feedback and support which along with raising the library's quality, has made this all the more enjoyable to work on.

References