chrome_remote 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +185 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/chrome_remote.gemspec +29 -0
- data/examples/network_dump_and_screenshot.rb +21 -0
- data/lib/chrome_remote.rb +31 -0
- data/lib/chrome_remote/client.rb +65 -0
- data/lib/chrome_remote/socket.rb +22 -0
- data/lib/chrome_remote/version.rb +3 -0
- data/lib/chrome_remote/web_socket_client.rb +57 -0
- metadata +160 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0d0252fdb7d96afe43850928ba57f69cdc66e373
|
4
|
+
data.tar.gz: 2500d1a4b85a0e77ee4b50db79011273b4e338dc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 86d013ba0f962afb6cf79847bd135f9ed72b1017ee0995bff299aa575f15d600d3c74f1070a5f7ad7fa99a38d051860da2cceede9813c795fb721b268b2a82b0
|
7
|
+
data.tar.gz: 20355a53f1b7dcf3f08a84fd3e111a3dc6015e3e5657dbe74f24c813cb6ca248785a40e7c053bc0848543e86f4c7606cbc1358bb3a512cf96a664e43d93add27
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at luismi@lmcavalle.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Luismi Cavalle
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
# ChromeRemote
|
2
|
+
|
3
|
+
ChromeRemote is a client implementation of the [Chrome DevTools Protocol][1] in Ruby. It lets you instrument, inspect, debug and profile instances of Chrome/Chromium based browsers from your Ruby code.
|
4
|
+
|
5
|
+
[1]: https://chromedevtools.github.io/devtools-protocol/
|
6
|
+
|
7
|
+
## Usage example
|
8
|
+
|
9
|
+
The following snippet navigates to `https://github.com`, dumps any request made while loading the page, and takes a screenshot once the page is loaded:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require 'chrome_remote'
|
13
|
+
require 'base64'
|
14
|
+
|
15
|
+
chrome = ChromeRemote.client
|
16
|
+
|
17
|
+
# Enable events
|
18
|
+
chrome.send_cmd "Network.enable"
|
19
|
+
chrome.send_cmd "Page.enable"
|
20
|
+
|
21
|
+
# Setup handler to log network requests
|
22
|
+
chrome.on "Network.requestWillBeSent" do |params|
|
23
|
+
puts params["request"]["url"]
|
24
|
+
end
|
25
|
+
|
26
|
+
# Navigate to github.com and wait for the page to load
|
27
|
+
chrome.send_cmd "Page.navigate", url: "https://github.com"
|
28
|
+
chrome.wait_for "Page.loadEventFired"
|
29
|
+
|
30
|
+
# Take page screenshot
|
31
|
+
response = chrome.send_cmd "Page.captureScreenshot"
|
32
|
+
File.write "screenshot.png", Base64.decode64(response["data"])
|
33
|
+
```
|
34
|
+
|
35
|
+
## Installation
|
36
|
+
|
37
|
+
Add this line to your application's Gemfile:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
gem 'chrome_remote'
|
41
|
+
```
|
42
|
+
|
43
|
+
And then execute:
|
44
|
+
|
45
|
+
```
|
46
|
+
$ bundle
|
47
|
+
```
|
48
|
+
|
49
|
+
Or install it yourself as:
|
50
|
+
|
51
|
+
```
|
52
|
+
$ gem install chrome_remote
|
53
|
+
```
|
54
|
+
|
55
|
+
## Usage
|
56
|
+
|
57
|
+
To use ChromeRemote, you'll need a Chrome instance running on a known port (`localhost:9222` is the default), using the `--remote-debugging-port` flag.
|
58
|
+
|
59
|
+
In Linux:
|
60
|
+
|
61
|
+
```
|
62
|
+
$ google-chrome --remote-debugging-port=9222
|
63
|
+
```
|
64
|
+
|
65
|
+
In macOS:
|
66
|
+
|
67
|
+
```
|
68
|
+
$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
|
69
|
+
```
|
70
|
+
|
71
|
+
In Windows 7 or above:
|
72
|
+
|
73
|
+
```
|
74
|
+
> "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
|
75
|
+
```
|
76
|
+
|
77
|
+
#### Headless mode
|
78
|
+
|
79
|
+
Additionally, since version 59, you can use the `--headless` flag to start Chrome in [headless mode][2]
|
80
|
+
|
81
|
+
[2]: https://developers.google.com/web/updates/2017/04/headless-chrome
|
82
|
+
|
83
|
+
### Using the ChromeRemote API
|
84
|
+
|
85
|
+
The [Chrome DevTools Protocol][1] is divided into a number of domains ([Page][3], [DOM][4], [Debugger][5], [Network][6], etc.). Each domain defines a number of **commands** it supports and **events** it generates.
|
86
|
+
|
87
|
+
ChromeRemote provides a simple API that lets you send commands, and handle events of any of the domains in the protocol.
|
88
|
+
|
89
|
+
To start with, you need an instance of the `ChromeRemote` class.
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
chrome = ChromeRemote.client host: 'localhost', # optional, default: localhost
|
93
|
+
port: 9992 # optional, default: 9992
|
94
|
+
```
|
95
|
+
|
96
|
+
Now, to send commands, ChromeRemote provides the `ChromeRemote#send_cmd` method. For example, this is how you make Chrome navigate to a url by sending the [Page.navigate][7] command:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
chrome = ChromeRemote.client
|
100
|
+
chrome.send_cmd "Page.navigate", url: "https://github.com"
|
101
|
+
# => {:frameId=>1234}
|
102
|
+
```
|
103
|
+
|
104
|
+
To tackle events, you have several options but, first of all, you need to enable events for any domain you're interested in. You only need to do this once per domain:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
chrome = ChromeRemote.client
|
108
|
+
chrome.send_cmd "Network.enable"
|
109
|
+
```
|
110
|
+
|
111
|
+
Now, you can use the `ChromeRemote#on` method to subscribe to an event. For instance, this is how you subscribe to the [Network.requestWillBeSent][8] event:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
chrome = ChromeRemote.client
|
115
|
+
chrome.send_cmd "Network.enable"
|
116
|
+
|
117
|
+
chrome.on "Network.requestWillBeSent" do |params|
|
118
|
+
puts params["request"]["url"]
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
With the `ChromeRemote#wait_for` method, you can wait until the next time a given event is triggered. For example, the following snippet navigates to a page and waits for the [Page.loadEventFired][9] event to happen:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
chrome = ChromeRemote.client
|
126
|
+
chrome.send_cmd "Page.navigate", url: "https://github.com"
|
127
|
+
|
128
|
+
chrome.wait_for "Page.loadEventFired"
|
129
|
+
# => {:timestamp=>34}
|
130
|
+
```
|
131
|
+
|
132
|
+
In certain occasions, after you have subscribed to one or several events, you may just want to process messages indefinitely, and let the event handlers process any event that may happen until you kill your script. For those cases, ChromeRemote provides the `ChromeRemote#listen` method:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
chrome = ChromeRemote.client
|
136
|
+
chrome.send_cmd "Network.enable"
|
137
|
+
|
138
|
+
chrome.on "Network.requestWillBeSent" do |params|
|
139
|
+
puts params["request"]["url"]
|
140
|
+
end
|
141
|
+
|
142
|
+
chrome.listen # will process incoming messages indefinitely
|
143
|
+
```
|
144
|
+
|
145
|
+
Finally, you have `ChromeRemote#listen_until` that will listen and process incoming messages but only until a certain condition is met. For instance, the following snippet waits until 5 requests are received and then continues:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
chrome = ChromeRemote.client
|
149
|
+
chrome.send_cmd "Network.enable"
|
150
|
+
|
151
|
+
requests = 0
|
152
|
+
chrome.on "Network.requestWillBeSent" do |params|
|
153
|
+
requests += 1
|
154
|
+
end
|
155
|
+
|
156
|
+
chrome.listen_until { requests == 5 }
|
157
|
+
|
158
|
+
# do other stuff
|
159
|
+
```
|
160
|
+
|
161
|
+
[3]: https://chromedevtools.github.io/devtools-protocol/tot/Page/
|
162
|
+
[4]: https://chromedevtools.github.io/devtools-protocol/tot/DOM/
|
163
|
+
[5]: https://chromedevtools.github.io/devtools-protocol/tot/Debugger/
|
164
|
+
[6]: https://chromedevtools.github.io/devtools-protocol/tot/Network/
|
165
|
+
[7]: https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-navigate
|
166
|
+
[8]: https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-requestWillBeSent
|
167
|
+
[9]: https://chromedevtools.github.io/devtools-protocol/tot/Page/#event-loadEventFired
|
168
|
+
|
169
|
+
## Development
|
170
|
+
|
171
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment or any of the scripts in the `/examples` directory (e.g. `bundle exec ruby examples/network_dump_and_screenshot.rb`).
|
172
|
+
|
173
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
174
|
+
|
175
|
+
To release a new version (if you're a maintainer), update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
176
|
+
|
177
|
+
## Contributing
|
178
|
+
|
179
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cavalle/chrome_remote.
|
180
|
+
|
181
|
+
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to follow the [code of conduct](https://github.com/cavalle/chrome_remote/blob/master/CODE_OF_CONDUCT.md).
|
182
|
+
|
183
|
+
## License
|
184
|
+
|
185
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "chrome_remote"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "chrome_remote/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "chrome_remote"
|
8
|
+
spec.version = ChromeRemote::VERSION
|
9
|
+
spec.authors = ["Luismi Cavalle"]
|
10
|
+
spec.email = ["luismi@lmcavalle.com"]
|
11
|
+
|
12
|
+
spec.summary = "ChromeRemote is a client implementation of the Chrome DevTools Protocol in Ruby"
|
13
|
+
spec.homepage = "https://github.com/cavalle/chrome_remote"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "websocket-driver", "~> 0.6"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
spec.add_development_dependency "em-websocket", "~> 0.5"
|
27
|
+
spec.add_development_dependency "byebug"
|
28
|
+
spec.add_development_dependency "webmock"
|
29
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'chrome_remote'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
chrome = ChromeRemote.client
|
5
|
+
|
6
|
+
# Enable events
|
7
|
+
chrome.send_cmd "Network.enable"
|
8
|
+
chrome.send_cmd "Page.enable"
|
9
|
+
|
10
|
+
# Setup handler to log network requests
|
11
|
+
chrome.on "Network.requestWillBeSent" do |params|
|
12
|
+
puts params["request"]["url"]
|
13
|
+
end
|
14
|
+
|
15
|
+
# Navigate to github.com and wait for the page to load
|
16
|
+
chrome.send_cmd "Page.navigate", url: "https://github.com"
|
17
|
+
chrome.wait_for "Page.loadEventFired"
|
18
|
+
|
19
|
+
# Take page screenshot
|
20
|
+
response = chrome.send_cmd "Page.captureScreenshot"
|
21
|
+
File.write "screenshot.png", Base64.decode64(response["data"])
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "chrome_remote/version"
|
2
|
+
require "chrome_remote/client"
|
3
|
+
require "json"
|
4
|
+
require "net/http"
|
5
|
+
|
6
|
+
module ChromeRemote
|
7
|
+
class << self
|
8
|
+
DEFAULT_OPTIONS = {
|
9
|
+
host: "localhost",
|
10
|
+
port: 9222
|
11
|
+
}
|
12
|
+
|
13
|
+
def client(options = {})
|
14
|
+
options = DEFAULT_OPTIONS.merge(options)
|
15
|
+
|
16
|
+
Client.new(get_ws_url(options))
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def get_ws_url(options)
|
22
|
+
response = Net::HTTP.get(options[:host], "/json", options[:port])
|
23
|
+
# TODO handle unsuccesful request
|
24
|
+
response = JSON.parse(response)
|
25
|
+
|
26
|
+
first_page = response.find {|e| e["type"] == "page"}
|
27
|
+
# TODO handle no entry found
|
28
|
+
first_page["webSocketDebuggerUrl"]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "chrome_remote/web_socket_client"
|
2
|
+
|
3
|
+
module ChromeRemote
|
4
|
+
class Client
|
5
|
+
attr_reader :ws, :handlers
|
6
|
+
|
7
|
+
def initialize(ws_url)
|
8
|
+
@ws = WebSocketClient.new(ws_url)
|
9
|
+
@handlers = Hash.new {|hash, key| hash[key] = [] }
|
10
|
+
end
|
11
|
+
|
12
|
+
def send_cmd(command, params = {})
|
13
|
+
msg_id = generate_unique_id
|
14
|
+
|
15
|
+
ws.send_msg({method: command, params: params, id: msg_id}.to_json)
|
16
|
+
|
17
|
+
msg = read_until { |msg| msg["id"] == msg_id }
|
18
|
+
msg["result"]
|
19
|
+
end
|
20
|
+
|
21
|
+
def on(event_name, &block)
|
22
|
+
handlers[event_name] << block
|
23
|
+
end
|
24
|
+
|
25
|
+
def listen_until(&block)
|
26
|
+
read_until { block.call }
|
27
|
+
end
|
28
|
+
|
29
|
+
def listen
|
30
|
+
read_until { false }
|
31
|
+
end
|
32
|
+
|
33
|
+
def wait_for(event_name)
|
34
|
+
msg = read_until { |msg| msg["method"] == event_name }
|
35
|
+
msg["params"]
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def generate_unique_id
|
41
|
+
@last_id ||= 0
|
42
|
+
@last_id += 1
|
43
|
+
end
|
44
|
+
|
45
|
+
def read_msg
|
46
|
+
msg = JSON.parse(ws.read_msg)
|
47
|
+
|
48
|
+
# Check if it’s an event and invoke any handlers
|
49
|
+
if event_name = msg["method"]
|
50
|
+
handlers[event_name].each do |handler|
|
51
|
+
handler.call(msg["params"])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
msg
|
56
|
+
end
|
57
|
+
|
58
|
+
def read_until(&block)
|
59
|
+
loop do
|
60
|
+
msg = read_msg
|
61
|
+
return msg if block.call(msg)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "socket"
|
2
|
+
|
3
|
+
module ChromeRemote
|
4
|
+
class Socket
|
5
|
+
attr_reader :url, :io
|
6
|
+
|
7
|
+
def initialize(url)
|
8
|
+
uri = URI.parse(url)
|
9
|
+
|
10
|
+
@url = url
|
11
|
+
@io = TCPSocket.new(uri.host, uri.port)
|
12
|
+
end
|
13
|
+
|
14
|
+
def write(data)
|
15
|
+
io.print data
|
16
|
+
end
|
17
|
+
|
18
|
+
def read
|
19
|
+
io.readpartial(1024)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "websocket/driver"
|
2
|
+
require "chrome_remote/socket"
|
3
|
+
|
4
|
+
module ChromeRemote
|
5
|
+
class WebSocketClient
|
6
|
+
attr_reader :socket, :driver, :messages, :status
|
7
|
+
|
8
|
+
def initialize(url)
|
9
|
+
@socket = ChromeRemote::Socket.new(url)
|
10
|
+
@driver = ::WebSocket::Driver.client(socket)
|
11
|
+
|
12
|
+
@messages = []
|
13
|
+
@status = :closed
|
14
|
+
|
15
|
+
setup_driver
|
16
|
+
start_driver
|
17
|
+
end
|
18
|
+
|
19
|
+
def send_msg(msg)
|
20
|
+
driver.text msg
|
21
|
+
end
|
22
|
+
|
23
|
+
def read_msg
|
24
|
+
parse_input until msg = messages.shift
|
25
|
+
msg
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def setup_driver
|
31
|
+
driver.on(:message) do |e|
|
32
|
+
messages << e.data
|
33
|
+
end
|
34
|
+
|
35
|
+
driver.on(:error) do |e|
|
36
|
+
raise e.message
|
37
|
+
end
|
38
|
+
|
39
|
+
driver.on(:close) do |e|
|
40
|
+
@status = :closed
|
41
|
+
end
|
42
|
+
|
43
|
+
driver.on(:open) do |e|
|
44
|
+
@status = :open
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def start_driver
|
49
|
+
driver.start
|
50
|
+
parse_input until status == :open
|
51
|
+
end
|
52
|
+
|
53
|
+
def parse_input
|
54
|
+
@driver.parse(@socket.read)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chrome_remote
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Luismi Cavalle
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: websocket-driver
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: em-websocket
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: webmock
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- luismi@lmcavalle.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- CODE_OF_CONDUCT.md
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- bin/console
|
127
|
+
- bin/setup
|
128
|
+
- chrome_remote.gemspec
|
129
|
+
- examples/network_dump_and_screenshot.rb
|
130
|
+
- lib/chrome_remote.rb
|
131
|
+
- lib/chrome_remote/client.rb
|
132
|
+
- lib/chrome_remote/socket.rb
|
133
|
+
- lib/chrome_remote/version.rb
|
134
|
+
- lib/chrome_remote/web_socket_client.rb
|
135
|
+
homepage: https://github.com/cavalle/chrome_remote
|
136
|
+
licenses:
|
137
|
+
- MIT
|
138
|
+
metadata: {}
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 2.6.12
|
156
|
+
signing_key:
|
157
|
+
specification_version: 4
|
158
|
+
summary: ChromeRemote is a client implementation of the Chrome DevTools Protocol in
|
159
|
+
Ruby
|
160
|
+
test_files: []
|