Skip to content

Chrome 111 compatibility hotfix #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/ChromeDevtoolsProtocol/Instance/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);

Expand All @@ -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,
Expand All @@ -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()));
Expand Down Expand Up @@ -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,
]);
}
Expand All @@ -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,
]);
}
Expand Down
30 changes: 20 additions & 10 deletions src/ChromeDevtoolsProtocol/Instance/Launcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down