diff --git a/src/ChromeDevtoolsProtocol/Instance/Instance.php b/src/ChromeDevtoolsProtocol/Instance/Instance.php index e5b51102..4e80876b 100644 --- a/src/ChromeDevtoolsProtocol/Instance/Instance.php +++ b/src/ChromeDevtoolsProtocol/Instance/Instance.php @@ -58,7 +58,7 @@ public function __construct(string $host, int $port, ?ClientInterface $httpClien public function tabs(ContextInterface $ctx): array { - $response = $this->httpClient->request("POST", "http://{$this->host}:{$this->port}/json/list", [ + $response = $this->httpClient->request("GET", "http://{$this->host}:{$this->port}/json/list", [ "timeout" => $ctx->getDeadline() !== null ? $ctx->deadlineFromNow() : 0, ]); @@ -73,7 +73,7 @@ public function tabs(ContextInterface $ctx): array public function open(ContextInterface $ctx, ?string $url = null): Tab { $response = $this->httpClient->request( - "POST", + "PUT", "http://{$this->host}:{$this->port}/json/new" . ($url !== null ? "?" . urlencode($url) : ""), [ "timeout" => $ctx->getDeadline() !== null ? $ctx->deadlineFromNow() : 0, @@ -84,7 +84,7 @@ public function open(ContextInterface $ctx, ?string $url = null): Tab public function version(ContextInterface $ctx): Version { - $response = $this->httpClient->request("POST", "http://{$this->host}:{$this->port}/json/version", [ + $response = $this->httpClient->request("GET", "http://{$this->host}:{$this->port}/json/version", [ "timeout" => $ctx->getDeadline() !== null ? $ctx->deadlineFromNow() : 0, ]); return Version::fromJson(json_decode($response->getBody()->getContents())); @@ -160,7 +160,7 @@ public function createSession(ContextInterface $ctx, string $url = "about:blank" */ public function activateTabById(ContextInterface $ctx, string $id): void { - $this->httpClient->request("POST", "http://{$this->host}:{$this->port}/json/activate/{$id}", [ + $this->httpClient->request("GET", "http://{$this->host}:{$this->port}/json/activate/{$id}", [ "timeout" => $ctx->getDeadline() !== null ? $ctx->deadlineFromNow() : 0, ]); } @@ -170,7 +170,7 @@ public function activateTabById(ContextInterface $ctx, string $id): void */ public function closeTabById(ContextInterface $ctx, string $id): void { - $this->httpClient->request("POST", "http://{$this->host}:{$this->port}/json/close/{$id}", [ + $this->httpClient->request("GET", "http://{$this->host}:{$this->port}/json/close/{$id}", [ "timeout" => $ctx->getDeadline() !== null ? $ctx->deadlineFromNow() : 0, ]); } diff --git a/src/ChromeDevtoolsProtocol/Instance/Launcher.php b/src/ChromeDevtoolsProtocol/Instance/Launcher.php index 937d98fd..50cf6410 100644 --- a/src/ChromeDevtoolsProtocol/Instance/Launcher.php +++ b/src/ChromeDevtoolsProtocol/Instance/Launcher.php @@ -20,9 +20,10 @@ class Launcher const DEFAULT_LINUX_EXECUTABLE = "google-chrome"; const DEFAULT_WINDOWS_EXECUTABLE = "chrome"; - public static $defaultArgs = [ - "--headless", - ]; + public static $defaultArgs = [ + "--headless", + "--remote-allow-origins=http://127.0.0.1" + ]; /** @var string */ private $executable; @@ -40,16 +41,11 @@ class Launcher private $input; /** - * @param int $port If port <= 0, random port number is generated. - * @throws \Exception + * @param int $port If port <= 0, random available port is used. */ public function __construct($port = 0) { - if ($port <= 0) { - $port = random_int(1024 + 1, 65535); - } - - $this->port = $port; + $this->port = max(0, $port); } /** @@ -212,6 +208,10 @@ private function launchWithExecutable(ContextInterface $ctx, string $executable, $temporaryUserDataDir = null; if (!$foundUserDataDir) { $temporaryUserDataDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "chrome-profile-" . $this->port; + if ($this->port === 0) { + $temporaryUserDataDir .= "-" . bin2hex(random_bytes(8)); + } + $fs->mkdir($temporaryUserDataDir); $args[] = "--user-data-dir=" . $temporaryUserDataDir; } @@ -226,6 +226,16 @@ private function launchWithExecutable(ContextInterface $ctx, string $executable, ); $process->start(); + if ($this->port === 0) { + $process->waitUntil(function ($type, $buffer) { + if (preg_match('~DevTools listening on ws://.+:(\d+)/devtools~', $buffer, $m)) { + $this->port = (int)$m[1]; + return true; + } + return false; + }); + } + $instance = new ProcessInstance($process, $temporaryUserDataDir, $this->port); while (true) {