Class: Camoufox::SyncAPI::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/camoufox/sync_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(launch_options) ⇒ Page

Returns a new instance of Page.



41
42
43
44
45
46
47
48
# File 'lib/camoufox/sync_api.rb', line 41

def initialize(launch_options)
  @launch_options = launch_options
  @session = NodeRunner::Session.new(@launch_options)
  @title = nil
  @content = nil
  @closed = false
  @on_close = nil
end

Instance Method Details

#closeObject



92
93
94
95
96
97
98
99
# File 'lib/camoufox/sync_api.rb', line 92

def close
  return if closed?

  @session.close
  @closed = true
  @on_close&.call(self)
  nil
end

#closed?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/camoufox/sync_api.rb', line 101

def closed?
  @closed
end

#contentObject



76
77
78
79
80
81
82
# File 'lib/camoufox/sync_api.rb', line 76

def content
  ensure_open!
  @content ||= begin
    result = @session.request('content')
    result['content']&.to_s
  end
end

#goto(url) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/camoufox/sync_api.rb', line 54

def goto(url)
  ensure_open!
  result = @session.request('goto', 'url' => url)
  @title = result['title']
  @content = result['content']&.to_s
  self
end

#on_close(&block) ⇒ Object



50
51
52
# File 'lib/camoufox/sync_api.rb', line 50

def on_close(&block)
  @on_close = block
end

#titleObject



84
85
86
87
88
89
90
# File 'lib/camoufox/sync_api.rb', line 84

def title
  ensure_open!
  @title ||= begin
    result = @session.request('title')
    result['title']
  end
end

#wait_for_selector(selector, **options) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/camoufox/sync_api.rb', line 62

def wait_for_selector(selector, **options)
  ensure_open!
  raise ArgumentError, "selector must be provided" if selector.to_s.empty?

  wait_options = options.compact
  params = { 'selector' => selector }
  params['options'] = Utils.camelize_hash(wait_options) unless wait_options.empty?
  @session.request('wait_for_selector', params)

  @title = nil
  @content = nil
  self
end