Skip to content

Commit

Permalink
implement Page#geolocation= and BrowserContext#override_permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeIwaki committed Dec 7, 2020
1 parent 578b940 commit 72c08e6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 49 deletions.
1 change: 1 addition & 0 deletions lib/puppeteer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Puppeteer; end
require 'puppeteer/device'
require 'puppeteer/events'
require 'puppeteer/errors'
require 'puppeteer/geolocation'
require 'puppeteer/viewport'

# Modules
Expand Down
78 changes: 42 additions & 36 deletions lib/puppeteer/browser_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,42 +57,48 @@ def incognito?
!!@id
end

# /**
# * @param {string} origin
# * @param {!Array<string>} permissions
# */
# async overridePermissions(origin, permissions) {
# const webPermissionToProtocol = new Map([
# ['geolocation', 'geolocation'],
# ['midi', 'midi'],
# ['notifications', 'notifications'],
# ['push', 'push'],
# ['camera', 'videoCapture'],
# ['microphone', 'audioCapture'],
# ['background-sync', 'backgroundSync'],
# ['ambient-light-sensor', 'sensors'],
# ['accelerometer', 'sensors'],
# ['gyroscope', 'sensors'],
# ['magnetometer', 'sensors'],
# ['accessibility-events', 'accessibilityEvents'],
# ['clipboard-read', 'clipboardRead'],
# ['clipboard-write', 'clipboardWrite'],
# ['payment-handler', 'paymentHandler'],
# // chrome-specific permissions we have.
# ['midi-sysex', 'midiSysex'],
# ]);
# permissions = permissions.map(permission => {
# const protocolPermission = webPermissionToProtocol.get(permission);
# if (!protocolPermission)
# throw new Error('Unknown permission: ' + permission);
# return protocolPermission;
# });
# await this._connection.send('Browser.grantPermissions', {origin, browserContextId: this._id || undefined, permissions});
# }

# async clearPermissionOverrides() {
# await this._connection.send('Browser.resetPermissions', {browserContextId: this._id || undefined});
# }
WEB_PERMISSION_TO_PROTOCOL = {
'geolocation' => 'geolocation',
'midi' => 'midi',
'notifications' => 'notifications',
# TODO: push isn't a valid type?
# 'push' => 'push',
'camera' => 'videoCapture',
'microphone' => 'audioCapture',
'background-sync' => 'backgroundSync',
'ambient-light-sensor' => 'sensors',
'accelerometer' => 'sensors',
'gyroscope' => 'sensors',
'magnetometer' => 'sensors',
'accessibility-events' => 'accessibilityEvents',
'clipboard-read' => 'clipboardReadWrite',
'clipboard-write' => 'clipboardReadWrite',
'payment-handler' => 'paymentHandler',
'idle-detection' => 'idleDetection',
# chrome-specific permissions we have.
'midi-sysex' => 'midiSysex',
}.freeze

# @param origin [String]
# @param permissions [Array<String>]
def override_permissions(origin, permissions)
protocol_permissions = permissions.map do |permission|
WEB_PERMISSION_TO_PROTOCOL[permission] or raise ArgumentError.new("Unknown permission: #{permission}")
end
@connection.send_message('Browser.grantPermissions', {
origin: origin,
browserContextId: @id,
permissions: protocol_permissions,
}.compact)
end

def clear_permission_overrides
if @id
@connection.send_message('Browser.resetPermissions', browserContextId: @id)
else
@connection.send_message('Browser.resetPermissions')
end
end

# @return [Future<Puppeteer::Page>]
def new_page
Expand Down
24 changes: 24 additions & 0 deletions lib/puppeteer/geolocation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Puppeteer::Geolocation
# @param latitude [Fixnum]
# @param longitude [Fixnum]
# @param accuracy [Fixnum]
def initialize(latitude:, longitude:, accuracy: 0)
unless (-180..180).include?(longitude)
raise ArgumentError.new("Invalid longitude \"#{longitude}\": precondition -180 <= LONGITUDE <= 180 failed.")
end
unless (-90..90).include?(latitude)
raise ArgumentError.new("Invalid latitude \"#{latitude}\": precondition -90 <= LATITUDE <= 90 failed.")
end
if accuracy < 0
raise ArgumentError.new("Invalid accuracy \"#{longitude}\": precondition 0 <= ACCURACY failed.")
end

@latitude = latitude
@longitude = longitude
@accuracy = accuracy
end

def to_h
{ latitude: @latitude, longitude: @longitude, accuracy: @accuracy }
end
end
17 changes: 4 additions & 13 deletions lib/puppeteer/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,10 @@ def wait_for_file_chooser(timeout: nil)

define_async_method :async_wait_for_file_chooser

# /**
# * @param {!{longitude: number, latitude: number, accuracy: (number|undefined)}} options
# */
# async setGeolocation(options) {
# const { longitude, latitude, accuracy = 0} = options;
# if (longitude < -180 || longitude > 180)
# throw new Error(`Invalid longitude "${longitude}": precondition -180 <= LONGITUDE <= 180 failed.`);
# if (latitude < -90 || latitude > 90)
# throw new Error(`Invalid latitude "${latitude}": precondition -90 <= LATITUDE <= 90 failed.`);
# if (accuracy < 0)
# throw new Error(`Invalid accuracy "${accuracy}": precondition 0 <= ACCURACY failed.`);
# await this._client.send('Emulation.setGeolocationOverride', {longitude, latitude, accuracy});
# }
# @param [Puppeteer::Geolocation]
def geolocation=(geolocation)
@client.send_message('Emulation.setGeolocationOverride', geolocation.to_h)
end

attr_reader :javascript_enabled, :target

Expand Down

0 comments on commit 72c08e6

Please sign in to comment.