Skip to content

Commit 1706f5c

Browse files
authored
By default, make initial connection to page instead of browser (#71)
* make initial connection to page instead of browser * add delay
1 parent 19f5a30 commit 1706f5c

8 files changed

Lines changed: 28 additions & 26 deletions

File tree

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ import qualified Data.Text.Lazy.Encoding as TL
2929
import qualified CDP as CDP
3030

3131
main :: IO ()
32-
main = do
33-
let cfg = def
34-
CDP.runClient cfg printPDF
32+
main = CDP.runClient def printPDF
3533

3634
printPDF :: CDP.Handle -> IO ()
3735
printPDF handle = do
@@ -62,6 +60,7 @@ whileTrue f act = do
6260
if f a
6361
then pure . (a :) =<< whileTrue f act
6462
else pure [a]
63+
6564
```
6665

6766
More examples can be found in `examples`.

cdp.cabal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ name: cdp
88
version: 0.0.1.1
99
synopsis: A library for the Chrome Devtools Protocol
1010
description: A library for the Chrome Devtools Protocol (CDP). It provides access to Chrome, enabling tasks such as printing a page or opening a tab.
11-
11+
.
1212
Chrome Devtools Protocol: <https://chromedevtools.github.io/devtools-protocol/>
13-
13+
.
1414
README: <https://github.com/arsalan0c/cdp-hs>
15-
15+
.
1616
Examples: <https://github.com/arsalan0c/cdp-hs/examples>
1717
category: Package.Category
1818
homepage: https://github.com/arsalan0c/cdp-hs#readme

examples/print-page.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import qualified Data.Text.Lazy.Encoding as TL
1313
import qualified CDP as CDP
1414

1515
main :: IO ()
16-
main = do
17-
let cfg = def
18-
CDP.runClient cfg printPDF
16+
main = CDP.runClient def printPDF
1917

2018
printPDF :: CDP.Handle -> IO ()
2119
printPDF handle = do

examples/sessions.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ main = do
1818
-- Attaches to target, returning the session id
1919
attachTarget :: CDP.Handle -> IO T.Text
2020
attachTarget handle = do
21-
-- get a target id
21+
-- get a target id by sending the Target.getTargets command
2222
targetInfo <- head . CDP.targetGetTargetsTargetInfos <$>
2323
(CDP.sendCommandWait handle $ CDP.PTargetGetTargets Nothing)
2424
let targetId = CDP.targetTargetInfoTargetId targetInfo

src/CDP/Endpoints.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ browserAddress :: (String, Int) -> IO (String, Int, String)
135135
browserAddress hostPort = fromMaybe (throw . ERRParse $ "invalid URI when connecting to browser") .
136136
parseUri . T.unpack . bvWebSocketDebuggerUrl <$> getEndpoint hostPort EPBrowserVersion
137137

138+
pageAddress :: (String, Int) -> IO (String, Int, String)
139+
pageAddress hostPort = fromMaybe (throw . ERRParse $ "invalid URI when connecting to page") .
140+
parseUri . T.unpack . tiWebSocketDebuggerUrl . head <$> getEndpoint hostPort EPAllTargets
141+
138142
getRequest :: (String, Int) -> [T.Text] -> Maybe T.Text -> Http.Request
139143
getRequest (host, port) path mbParam = Http.parseRequest_ . T.unpack $ r
140144
where

src/CDP/Internal/Utils.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ data Handle = Handle
6262

6363
data Config = Config
6464
{ hostPort :: (String, Int)
65-
-- | WebSocket path to connect to.
66-
-- If Nothing, the initial connection is made to the browser.
67-
, path :: Maybe String
65+
-- | Target of initial connection.
66+
-- If False, the initial connection is made to the page.
67+
, connectToBrowser :: Bool
6868
, doLogResponses :: Bool
6969
-- | Number of microseconds to wait for a command response.
7070
-- Waits forever if Nothing.
@@ -73,10 +73,10 @@ data Config = Config
7373
instance Default Config where
7474
def = Config{..}
7575
where
76-
hostPort = ("http://127.0.0.1", 9222)
77-
path = def
78-
doLogResponses = False
79-
commandTimeout = def
76+
hostPort = ("http://127.0.0.1", 9222)
77+
connectToBrowser = False
78+
doLogResponses = False
79+
commandTimeout = def
8080

8181
class FromJSON a => Event a where
8282
eventName :: Proxy a -> String

src/CDP/Runtime.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ runClient config app = do
5959
responseBuffer <- newMVar []
6060

6161
(host, port, path) <- do
62-
let hp@(host,port) = hostPort config
63-
maybe (browserAddress hp) (\path -> pure (host, port, path)) $ path config
62+
let hp = hostPort config
63+
if connectToBrowser config
64+
then browserAddress hp
65+
else pageAddress hp
66+
6467
WS.runClient host port path $ \conn -> do
6568
let listen = forkIO $ do
6669
listenThread <- myThreadId

test/Main.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import Test.Hspec
66
import Control.Monad
77
import Data.Default
88
import Control.Concurrent
9-
import Data.Maybe
10-
import qualified Data.Text as T
119

1210
import qualified CDP as CDP
1311

@@ -27,14 +25,14 @@ main = hspec $ do
2725
]
2826

2927
targetInfo <- runIO $ CDP.connectToTab def "https://haskell.foundation"
30-
let (host,port,path) = fromMaybe (error "invalid uri") . CDP.parseUri . T.unpack . CDP.tiWebSocketDebuggerUrl $ targetInfo
31-
cfg = def{CDP.hostPort = (host,port), CDP.path = Just path}
32-
targetId = CDP.tiId targetInfo
33-
28+
runIO $ threadDelay 1
29+
let cfg = def
3430
describe "Command responses of the expected type are received" $ do
3531
it "sends commands: w/o params w/o results" $ do
3632
CDP.runClient cfg $ \handle -> do
37-
CDP.sendCommandForSessionWait handle targetId CDP.PBrowserCrashGpuProcess
33+
sessionId <- CDP.targetAttachToTargetSessionId <$>
34+
(CDP.sendCommandWait handle $ CDP.PTargetAttachToTarget (CDP.tiId targetInfo) (Just True))
35+
CDP.sendCommandForSessionWait handle sessionId CDP.PBrowserCrashGpuProcess
3836

3937
it "sends commands: w/o params w/ results" $ do
4038
void $ CDP.runClient cfg $ \handle -> do

0 commit comments

Comments
 (0)