Class: Relaton::Oasis::BrowserAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/oasis/browser_agent.rb

Overview

Thin Ferrum-backed agent that drives headless Chrome with stealth tweaks so the Cloudflare-protected oasis-open.org host serves real HTML instead of a “Just a moment…” challenge. Mirrors the pattern used by Relaton::Cie::BrowserAgent.

Constant Summary collapse

UA =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 " \
"(KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
CHALLENGE_MARKERS =
["Just a moment", "challenge-platform"].freeze
MAX_CHALLENGE_WAIT =
30

Instance Method Summary collapse

Constructor Details

#initializeBrowserAgent

Returns a new instance of BrowserAgent.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/relaton/oasis/browser_agent.rb', line 16

def initialize
  @browser = Ferrum::Browser.new(
    headless: true,
    timeout: 90,
    process_timeout: 90,
    window_size: [1366, 768],
    browser_options: {
      "disable-blink-features" => "AutomationControlled",
      "disable-quic" => nil,
      "no-sandbox" => nil,
    },
  )
  @browser.headers.set(
    "Accept-Language" => "en-US,en;q=0.9",
    "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9," \
                "image/webp,*/*;q=0.8",
    "User-Agent" => UA,
  )
  @browser.evaluate_on_new_document(<<~JS)
    Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
    Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
    Object.defineProperty(navigator, 'plugins', { get: () => [1,2,3,4,5] });
    window.chrome = { runtime: {} };
  JS
end

Instance Method Details

#get(url) ⇒ Object



42
43
44
45
46
# File 'lib/relaton/oasis/browser_agent.rb', line 42

def get(url)
  @browser.go_to(url)
  wait_for_challenge
  Nokogiri::HTML(@browser.body)
end

#last_statusObject

HTTP status code of the most recent navigation’s main resource, or nil if no navigation has happened yet.



50
51
52
# File 'lib/relaton/oasis/browser_agent.rb', line 50

def last_status
  @browser&.network&.status
end

#quitObject



54
55
56
57
58
# File 'lib/relaton/oasis/browser_agent.rb', line 54

def quit
  @browser&.quit
ensure
  @browser = nil
end