项目作者: andrewvy

项目描述 :
Elixir Client for the Chrome Debugger Protocol
高级语言: Elixir
项目地址: git://github.com/andrewvy/chrome-remote-interface.git
创建时间: 2017-08-16T07:53:48Z
项目社区:https://github.com/andrewvy/chrome-remote-interface

开源协议:

下载


Chrome Remote Interface

CircleCI

This library provides an Elixir Client to the Chrome Debugging Protocol with
a small layer of abstraction for handling and subscribing to domain events.

Note: This is a super minimal client wrapper around the Chrome Debugging Protocol.

Installation

Add :chrome_remote_interface to your mix.exs file!

  1. def deps do
  2. [
  3. {:chrome_remote_interface, "~> 0.4.1"}
  4. ]
  5. end

Chrome DevTools Protocol Selection

Chrome Remote Interface generated its API at compile time from the protocol
definition released by the Chrome DevTools Team.
For more info see: https://chromedevtools.github.io/devtools-protocol/

This can be overridden by setting CRI_PROTOCOL_VERSION environment variable
to:

  • 1-2
  • 1-3 * default
  • tot

Example:

  1. CRI_PROTOCOL_VERSION=1-2 mix compile
  2. CRI_PROTOCOL_VERSION=1-3 mix compile
  3. CRI_PROTOCOL_VERSION=tot mix compile

Usage

Note: In these examples, it assumes you’re already running chrome headless with remote debugging enabled.

  1. chrome --headless --disable-gpu --remote-debugging-port=9222

Basic API

  1. # ChromeRemoteInterface works off by creating a Session to the remote debugging port.
  2. # By default, connects to 'localhost:9222
  3. iex(1)> server = ChromeRemoteInterface.Session.new()
  4. %ChromeRemoteInterface.Server{host: "localhost", port: 9222}
  5. iex(2)> {:ok, pages} = ChromeRemoteInterface.Session.list_pages(server)
  6. {:ok,
  7. [%{"description" => "",
  8. "devtoolsFrontendUrl" => "/devtools/inspector.html?ws=localhost:9222/devtools/page/d4357ff1-47e8-4e53-8289-fc54089da33e",
  9. "id" => "d4357ff1-47e8-4e53-8289-fc54089da33e", "title" => "Google",
  10. "type" => "page", "url" => "https://www.google.com/?gws_rd=ssl",
  11. "webSocketDebuggerUrl" => "ws://localhost:9222/devtools/page/d4357ff1-47e8-4e53-8289-fc54089da33e"}]}
  12. # Now that we have a list of pages, we can connect to any page by using their 'webSocketDebuggerUrl'
  13. iex(3)> first_page = pages |> List.first()
  14. iex(4)> {:ok, page_pid} = ChromeRemoteInterface.PageSession.start_link(first_page)
  15. # Any methods from https://chromedevtools.github.io/devtools-protocol/1-2/ should be available
  16. # to execute on that Page.
  17. # 'Page.navigate'
  18. iex(5)> ChromeRemoteInterface.RPC.Page.navigate(page_pid, %{url: "https://google.com"})
  19. %{"id" => 1, "result" => %{"frameId" => "95446.1"}}
  20. # 'Page.printToPDF'
  21. iex(6)> ChromeRemoteInterface.RPC.Page.printToPDF(page_pid, %{})
  22. {:ok, %{"id" => 2, "result" => %{"data" => "JVBERi0xLj..."}}}