Class: Puppeteer::Bidi::Core::Navigation

Inherits:
EventEmitter show all
Includes:
Disposable::DisposableMixin
Defined in:
lib/puppeteer/bidi/core/navigation.rb,
sig/puppeteer/bidi/core/navigation.rbs

Overview

Navigation represents a single navigation operation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Disposable::DisposableMixin

#dispose, #disposed?

Methods inherited from EventEmitter

#dispose, #disposed?, #emit, #off, #on, #once, #remove_all_listeners

Constructor Details

#initialize(browsing_context) ⇒ Navigation

Returns a new instance of Navigation.

Parameters:

  • browsing_context (Object)


22
23
24
25
26
27
28
29
# File 'lib/puppeteer/bidi/core/navigation.rb', line 22

def initialize(browsing_context)
  super()
  @browsing_context = browsing_context
  @request = nil
  @navigation = nil
  @id = nil
  @disposables = Disposable::DisposableStack.new
end

Instance Attribute Details

#browsing_contextObject (readonly)

Returns the value of attribute browsing_context.

Returns:

  • (Object)


20
21
22
# File 'lib/puppeteer/bidi/core/navigation.rb', line 20

def browsing_context
  @browsing_context
end

#requestObject (readonly)

Returns the value of attribute request.

Returns:

  • (Object)


20
21
22
# File 'lib/puppeteer/bidi/core/navigation.rb', line 20

def request
  @request
end

Class Method Details

.from(browsing_context) ⇒ Navigation

Create a navigation instance

Parameters:

Returns:



14
15
16
17
18
# File 'lib/puppeteer/bidi/core/navigation.rb', line 14

def self.from(browsing_context)
  navigation = new(browsing_context)
  navigation.send(:initialize_navigation)
  navigation
end

Instance Method Details

#initialize_navigationObject

Returns:

  • (Object)


50
51
52
53
54
55
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/puppeteer/bidi/core/navigation.rb', line 50

def initialize_navigation
  # Listen for browsing context closure
  @browsing_context.once(:closed) do
    emit(:failed, {
      url: @browsing_context.url,
      timestamp: Time.now
    })
    dispose
  end

  # Listen for requests with navigation ID
  @browsing_context.on(:request) do |request|
    next unless request.navigation
    next unless matches?(request.navigation)

    @request = request
    emit(:request, request)

    # Listen for redirects
    request.on(:redirect) do |redirect_request|
      @request = redirect_request
    end
  end

  # Listen for nested navigation
  session.on('browsingContext.navigationStarted') do |info|
    next unless info['context'] == @browsing_context.id
    next if @navigation && !@navigation.disposed?

    @navigation = Navigation.from(@browsing_context)
  end

  # Listen for navigation completion events
  %w[
    browsingContext.domContentLoaded
    browsingContext.load
    browsingContext.navigationCommitted
  ].each do |event_name|
    session.on(event_name) do |info|
      next unless info['context'] == @browsing_context.id
      next if info['navigation'].nil?
      next unless matches?(info['navigation'])

      dispose
    end
  end

  # Listen for navigation end events
  {
    'browsingContext.fragmentNavigated' => :fragment,
    'browsingContext.navigationFailed' => :failed,
    'browsingContext.navigationAborted' => :aborted
  }.each do |event_name, emit_event|
    session.on(event_name) do |info|
      next unless info['context'] == @browsing_context.id
      next unless matches?(info['navigation'])

      emit(emit_event, {
        url: info['url'],
        timestamp: Time.at(info['timestamp'] / 1000.0)
      })
      dispose
    end
  end
end

#matches?(navigation_id) ⇒ Boolean

Parameters:

  • navigation_id (Object)

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/puppeteer/bidi/core/navigation.rb', line 116

def matches?(navigation_id)
  # If nested navigation exists and is not disposed, this navigation doesn't match
  return false if @navigation && !@navigation.disposed?

  # First navigation event sets the ID
  if @id.nil?
    @id = navigation_id
    return true
  end

  # Check if the navigation ID matches
  @id == navigation_id
end

Get the nested navigation if any

Returns:



33
34
35
# File 'lib/puppeteer/bidi/core/navigation.rb', line 33

def navigation
  @navigation
end

#perform_disposeObject

Returns:

  • (Object)


39
40
41
42
# File 'lib/puppeteer/bidi/core/navigation.rb', line 39

def perform_dispose
  @disposables.dispose
  super
end

#sessionObject

Returns:

  • (Object)


46
47
48
# File 'lib/puppeteer/bidi/core/navigation.rb', line 46

def session
  @browsing_context.user_context.browser.session
end