jakubkulhan / chrome-devtools-protocol
Chrome Devtools Protocol 的 PHP 客户端
v1.0.4
2020-06-29 19:39 UTC
Requires
- php: ~7.1
- ext-json: *
- guzzlehttp/guzzle: ^6.3
- symfony/filesystem: >=2.3
- symfony/process: >=3.3
- wrench/wrench: ~2.0.10
Requires (Dev)
- jakub-onderka/php-parallel-lint: ~1.0.0
- nette/php-generator: ~2.1|~3.0
- phpstan/phpstan: ^0.11.4
- phpunit/phpunit: ~6.4
README
PHP 客户端,用于Chrome Devtools Protocol。
基本用法
// context creates deadline for operations $ctx = Context::withTimeout(Context::background(), 30 /* seconds */); // launcher starts chrome process ($instance) $launcher = new Launcher(); $instance = $launcher->launch($ctx); try { // work with new tab $tab = $instance->open($ctx); $tab->activate($ctx); $devtools = $tab->devtools(); try { $devtools->page()->enable($ctx); $devtools->page()->navigate($ctx, NavigateRequest::builder()->setUrl("https://www.google.com/")->build()); $devtools->page()->awaitLoadEventFired($ctx); // ... work with page ... // e.g. // - print to PDF: $devtools->page()->printToPDF($ctx, PrintToPDFRequest::make()); // - capture screenshot: $devtools->page()->captureScreenshot($ctx, CaptureScreenshotRequest::builder()->setFormat("jpg")->setQuality(95)->build()); } finally { // devtools client needs to be closed $devtools->close(); } } finally { // process needs to be killed $instance->close(); }
无头 Chrome 独立上下文
无头 Chrome 支持名为 browser contexts 的功能 - 它们就像隐身窗口 - cookie、本地存储等不被共享。在销毁 browser context 之后,在特定上下文中创建的用户数据将被销毁。
与隐身窗口不同,可以同时存在多个隔离的 browser contexts。
$ctx = Context::withTimeout(Context::background(), 10); $launcher = new Launcher(); $instance = $launcher->launch($ctx); try { $session = $instance->createSession($ctx); try { // $session implements DevtoolsClientInterface, same as returned from Tab::devtools() } finally { $session->close(); } } finally { $instance->close(); }
使用已运行的 Chrome 浏览器
use ChromeDevtoolsProtocol\Instance\Instance; $instance = new Instance(/* host: */ "localhost", /* port: */ 9222); $ctx = Context::withTimeout(Context::background(), 30 /* seconds */); $tab = $instance->open($ctx); $tab->activate($ctx); $devtools = $tab->devtools(); // ...work with devtools // no need to call ->close() as no new process is started
许可证
MIT 许可证下授权。请参阅 LICENSE
文件。