Created
April 6, 2025 13:21
-
-
Save thekid/5f6ce2faec4a1f13244ebeaa10961d4c to your computer and use it in GitHub Desktop.
CDP in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use io\{File, Files}; | |
use lang\Value; | |
use peer\http\HttpConnection; | |
use text\json\{Json, StreamInput}; | |
use util\Comparison; | |
use util\cmd\Console; | |
use websocket\WebSocket; | |
/** @see https://chromedevtools.github.io/devtools-protocol/tot/ */ | |
class CDP implements Value { | |
use Comparison; | |
private $ws; | |
private $id= 0; | |
public function __construct($url) { | |
$this->ws= new WebSocket($url); | |
$this->ws->connect(); | |
} | |
public function send($method, $params= null) { | |
$payload= ['id' => ++$this->id, 'method' => $method]; | |
isset($params) && $payload['params']= $params; | |
$this->ws->send(Json::of($payload)); | |
return $this->id; | |
} | |
public function receive() { | |
return Json::read($this->ws->receive()); | |
} | |
public function close() { | |
$this->ws->close(); | |
} | |
public function toString() { | |
return nameof($this).'(->'.$this->ws->socket()->toString().$this->ws->path().')'; | |
} | |
} | |
// Fetch list of open tabs | |
$response= (new HttpConnection('http://localhost:9222/json'))->get(); | |
$tabs= Json::read($response->readData($response->header('Content-Length')[0])); | |
$target= null; | |
foreach ($tabs as $tab) { | |
if ('edge://newtab/' === $tab['url']) $target= $tab; | |
} | |
if (empty($target)) { | |
Console::$err->writeLine('No active tab in ', $tabs); | |
return 1; | |
} | |
// Connect to CDP websocket | |
$cdp= new CDP($target['webSocketDebuggerUrl']); | |
Console::writeLine($cdp); | |
$cdp->send('Page.enable'); | |
$cdp->send('Page.navigate', ['url' => $argv[1]]); | |
do { | |
$answer= $cdp->receive(); | |
if ('Page.frameStoppedLoading' === ($answer['method'] ?? null)) { | |
Console::writeLine($answer); | |
break; | |
} | |
Console::writeLine($answer); | |
} while ($answer); | |
$dom= $cdp->send('Runtime.evaluate', [ | |
'expression' => 'document.body.outerHTML', | |
'returnByValue' => true, | |
]); | |
do { | |
$answer= $cdp->receive(); | |
if ($dom === ($answer['id'] ?? null)) { | |
Console::writeLine($answer['result']); | |
break; | |
} | |
Console::writeLine($answer); | |
} while ($answer); | |
// $screenshot= $cdp->send('Page.captureScreenshot', [ | |
// 'format' => 'png', | |
// 'fromSurface' => true, | |
// 'captureBeyondViewport' => true, | |
// ]); | |
// do { | |
// $answer= $cdp->receive(); | |
// if ($screenshot === ($answer['id'] ?? null)) { | |
// Files::write(new File('screenshot.png'), base64_decode($answer['result']['data'])); | |
// break; | |
// } | |
// Console::writeLine($answer); | |
// } while ($answer); | |
$cdp->close(); |
Author
thekid
commented
Apr 6, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment