Class: Ronin::Web::Browser::Agent
- Inherits:
-
Ferrum::Browser
- Object
- Ferrum::Browser
- Ronin::Web::Browser::Agent
- Defined in:
- lib/ronin/web/browser/agent.rb
Overview
Represents an instance of a Chrome headless or visible browser.
Instance Attribute Summary collapse
-
#proxy ⇒ Hash{Symbol => Object}?
readonly
The configured proxy information.
Class Method Summary collapse
-
.open(**kwargs) {|browser| ... } ⇒ Agent
Opens a new browser.
Instance Method Summary collapse
-
#at(query) ⇒ Ferrum::Node?
Queries the XPath or CSS-path query and returns the first match.
-
#bypass_csp=(mode) ⇒ Object
Enables or disables bypassing CSP.
-
#each_session_cookie {|cookie| ... } ⇒ Enumerator
Enumerates over all session cookies.
-
#every_request {|request| ... } ⇒ Object
Passes every request to the given block.
-
#every_response {|response| ... } ⇒ Object
Passes every response to the given block.
-
#every_url {|url| ... } ⇒ Object
Passes every requested URL to the given block.
-
#every_url_like(pattern) {|url| ... } ⇒ Object
Passes every requested URL that matches the given pattern to the given block.
-
#headless? ⇒ Boolean
Determines whether the browser was opened in headless mode.
-
#initialize(visible: false, headless: !visible,, proxy: Ronin::Support::Network::HTTP.proxy, **kwargs) ⇒ Agent
constructor
Initializes the browser agent.
-
#links ⇒ Array<String>
Queries all
<a href="...">
links in the current page. -
#load_cookies(path) ⇒ Object
Loads the cookies from the cookie file.
-
#on(event) {|request| ... } ⇒ Object
Registers a callback for the given event type.
-
#page_uri ⇒ URI::HTTP
The page's current URI.
-
#proxy? ⇒ Boolean
Determines whether the proxy was initialized with a proxy.
-
#save_cookies(path) ⇒ Object
Saves the cookies to a cookie file.
-
#search(query) ⇒ Array<Ferrum::Node>
Queries the XPath or CSS-path query and returns the matching nodes.
-
#session_cookies ⇒ Array<Ferrum::Cookie>
Fetches all session cookies.
-
#set_cookie(name, value, **options) ⇒ Object
Sets a cookie.
-
#urls ⇒ Array<URI::HTTP, URI::HTTPS>
All link URLs in the current page.
-
#visible? ⇒ Boolean
Determines whether the browser was opened in visible mode.
-
#wait_until_closed ⇒ Object
Waits indefinitely until the browser window is closed.
Constructor Details
#initialize(visible: false, headless: !visible,, proxy: Ronin::Support::Network::HTTP.proxy, **kwargs) ⇒ Agent
Initializes the browser agent.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ronin/web/browser/agent.rb', line 56 def initialize(visible: false, headless: !visible, proxy: Ronin::Support::Network::HTTP.proxy, **kwargs) proxy = case proxy when Hash, nil then proxy when URI::HTTP, Addressable::URI { host: proxy.host, port: proxy.port, user: proxy.user, password: proxy.password } when String uri = URI(proxy) { host: uri.host, port: uri.port, user: uri.user, password: uri.password } else raise(ArgumentError,"invalid proxy value (#{proxy.inspect}), must be either a Hash, URI::HTTP, String, or nil") end @headless = headless @proxy = proxy super(headless: headless, proxy: proxy, **kwargs) end |
Instance Attribute Details
#proxy ⇒ Hash{Symbol => Object}? (readonly)
The configured proxy information.
39 40 41 |
# File 'lib/ronin/web/browser/agent.rb', line 39 def proxy @proxy end |
Class Method Details
.open(**kwargs) {|browser| ... } ⇒ Agent
Opens a new browser.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ronin/web/browser/agent.rb', line 104 def self.open(**kwargs) browser = new(**kwargs) if block_given? yield browser browser.quit end return browser end |
Instance Method Details
#at(query) ⇒ Ferrum::Node?
Queries the XPath or CSS-path query and returns the first match.
313 314 315 316 317 318 319 |
# File 'lib/ronin/web/browser/agent.rb', line 313 def at(query) if query.start_with?('/') at_xpath(query) else at_css(query) end end |
#bypass_csp=(mode) ⇒ Object
Enables or disables bypassing CSP.
146 147 148 149 150 |
# File 'lib/ronin/web/browser/agent.rb', line 146 def bypass_csp=(mode) if mode then bypass_csp(enabled: true) else bypass_csp(enabled: false) end end |
#each_session_cookie {|cookie| ... } ⇒ Enumerator
Enumerates over all session cookies.
358 359 360 361 362 363 364 |
# File 'lib/ronin/web/browser/agent.rb', line 358 def return enum_for(__method__) unless block_given? .each do || yield if .session? end end |
#every_request {|request| ... } ⇒ Object
Passes every request to the given block.
212 213 214 215 216 217 218 219 |
# File 'lib/ronin/web/browser/agent.rb', line 212 def every_request network.intercept on(:request) do |request| yield request request.continue end end |
#every_response {|response| ... } ⇒ Object
Passes every response to the given block.
238 239 240 241 242 243 244 245 246 |
# File 'lib/ronin/web/browser/agent.rb', line 238 def every_response(&block) on(:response) do |exchange,index,total| if block.arity == 2 yield exchange.response, exchange.request else yield exchange.response end end end |
#every_url {|url| ... } ⇒ Object
Passes every requested URL to the given block.
257 258 259 260 261 |
# File 'lib/ronin/web/browser/agent.rb', line 257 def every_url every_request do |request| yield request.url end end |
#every_url_like(pattern) {|url| ... } ⇒ Object
Passes every requested URL that matches the given pattern to the given block.
276 277 278 279 280 281 282 |
# File 'lib/ronin/web/browser/agent.rb', line 276 def every_url_like(pattern) every_url do |url| if pattern.match(url) yield url end end end |
#headless? ⇒ Boolean
Determines whether the browser was opened in headless mode.
120 121 122 |
# File 'lib/ronin/web/browser/agent.rb', line 120 def headless? @headless end |
#links ⇒ Array<String>
Queries all <a href="...">
links in the current page.
326 327 328 |
# File 'lib/ronin/web/browser/agent.rb', line 326 def links xpath('//a/@href').map(&:value) end |
#load_cookies(path) ⇒ Object
Loads the cookies from the cookie file.
398 399 400 401 402 |
# File 'lib/ronin/web/browser/agent.rb', line 398 def (path) CookieFile.new(path).each do || .set() end end |
#on(event) {|request| ... } ⇒ Object
Registers a callback for the given event type.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/ronin/web/browser/agent.rb', line 186 def on(event,&block) case event when :response super('Network.responseReceived') do |params,index,total| exchange = network.select(params['requestId']).last if exchange block.call(exchange,index,total) end end when :close super('Inspector.detached',&block) else super(event,&block) end end |
#page_uri ⇒ URI::HTTP
The page's current URI.
289 290 291 |
# File 'lib/ronin/web/browser/agent.rb', line 289 def page_uri URI.parse(url) end |
#proxy? ⇒ Boolean
Determines whether the proxy was initialized with a proxy.
136 137 138 |
# File 'lib/ronin/web/browser/agent.rb', line 136 def proxy? !@proxy.nil? end |
#save_cookies(path) ⇒ Object
Saves the cookies to a cookie file.
410 411 412 |
# File 'lib/ronin/web/browser/agent.rb', line 410 def (path) CookieFile.save(path,) end |
#search(query) ⇒ Array<Ferrum::Node>
Queries the XPath or CSS-path query and returns the matching nodes.
299 300 301 302 303 304 305 |
# File 'lib/ronin/web/browser/agent.rb', line 299 def search(query) if query.start_with?('/') xpath(query) else css(query) end end |
#session_cookies ⇒ Array<Ferrum::Cookie>
Fetches all session cookies.
372 373 374 |
# File 'lib/ronin/web/browser/agent.rb', line 372 def .to_a end |
#set_cookie(name, value, **options) ⇒ Object
Sets a cookie.
388 389 390 |
# File 'lib/ronin/web/browser/agent.rb', line 388 def (name,value,**) .set(name: name, value: value, **) end |
#urls ⇒ Array<URI::HTTP, URI::HTTPS>
All link URLs in the current page.
335 336 337 338 339 |
# File 'lib/ronin/web/browser/agent.rb', line 335 def urls page_uri = self.page_uri links.map { |link| page_uri.merge(link) } end |
#visible? ⇒ Boolean
Determines whether the browser was opened in visible mode.
129 130 131 |
# File 'lib/ronin/web/browser/agent.rb', line 129 def visible? !@headless end |
#wait_until_closed ⇒ Object
Waits indefinitely until the browser window is closed.
417 418 419 420 421 422 423 424 425 |
# File 'lib/ronin/web/browser/agent.rb', line 417 def wait_until_closed window_closed = false on('Inspector.detached') do window_closed = true end sleep(1) until window_closed end |