puppeteer-ruby 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +3 -0
- data/.rubocop.yml +36 -0
- data/.travis.yml +7 -0
- data/Dockerfile +6 -0
- data/Gemfile +6 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/docker-compose.yml +15 -0
- data/example.rb +7 -0
- data/lib/puppeteer.rb +192 -0
- data/lib/puppeteer/async_await_behavior.rb +34 -0
- data/lib/puppeteer/browser.rb +240 -0
- data/lib/puppeteer/browser_context.rb +90 -0
- data/lib/puppeteer/browser_fetcher.rb +6 -0
- data/lib/puppeteer/browser_runner.rb +142 -0
- data/lib/puppeteer/cdp_session.rb +78 -0
- data/lib/puppeteer/concurrent_ruby_utils.rb +37 -0
- data/lib/puppeteer/connection.rb +254 -0
- data/lib/puppeteer/console_message.rb +24 -0
- data/lib/puppeteer/debug_print.rb +20 -0
- data/lib/puppeteer/device.rb +12 -0
- data/lib/puppeteer/devices.rb +885 -0
- data/lib/puppeteer/dom_world.rb +447 -0
- data/lib/puppeteer/element_handle.rb +433 -0
- data/lib/puppeteer/emulation_manager.rb +46 -0
- data/lib/puppeteer/errors.rb +4 -0
- data/lib/puppeteer/event_callbackable.rb +88 -0
- data/lib/puppeteer/execution_context.rb +230 -0
- data/lib/puppeteer/frame.rb +278 -0
- data/lib/puppeteer/frame_manager.rb +380 -0
- data/lib/puppeteer/if_present.rb +18 -0
- data/lib/puppeteer/js_handle.rb +142 -0
- data/lib/puppeteer/keyboard.rb +183 -0
- data/lib/puppeteer/keyboard/key_description.rb +19 -0
- data/lib/puppeteer/keyboard/us_keyboard_layout.rb +283 -0
- data/lib/puppeteer/launcher.rb +26 -0
- data/lib/puppeteer/launcher/base.rb +48 -0
- data/lib/puppeteer/launcher/browser_options.rb +41 -0
- data/lib/puppeteer/launcher/chrome.rb +165 -0
- data/lib/puppeteer/launcher/chrome_arg_options.rb +49 -0
- data/lib/puppeteer/launcher/launch_options.rb +68 -0
- data/lib/puppeteer/lifecycle_watcher.rb +168 -0
- data/lib/puppeteer/mouse.rb +120 -0
- data/lib/puppeteer/network_manager.rb +122 -0
- data/lib/puppeteer/page.rb +1001 -0
- data/lib/puppeteer/page/screenshot_options.rb +78 -0
- data/lib/puppeteer/remote_object.rb +124 -0
- data/lib/puppeteer/target.rb +150 -0
- data/lib/puppeteer/timeout_settings.rb +15 -0
- data/lib/puppeteer/touch_screen.rb +43 -0
- data/lib/puppeteer/version.rb +3 -0
- data/lib/puppeteer/viewport.rb +36 -0
- data/lib/puppeteer/wait_task.rb +6 -0
- data/lib/puppeteer/web_socket.rb +117 -0
- data/lib/puppeteer/web_socket_transport.rb +49 -0
- data/puppeteer-ruby.gemspec +29 -0
- metadata +213 -0
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'websocket/driver'
|
3
|
+
|
4
|
+
# ref: https://github.com/rails/rails/blob/master/actioncable/lib/action_cable/connection/client_socket.rb
|
5
|
+
# ref: https://github.com/cavalle/chrome_remote/blob/master/lib/chrome_remote/web_socket_client.rb
|
6
|
+
class Puppeteer::WebSocket
|
7
|
+
class DriverImpl # providing #url, #write(string)
|
8
|
+
def initialize(url)
|
9
|
+
@url = url
|
10
|
+
|
11
|
+
endpoint = URI.parse(url)
|
12
|
+
@socket = TCPSocket.new(endpoint.host, endpoint.port)
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :url
|
16
|
+
|
17
|
+
def write(data)
|
18
|
+
@socket.write(data)
|
19
|
+
end
|
20
|
+
|
21
|
+
def readpartial(maxlen = 1024)
|
22
|
+
@socket.readpartial(maxlen)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
STATE_CONNECTING = 0
|
27
|
+
STATE_OPENED = 1
|
28
|
+
STATE_CLOSING = 2
|
29
|
+
STATE_CLOSED = 3
|
30
|
+
|
31
|
+
def initialize(url:, max_payload_size:)
|
32
|
+
@impl = DriverImpl.new(url)
|
33
|
+
@driver = ::WebSocket::Driver.client(@impl, max_length: max_payload_size)
|
34
|
+
|
35
|
+
setup
|
36
|
+
@driver.start
|
37
|
+
|
38
|
+
Thread.new do
|
39
|
+
wait_for_data until @ready_state >= STATE_CLOSING
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private def setup
|
44
|
+
@ready_state = STATE_CONNECTING
|
45
|
+
@driver.on(:open) do
|
46
|
+
@ready_state = STATE_OPENED
|
47
|
+
handle_on_open
|
48
|
+
end
|
49
|
+
@driver.on(:close) do |event|
|
50
|
+
@ready_state = STATE_CLOSED
|
51
|
+
handle_on_close(reason: event.reason, code: event.code)
|
52
|
+
end
|
53
|
+
@driver.on(:error) do |event|
|
54
|
+
if !handle_on_error(error_message: event.message)
|
55
|
+
raise Puppeteer::WebSocktTransportError.new(event.message)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
@driver.on(:message) do |event|
|
59
|
+
handle_on_message(event.data)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private def wait_for_data
|
64
|
+
@driver.parse(@impl.readpartial)
|
65
|
+
end
|
66
|
+
|
67
|
+
# @param message [String]
|
68
|
+
def send_text(message)
|
69
|
+
return if @ready_state >= STATE_CLOSING
|
70
|
+
@driver.text(message)
|
71
|
+
end
|
72
|
+
|
73
|
+
def close(code: 1000, reason: "")
|
74
|
+
return if @ready_state >= STATE_CLOSING
|
75
|
+
@ready_state = STATE_CLOSING
|
76
|
+
@driver.close(reason, code)
|
77
|
+
end
|
78
|
+
|
79
|
+
def on_open(&block)
|
80
|
+
@on_open = block
|
81
|
+
end
|
82
|
+
|
83
|
+
# @param block [Proc(reason: String, code: Numeric)]
|
84
|
+
def on_close(&block)
|
85
|
+
@on_close = block
|
86
|
+
end
|
87
|
+
|
88
|
+
# @param block [Proc(error_message: String)]
|
89
|
+
def on_error(&block)
|
90
|
+
@on_error = block
|
91
|
+
end
|
92
|
+
|
93
|
+
def on_message(&block)
|
94
|
+
@on_message = block
|
95
|
+
end
|
96
|
+
|
97
|
+
private def handle_on_open
|
98
|
+
@on_open&.call
|
99
|
+
end
|
100
|
+
|
101
|
+
private def handle_on_close(reason:, code:)
|
102
|
+
@on_close&.call(reason, code)
|
103
|
+
end
|
104
|
+
|
105
|
+
private def handle_on_error(error_message:)
|
106
|
+
return false if @on_error.nil?
|
107
|
+
|
108
|
+
@on_error.call(error_message)
|
109
|
+
true
|
110
|
+
end
|
111
|
+
|
112
|
+
private def handle_on_message(data)
|
113
|
+
return if @ready_state != STATE_OPENED
|
114
|
+
|
115
|
+
@on_message&.call(data)
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class Puppeteer::WebSocketTransport
|
2
|
+
# @param {string} url
|
3
|
+
# @return [Puppeteer::WebSocketTransport]
|
4
|
+
def self.create(url)
|
5
|
+
ws = Puppeteer::WebSocket.new(
|
6
|
+
url: url,
|
7
|
+
max_payload_size: 256 * 1024 * 1024, # 256MB
|
8
|
+
)
|
9
|
+
promise = resolvable_future
|
10
|
+
ws.on_open do
|
11
|
+
promise.fulfill(Puppeteer::WebSocketTransport.new(ws))
|
12
|
+
end
|
13
|
+
ws.on_error do |error_message|
|
14
|
+
promise.reject(Puppeteer::WebSocktTransportError.new(error_message))
|
15
|
+
end
|
16
|
+
promise.value!
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param {!WebSocket::Driver} web_socket
|
20
|
+
def initialize(web_socket)
|
21
|
+
@ws = web_socket
|
22
|
+
@ws.on_message do |data|
|
23
|
+
@on_message&.call(data)
|
24
|
+
end
|
25
|
+
@ws.on_close do |reason, code|
|
26
|
+
@on_close&.call(reason, code)
|
27
|
+
end
|
28
|
+
@ws.on_error do |error|
|
29
|
+
# Silently ignore all errors - we don't know what to do with them.
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# @param message [String]
|
34
|
+
def send_text(message)
|
35
|
+
@ws.send_text(message)
|
36
|
+
end
|
37
|
+
|
38
|
+
def close
|
39
|
+
@ws.close
|
40
|
+
end
|
41
|
+
|
42
|
+
def on_close(&block)
|
43
|
+
@on_close = block
|
44
|
+
end
|
45
|
+
|
46
|
+
def on_message(&block)
|
47
|
+
@on_message = block
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'puppeteer/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'puppeteer-ruby'
|
7
|
+
spec.version = Puppeteer::VERSION
|
8
|
+
spec.authors = ['YusukeIwaki']
|
9
|
+
spec.email = ['q7w8e9w8q7w8e9@yahoo.co.jp']
|
10
|
+
|
11
|
+
spec.summary = 'A ruby port of puppeteer'
|
12
|
+
spec.homepage = 'https://github.com/YusukeIwaki/puppeteer-ruby'
|
13
|
+
|
14
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
15
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
end
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'concurrent-ruby', "~> 1.1.0"
|
22
|
+
spec.add_dependency 'websocket-driver', '>= 0.6.0'
|
23
|
+
spec.add_dependency 'mime-types', '>= 3.0'
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
25
|
+
spec.add_development_dependency 'pry'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
|
+
spec.add_development_dependency 'rubocop', '~> 0.80.0'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: puppeteer-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- YusukeIwaki
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: concurrent-ruby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: websocket-driver
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.6.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.6.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mime-types
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.17'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.17'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.80.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.80.0
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- q7w8e9w8q7w8e9@yahoo.co.jp
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".rubocop.yml"
|
135
|
+
- ".travis.yml"
|
136
|
+
- Dockerfile
|
137
|
+
- Gemfile
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- docker-compose.yml
|
143
|
+
- example.rb
|
144
|
+
- lib/puppeteer.rb
|
145
|
+
- lib/puppeteer/async_await_behavior.rb
|
146
|
+
- lib/puppeteer/browser.rb
|
147
|
+
- lib/puppeteer/browser_context.rb
|
148
|
+
- lib/puppeteer/browser_fetcher.rb
|
149
|
+
- lib/puppeteer/browser_runner.rb
|
150
|
+
- lib/puppeteer/cdp_session.rb
|
151
|
+
- lib/puppeteer/concurrent_ruby_utils.rb
|
152
|
+
- lib/puppeteer/connection.rb
|
153
|
+
- lib/puppeteer/console_message.rb
|
154
|
+
- lib/puppeteer/debug_print.rb
|
155
|
+
- lib/puppeteer/device.rb
|
156
|
+
- lib/puppeteer/devices.rb
|
157
|
+
- lib/puppeteer/dom_world.rb
|
158
|
+
- lib/puppeteer/element_handle.rb
|
159
|
+
- lib/puppeteer/emulation_manager.rb
|
160
|
+
- lib/puppeteer/errors.rb
|
161
|
+
- lib/puppeteer/event_callbackable.rb
|
162
|
+
- lib/puppeteer/execution_context.rb
|
163
|
+
- lib/puppeteer/frame.rb
|
164
|
+
- lib/puppeteer/frame_manager.rb
|
165
|
+
- lib/puppeteer/if_present.rb
|
166
|
+
- lib/puppeteer/js_handle.rb
|
167
|
+
- lib/puppeteer/keyboard.rb
|
168
|
+
- lib/puppeteer/keyboard/key_description.rb
|
169
|
+
- lib/puppeteer/keyboard/us_keyboard_layout.rb
|
170
|
+
- lib/puppeteer/launcher.rb
|
171
|
+
- lib/puppeteer/launcher/base.rb
|
172
|
+
- lib/puppeteer/launcher/browser_options.rb
|
173
|
+
- lib/puppeteer/launcher/chrome.rb
|
174
|
+
- lib/puppeteer/launcher/chrome_arg_options.rb
|
175
|
+
- lib/puppeteer/launcher/launch_options.rb
|
176
|
+
- lib/puppeteer/lifecycle_watcher.rb
|
177
|
+
- lib/puppeteer/mouse.rb
|
178
|
+
- lib/puppeteer/network_manager.rb
|
179
|
+
- lib/puppeteer/page.rb
|
180
|
+
- lib/puppeteer/page/screenshot_options.rb
|
181
|
+
- lib/puppeteer/remote_object.rb
|
182
|
+
- lib/puppeteer/target.rb
|
183
|
+
- lib/puppeteer/timeout_settings.rb
|
184
|
+
- lib/puppeteer/touch_screen.rb
|
185
|
+
- lib/puppeteer/version.rb
|
186
|
+
- lib/puppeteer/viewport.rb
|
187
|
+
- lib/puppeteer/wait_task.rb
|
188
|
+
- lib/puppeteer/web_socket.rb
|
189
|
+
- lib/puppeteer/web_socket_transport.rb
|
190
|
+
- puppeteer-ruby.gemspec
|
191
|
+
homepage: https://github.com/YusukeIwaki/puppeteer-ruby
|
192
|
+
licenses: []
|
193
|
+
metadata: {}
|
194
|
+
post_install_message:
|
195
|
+
rdoc_options: []
|
196
|
+
require_paths:
|
197
|
+
- lib
|
198
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
199
|
+
requirements:
|
200
|
+
- - ">="
|
201
|
+
- !ruby/object:Gem::Version
|
202
|
+
version: '0'
|
203
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
204
|
+
requirements:
|
205
|
+
- - ">="
|
206
|
+
- !ruby/object:Gem::Version
|
207
|
+
version: '0'
|
208
|
+
requirements: []
|
209
|
+
rubygems_version: 3.0.3
|
210
|
+
signing_key:
|
211
|
+
specification_version: 4
|
212
|
+
summary: A ruby port of puppeteer
|
213
|
+
test_files: []
|