Class: Puppeteer::Mouse

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/mouse.rb,
sig/puppeteer/mouse.rbs

Overview

rbs_inline: enabled

Defined Under Namespace

Modules: Button, ButtonFlag

Instance Method Summary collapse

Constructor Details

#initialize(client, keyboard) ⇒ Mouse

Returns a new instance of Mouse.

Parameters:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppeteer/mouse.rb', line 27

def initialize(client, keyboard)
  @client = client
  @keyboard = keyboard

  @base_state = {
    position: {
      x: 0,
      y: 0,
    },
    buttons: ButtonFlag::NONE,
  }
  @transactions = []
  @state_mutex = Mutex.new
  @dispatch_mutex = Mutex.new
end

Instance Method Details

#click(x, y, delay: nil, button: nil, count: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • x (Numeric)
  • y (Numeric)
  • delay: (Numeric, nil) (defaults to: nil)
  • button: (String, nil) (defaults to: nil)
  • count: (Integer, nil) (defaults to: nil)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/puppeteer/mouse.rb', line 113

def click(x, y, delay: nil, button: nil, count: nil)
  count ||= 1
  if count < 1
    raise Puppeteer::Error.new('Click must occur a positive number of times.')
  end
  # Serialize click sequences to keep event ordering stable under thread-based concurrency.
  @dispatch_mutex.synchronize do
    move(x, y)
    1.upto(count - 1) do |i|
      down(button: button, click_count: i)
      up(button: button, click_count: i)
    end
    down(button: button, click_count: count)
    if !delay.nil?
      Puppeteer::AsyncUtils.sleep_seconds(delay / 1000.0)
    end
    up(button: button, click_count: count)
  end
end

#down(button: nil, click_count: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • button: (String, nil) (defaults to: nil)
  • click_count: (Integer, nil) (defaults to: nil)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/puppeteer/mouse.rb', line 138

def down(button: nil, click_count: nil)
  button ||= Button::LEFT
  flag = button_flag(button)
  with_transaction do |update_state|
    update_state.call(
      buttons: state[:buttons] | flag,
    )
    current_state = state
    position = current_state[:position]
    @client.send_message('Input.dispatchMouseEvent',
      type: 'mousePressed',
      modifiers: @keyboard.modifiers,
      clickCount: click_count || 1,
      buttons: current_state[:buttons],
      button: button,
      x: position[:x],
      y: position[:y],
    )
  end
end

#drag(start, target) ⇒ Hash[String, untyped]

Parameters:

Returns:

  • (Hash[String, untyped])


210
211
212
213
214
215
216
217
218
219
220
# File 'lib/puppeteer/mouse.rb', line 210

def drag(start, target)
  promise = Async::Promise.new.tap do |future|
    @client.once('Input.dragIntercepted') do |event|
      future.resolve(event['data'])
    end
  end
  move(start.x, start.y)
  down
  move(target.x, target.y)
  promise.wait
end

#drag_and_drop(start, target, delay: nil) ⇒ void

This method returns an undefined value.

Parameters:



265
266
267
268
269
270
271
272
273
274
# File 'lib/puppeteer/mouse.rb', line 265

def drag_and_drop(start, target, delay: nil)
  data = drag(start, target)
  drag_enter(target, data)
  drag_over(target, data)
  if delay
    Puppeteer::AsyncUtils.sleep_seconds(delay / 1000.0)
  end
  drop(target, data)
  up
end

#drag_enter(target, data) ⇒ void

This method returns an undefined value.

Parameters:



225
226
227
228
229
230
231
232
233
# File 'lib/puppeteer/mouse.rb', line 225

def drag_enter(target, data)
  @client.send_message('Input.dispatchDragEvent',
    type: 'dragEnter',
    x: target.x,
    y: target.y,
    modifiers: @keyboard.modifiers,
    data: data,
  )
end

#drag_over(target, data) ⇒ void

This method returns an undefined value.

Parameters:



238
239
240
241
242
243
244
245
246
# File 'lib/puppeteer/mouse.rb', line 238

def drag_over(target, data)
  @client.send_message('Input.dispatchDragEvent',
    type: 'dragOver',
    x: target.x,
    y: target.y,
    modifiers: @keyboard.modifiers,
    data: data,
  )
end

#drop(target, data) ⇒ void

This method returns an undefined value.

Parameters:



251
252
253
254
255
256
257
258
259
# File 'lib/puppeteer/mouse.rb', line 251

def drop(target, data)
  @client.send_message('Input.dispatchDragEvent',
    type: 'drop',
    x: target.x,
    y: target.y,
    modifiers: @keyboard.modifiers,
    data: data,
  )
end

#move(x, y, steps: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • x (Numeric)
  • y (Numeric)
  • steps: (Integer, nil) (defaults to: nil)


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
# File 'lib/puppeteer/mouse.rb', line 71

def move(x, y, steps: nil)
  move_steps = (steps || 1).to_i

  from = state[:position]
  to = {
    x: x,
    y: y,
  }

  return if move_steps <= 0

  1.upto(move_steps) do |i|
    with_transaction do |update_state|
      update_state.call(
        position: {
          x: from[:x] + (to[:x] - from[:x]) * i / move_steps.to_f,
          y: from[:y] + (to[:y] - from[:y]) * i / move_steps.to_f,
        },
      )
      current_state = state
      buttons = current_state[:buttons]
      position = current_state[:position]
      @client.send_message('Input.dispatchMouseEvent',
        type: 'mouseMoved',
        modifiers: @keyboard.modifiers,
        buttons: buttons,
        button: button_from_pressed_buttons(buttons),
        x: position[:x],
        y: position[:y],
      )
    end
  end
end

#resetvoid

This method returns an undefined value.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppeteer/mouse.rb', line 50

def reset
  [
    [ButtonFlag::RIGHT, Button::RIGHT],
    [ButtonFlag::MIDDLE, Button::MIDDLE],
    [ButtonFlag::LEFT, Button::LEFT],
    [ButtonFlag::FORWARD, Button::FORWARD],
    [ButtonFlag::BACK, Button::BACK],
  ].each do |flag, button|
    up(button: button) if (state[:buttons] & flag) != 0
  end
  if state[:position][:x] != 0 || state[:position][:y] != 0
    move(0, 0)
  end
end

#up(button: nil, click_count: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • button: (String, nil) (defaults to: nil)
  • click_count: (Integer, nil) (defaults to: nil)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/puppeteer/mouse.rb', line 164

def up(button: nil, click_count: nil)
  button ||= Button::LEFT
  flag = button_flag(button)
  with_transaction do |update_state|
    update_state.call(
      buttons: state[:buttons] & ~flag,
    )
    current_state = state
    position = current_state[:position]
    @client.send_message('Input.dispatchMouseEvent',
      type: 'mouseReleased',
      modifiers: @keyboard.modifiers,
      clickCount: click_count || 1,
      buttons: current_state[:buttons],
      button: button,
      x: position[:x],
      y: position[:y],
    )
  end
end

#update_client(client) ⇒ void

This method returns an undefined value.

Parameters:



45
46
47
# File 'lib/puppeteer/mouse.rb', line 45

def update_client(client)
  @client = client
end

#wheel(delta_x: 0, delta_y: 0) ⇒ void

This method returns an undefined value.

Dispatches a mousewheel event.

Parameters:

  • delta_x: (Numeric) (defaults to: 0)
  • delta_y: (Numeric) (defaults to: 0)


192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/puppeteer/mouse.rb', line 192

def wheel(delta_x: 0, delta_y: 0)
  current_state = state
  position = current_state[:position]
  @client.send_message('Input.dispatchMouseEvent',
    type: 'mouseWheel',
    x: position[:x],
    y: position[:y],
    deltaX: delta_x,
    deltaY: delta_y,
    modifiers: @keyboard.modifiers,
    pointerType: 'mouse',
    buttons: current_state[:buttons],
  )
end