Class: Browser::Navigator

Inherits:
Object show all
Includes:
NativeCachedWrapper
Defined in:
opal/browser/navigator.rb

Overview

Representation of the navigator application.

Defined Under Namespace

Classes: MimeType, Plugin, Plugins, Position, Product, Vendor, Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NativeCachedWrapper

#restricted?, #set_native_reference

Instance Attribute Details

#codeString (readonly)



88
# File 'opal/browser/navigator.rb', line 88

alias_native :code, :appCodeName

#languageString (readonly)



110
# File 'opal/browser/navigator.rb', line 110

alias_native :language

#mime_typesNative::Array<MimeType> (readonly)



114
115
116
117
118
# File 'opal/browser/navigator.rb', line 114

def mime_types
  Native::Array.new `#@native.mimeTypes`, get: :item, named: :namedItem do |m|
    MimeType.new(m)
  end
end

#nameString (readonly)



92
# File 'opal/browser/navigator.rb', line 92

alias_native :name, :appName

#operating_systemString (readonly) Also known as: os



127
# File 'opal/browser/navigator.rb', line 127

alias_native :operating_system, :oscpu

#platformString (readonly)



133
# File 'opal/browser/navigator.rb', line 133

alias_native :platform

#pluginsPlugins (readonly)



137
138
139
# File 'opal/browser/navigator.rb', line 137

def plugins
  Plugins.new(`#@native.plugins`)
end

#productProduct (readonly)



143
144
145
# File 'opal/browser/navigator.rb', line 143

def product
  Product.new(`#@native.product`, `#@native.productSub`)
end

#user_agentString (readonly)



149
# File 'opal/browser/navigator.rb', line 149

alias_native :user_agent, :userAgent

#vendorVendor (readonly)



153
154
155
# File 'opal/browser/navigator.rb', line 153

def vendor
  Vendor.new(`#@native.vendor`, `#@native.vendorSub`)
end

#versionVersion (readonly)



96
97
98
# File 'opal/browser/navigator.rb', line 96

def version
  Version.new(`#@native.appVersion`, `#@native.appMinorVersion`, `#@native.buildID`)
end

Instance Method Details

#geolocate(max_age: 0, timeout: Float::INFINITY, high_accuracy: false) ⇒ Promise

Geolocates the user once



207
208
209
210
211
212
213
214
# File 'opal/browser/navigator.rb', line 207

def geolocate(max_age: 0, timeout: Float::INFINITY, high_accuracy: false)
  promise = Promise.new
  succ = proc { |i| promise.resolve(Position.new(i)) }
  fail = proc { |i| promise.reject(Native(i)) }
  opts = {maxAge: max_age, timeout: timeout, enableHighAccuracy: high_accuracy}
  `#@native.geolocation.getCurrentPosition(#{succ.to_n}, #{fail.to_n}, #{opts.to_n})`
  promise
end

#get_batteryPromise

Check a battery status of user device. This API is deprecated in the browser context and usable mainly in privileged contexts.



247
248
249
250
251
252
253
# File 'opal/browser/navigator.rb', line 247

def get_battery
  promise = Promise.new
  yes = proc { |r| promise.resolve(Native(r)) }
  no = proc { |r| promise.reject(Native(r)) }
  `#@native.getBattery().then(#{yes.to_n}).catch(#{no.to_n})`
  promise
end

#java?Boolean

Check if Java is enabled.



158
159
160
161
162
# File 'opal/browser/navigator.rb', line 158

def java?
  `#@native.javaEnabled()`
rescue
  false
end

#offline?Boolean

Check if the browser is in offline mode.



121
122
123
# File 'opal/browser/navigator.rb', line 121

def offline?
  `!#@native.onLine`
end

#send_beacon(url, payload = nil) ⇒ Object

Queue to send a small amount of data to a server.



261
262
263
# File 'opal/browser/navigator.rb', line 261

def send_beacon(url, payload=nil)
  `#@native.sendBeacon(#{url}, #{payload.to_n})`
end

#stop_tracking(id) ⇒ Object



228
229
230
# File 'opal/browser/navigator.rb', line 228

def stop_tracking(id)
  `#@native.geolocation.clearWatch(#{id})`
end

#track(max_age: 0, timeout: Float::INFINITY, high_accuracy: false, error: proc{|i|}, &block) ⇒ Integer

Geolocates the user multiple times and calls a block with his location until #stop_tracking is called with a returned id. Calls a proc named error if error happens.



221
222
223
224
225
226
# File 'opal/browser/navigator.rb', line 221

def track(max_age: 0, timeout: Float::INFINITY, high_accuracy: false, error: proc{|i|}, &block)
  opts = {maxAge: max_age, timeout: timeout, enableHighAccuracy: high_accuracy}
  succ = proc { |i| block.call(Position.new(i)) }
  fail = proc { |i| error.call(Native(i)) }
  `#@native.geolocation.watchPosition(#{succ.to_n}, #{fail.to_n}, #{opts.to_n})`
end

#track?Boolean

Check if DNT is disabled.



104
105
106
# File 'opal/browser/navigator.rb', line 104

def track?
  `!#@native.doNotTrack`
end

#vibrate(pattern) ⇒ Object

Triggers a vibration on a device. A pattern can be either a number of miliseconds for a vibration length, or an array of lengths (in miliseconds) which describes a vibration pattern - first element of said array describes how long the device should vibrate, second - how long to stop for and so on.



237
238
239
# File 'opal/browser/navigator.rb', line 237

def vibrate(pattern)
  `#@native.vibrate(#{pattern.to_n})`
end