andrewhao-chrome_remote 0.2.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 +30 -0
- data/examples/network_dump_and_screenshot.rb +21 -0
- data/lib/chrome_remote.rb +31 -0
- data/lib/chrome_remote/client.rb +70 -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 +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6616c7823cff2bdf02b5e03e4744b1262701cf5c962c1b9a69851b307789aa9c
|
4
|
+
data.tar.gz: 32601cc6180df55a7df5191c18522ac1c5186eea77de01a913925c7880c32c75
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 54bb5570a24d5983df28f88aa6759c7256b81fdb1334fda253c83b4901c4ef673bca66cbe9071827834f33974d6a8d97e22c040942ed7d8fcbbecaeaceda3ee3
|
7
|
+
data.tar.gz: cf167c78c14c67a24f4dbf3cfca44a7a16df32156874a814277ecedd634de5bf3f4cce47324e3fefedb1e582db274151e96581d59bcfa034c25bde286af97253
|
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: 9222 # optional, default: 9222
|
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,30 @@
|
|
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 = "andrewhao-chrome_remote"
|
8
|
+
spec.version = ChromeRemote::VERSION
|
9
|
+
spec.authors = ["Luismi Cavalle", "Andrew Hao"]
|
10
|
+
spec.email = ["luismi@lmcavalle.com", "andrew@carbonfive.com"]
|
11
|
+
|
12
|
+
spec.summary = "ChromeRemote is a client implementation of the Chrome DevTools Protocol in Ruby"
|
13
|
+
spec.homepage = "https://github.com/andrewhao/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
|
+
spec.add_dependency "hashie", "~> 3.5"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "em-websocket", "~> 0.5"
|
28
|
+
spec.add_development_dependency "byebug"
|
29
|
+
spec.add_development_dependency "webmock"
|
30
|
+
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,70 @@
|
|
1
|
+
require "chrome_remote/web_socket_client"
|
2
|
+
require 'hashie/mash'
|
3
|
+
|
4
|
+
module ChromeRemote
|
5
|
+
class Client
|
6
|
+
attr_reader :ws, :handlers
|
7
|
+
|
8
|
+
def initialize(ws_url)
|
9
|
+
@ws = WebSocketClient.new(ws_url)
|
10
|
+
@handlers = Hash.new { |hash, key| hash[key] = [] }
|
11
|
+
end
|
12
|
+
|
13
|
+
def send_cmd(command, params = {})
|
14
|
+
msg_id = generate_unique_id
|
15
|
+
|
16
|
+
ws.send_msg({method: command, params: params, id: msg_id}.to_json)
|
17
|
+
|
18
|
+
msg = read_until { |msg| msg["id"] == msg_id }
|
19
|
+
Hashie::Mash.new(msg["result"])
|
20
|
+
end
|
21
|
+
|
22
|
+
def on(event_name, &block)
|
23
|
+
handlers[event_name] << block
|
24
|
+
end
|
25
|
+
|
26
|
+
def listen_until(&block)
|
27
|
+
read_until { block.call }
|
28
|
+
end
|
29
|
+
|
30
|
+
def listen
|
31
|
+
read_until { false }
|
32
|
+
end
|
33
|
+
|
34
|
+
def wait_for(event_name=nil)
|
35
|
+
if event_name
|
36
|
+
msg = read_until { |msg| msg["method"] == event_name }
|
37
|
+
elsif block_given?
|
38
|
+
msg = read_until { |msg| yield(msg["method"], msg["params"]) }
|
39
|
+
end
|
40
|
+
Hashie::Mash.new(msg["params"])
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def generate_unique_id
|
46
|
+
@last_id ||= 0
|
47
|
+
@last_id += 1
|
48
|
+
end
|
49
|
+
|
50
|
+
def read_msg
|
51
|
+
msg = JSON.parse(ws.read_msg)
|
52
|
+
|
53
|
+
# Check if it’s an event and invoke any handlers
|
54
|
+
if event_name = msg["method"]
|
55
|
+
handlers[event_name].each do |handler|
|
56
|
+
handler.call(msg["params"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
msg
|
61
|
+
end
|
62
|
+
|
63
|
+
def read_until(&block)
|
64
|
+
loop do
|
65
|
+
msg = read_msg
|
66
|
+
return msg if block.call(msg)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
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,176 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: andrewhao-chrome_remote
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Luismi Cavalle
|
8
|
+
- Andrew Hao
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-01-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: websocket-driver
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0.6'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0.6'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: hashie
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '3.5'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '3.5'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: bundler
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.15'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.15'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rake
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '10.0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '10.0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rspec
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '3.0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '3.0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: em-websocket
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0.5'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0.5'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: byebug
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: webmock
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
description:
|
127
|
+
email:
|
128
|
+
- luismi@lmcavalle.com
|
129
|
+
- andrew@carbonfive.com
|
130
|
+
executables: []
|
131
|
+
extensions: []
|
132
|
+
extra_rdoc_files: []
|
133
|
+
files:
|
134
|
+
- ".gitignore"
|
135
|
+
- ".rspec"
|
136
|
+
- ".travis.yml"
|
137
|
+
- CODE_OF_CONDUCT.md
|
138
|
+
- Gemfile
|
139
|
+
- LICENSE.txt
|
140
|
+
- README.md
|
141
|
+
- Rakefile
|
142
|
+
- bin/console
|
143
|
+
- bin/setup
|
144
|
+
- chrome_remote.gemspec
|
145
|
+
- examples/network_dump_and_screenshot.rb
|
146
|
+
- lib/chrome_remote.rb
|
147
|
+
- lib/chrome_remote/client.rb
|
148
|
+
- lib/chrome_remote/socket.rb
|
149
|
+
- lib/chrome_remote/version.rb
|
150
|
+
- lib/chrome_remote/web_socket_client.rb
|
151
|
+
homepage: https://github.com/andrewhao/chrome_remote
|
152
|
+
licenses:
|
153
|
+
- MIT
|
154
|
+
metadata: {}
|
155
|
+
post_install_message:
|
156
|
+
rdoc_options: []
|
157
|
+
require_paths:
|
158
|
+
- lib
|
159
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
requirements: []
|
170
|
+
rubyforge_project:
|
171
|
+
rubygems_version: 2.7.3
|
172
|
+
signing_key:
|
173
|
+
specification_version: 4
|
174
|
+
summary: ChromeRemote is a client implementation of the Chrome DevTools Protocol in
|
175
|
+
Ruby
|
176
|
+
test_files: []
|