Class: Browser::Navigator
- 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
-
#code ⇒ String
readonly
The browser code name.
-
#language ⇒ String
readonly
The browser language.
-
#mime_types ⇒ Native::Array<MimeType>
readonly
The supported MIME types.
-
#name ⇒ String
readonly
The browser name.
-
#operating_system ⇒ String
(also: #os)
readonly
The operating system the browser is running on.
-
#platform ⇒ String
readonly
The platform the browser is running on.
-
#plugins ⇒ Plugins
readonly
The enabled plugins.
-
#product ⇒ Product
readonly
The product name and version.
-
#user_agent ⇒ String
readonly
The browser's user agent.
-
#vendor ⇒ Vendor
readonly
The vendor name and version.
-
#version ⇒ Version
readonly
The browser version.
Instance Method Summary collapse
-
#geolocate(max_age: 0, timeout: Float::INFINITY, high_accuracy: false) ⇒ Promise
Geolocates the user once.
-
#get_battery ⇒ Promise
Check a battery status of user device.
-
#java? ⇒ Boolean
Check if Java is enabled.
-
#offline? ⇒ Boolean
Check if the browser is in offline mode.
-
#send_beacon(url, payload = nil) ⇒ Object
Queue to send a small amount of data to a server.
- #stop_tracking(id) ⇒ Object
-
#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.
-
#track? ⇒ Boolean
Check if DNT is disabled.
-
#vibrate(pattern) ⇒ Object
Triggers a vibration on a device.
Methods included from NativeCachedWrapper
#restricted?, #set_native_reference
Instance Attribute Details
#code ⇒ String (readonly)
Returns the browser code name.
89 |
# File 'opal/browser/navigator.rb', line 89 alias_native :code, :appCodeName |
#language ⇒ String (readonly)
Returns the browser language.
111 |
# File 'opal/browser/navigator.rb', line 111 alias_native :language |
#mime_types ⇒ Native::Array<MimeType> (readonly)
Returns the supported MIME types.
115 116 117 118 119 |
# File 'opal/browser/navigator.rb', line 115 def mime_types Native::Array.new `#@native.mimeTypes`, get: :item, named: :namedItem do |m| MimeType.new(m) end end |
#name ⇒ String (readonly)
Returns the browser name.
93 |
# File 'opal/browser/navigator.rb', line 93 alias_native :name, :appName |
#operating_system ⇒ String (readonly) Also known as: os
Returns the operating system the browser is running on.
128 |
# File 'opal/browser/navigator.rb', line 128 alias_native :operating_system, :oscpu |
#platform ⇒ String (readonly)
Returns the platform the browser is running on.
134 |
# File 'opal/browser/navigator.rb', line 134 alias_native :platform |
#plugins ⇒ Plugins (readonly)
Returns the enabled plugins.
138 139 140 |
# File 'opal/browser/navigator.rb', line 138 def plugins Plugins.new(`#@native.plugins`) end |
#product ⇒ Product (readonly)
Returns the product name and version.
144 145 146 |
# File 'opal/browser/navigator.rb', line 144 def product Product.new(`#@native.product`, `#@native.productSub`) end |
#user_agent ⇒ String (readonly)
Returns the browser's user agent.
150 |
# File 'opal/browser/navigator.rb', line 150 alias_native :user_agent, :userAgent |
Instance Method Details
#geolocate(max_age: 0, timeout: Float::INFINITY, high_accuracy: false) ⇒ Promise
Geolocates the user once
208 209 210 211 212 213 214 215 |
# File 'opal/browser/navigator.rb', line 208 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_battery ⇒ Promise
Check a battery status of user device. This API is deprecated in the browser context and usable mainly in privileged contexts.
248 249 250 251 252 253 254 |
# File 'opal/browser/navigator.rb', line 248 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.
159 160 161 162 163 |
# File 'opal/browser/navigator.rb', line 159 def java? `#@native.javaEnabled()` rescue false end |
#offline? ⇒ Boolean
Check if the browser is in offline mode.
122 123 124 |
# File 'opal/browser/navigator.rb', line 122 def offline? `!#@native.onLine` end |
#send_beacon(url, payload = nil) ⇒ Object
Queue to send a small amount of data to a server.
262 263 264 |
# File 'opal/browser/navigator.rb', line 262 def send_beacon(url, payload=nil) `#@native.sendBeacon(#{url}, #{payload.to_n})` end |
#stop_tracking(id) ⇒ Object
229 230 231 |
# File 'opal/browser/navigator.rb', line 229 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.
222 223 224 225 226 227 |
# File 'opal/browser/navigator.rb', line 222 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.
105 106 107 |
# File 'opal/browser/navigator.rb', line 105 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.
238 239 240 |
# File 'opal/browser/navigator.rb', line 238 def vibrate(pattern) `#@native.vibrate(#{pattern.to_n})` end |