This is a thin wrapper Chrome DevTools Protocol client written in Kotlin with coroutine based API and supporting Kotlin Multi Platform (KMP).
// You need launching Chrome with "--remote-debugging-port=9222" option
// In a coroutine scope
val cdp = CDPClient.create("localhost", 9222, "devtools/browser/d0cd4d55-c55b-4c3a-973f-717079053d95")
cdp.target.createTarget("http://example.com")
// Now Chrome opens a new page!
You can call CDP methods like a normal function!
// Calling "Target.createPage"
// https://chromedevtools.github.io/devtools-protocol/tot/Target/#method-createTarget
cdp.target.createTarget("http://example.com/")
// Calling "Page.captureScreenshot"
// https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot
val (data) = cdp.page.captureScreenshot(format = "jpeg", quality = 50)
// Calling "Runtime.evaluate"
// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-evaluate
val (result, exceptionDetails) = cdp.runtime.evaluate("document.querySelector('#main')")
Also, you can subscribe CDP events you want to watch as Flow
streams!
// Listening "Target.targetCreated"
// https://chromedevtools.github.io/devtools-protocol/tot/Target/#event-targetCreated
cdp.target.targetCreated
.filter { (targetInfo) -> targetInfo.attached}
.collect { (targetInfo) ->
print(targetInfo.url)
}
This is Kotlin Multi-platform Project, so you can use this wrapper from JVM project (including Android project), JavaScript project, native application project (including iOS project)!
Codes for CDP Domains are auto generated. We can easily update it from the protocol information of Google Chrome / Chromium.
TBD
- Generator from protocol JSON file
- Coroutine support
- Testing with headless Chrome
- Separate artifact IDs by versions of protocol documents (1.2, 1.3RC, node, tot)
- Publish to MavenCentral
- Making "PuppeteerKT" using this