drab v0.8.3 Drab.Browser

Browser related functions.

Provides information about connected browser, such as local datetime, user agent.

Link to this section Summary

Functions

Broadcasts the log to the browser consoles for debugging/

Like set_url/2, but broadcasting the change to all connected browsers

Sends the log to the browser console for debugging

Returns string with the browser unique id

Returns string with the browser unique id

Returns browser language

Bang version of language/1

Returns a list of browser supported languages

Bang version of language/1

Returns local browser time as NaiveDateTime. Timezone information is not included

Bang version of now/1. Raises exception on error

Redirects to the given url

Replaces the URL in the browser navigation bar for the given URL

Exception throwing version of set_url/2

Returns browser information (userAgent)

Returns utc offset (the difference between local browser time and UTC time), in seconds

Returns utc offset (the difference between local browser time and UTC time), in seconds. Raises exception on error

Link to this section Functions

Link to this function broadcast_console(socket, log)
broadcast_console(Phoenix.Socket.t(), String.t()) :: Drab.Core.bcast_result()

Broadcasts the log to the browser consoles for debugging/

Link to this function broadcast_redirect_to(socket, url)
broadcast_redirect_to(Phoenix.Socket.t(), String.t()) :: any()

Broadcast version of redirect_to/2.

WARNING: redirection will disconnect the current websocket, so it should be the last function launched in the handler.

Link to this function broadcast_set_url(socket, url)
broadcast_set_url(Phoenix.Socket.t(), String.t()) :: Drab.Core.bcast_result()

Like set_url/2, but broadcasting the change to all connected browsers.

Link to this function console(socket, log)

Sends the log to the browser console for debugging.

Link to this function id(socket)
id(Phoenix.Socket.t()) :: {:ok | :error, String.t()}

Returns string with the browser unique id.

The id is generated on the client side and stored in local store.

iex> Drab.Browser.id(socket)
{:ok, "2bd34ffc-b365-46a9-9479-474b628364ed"}

Returns string with the browser unique id.

Bang version of id/1.

Returns browser language.

Example:

iex> Drab.Browser.language(socket)
{:ok, "en-GB"}
Link to this function language!(socket)
language!(Phoenix.Socket.t()) :: String.t()

Bang version of language/1.

Example:

iex> Drab.Browser.language!(socket)
"en-GB"
Link to this function languages(socket)

Returns a list of browser supported languages.

Example:

iex> Drab.Browser.languages(socket)
{:ok, ["en-US", "en", "pl"]}
Link to this function languages!(socket)
languages!(Phoenix.Socket.t()) :: list()

Bang version of language/1.

Example:

iex> Drab.Browser.languages!(socket)
["en-US", "en", "pl"]

Returns local browser time as NaiveDateTime. Timezone information is not included.

Examples:

iex> Drab.Browser.now(socket)
{:ok, ~N[2017-04-01 15:07:57.027000]}

Bang version of now/1. Raises exception on error.

Examples:

iex> Drab.Browser.now!(socket)
~N[2017-04-01 15:07:57.027000]
Link to this function redirect_to(socket, url)
redirect_to(Phoenix.Socket.t(), String.t()) :: Drab.Core.result()

Redirects to the given url.

WARNING: redirection will disconnect the current websocket, so it should be the last function launched in the handler.

Link to this function set_url(socket, url)

Replaces the URL in the browser navigation bar for the given URL.

The new URL can be absolute or relative to the current path. It must have the same origin as the current one.

iex> Drab.Browser.set_url socket, "/servers/1"
{:ok, nil}

iex> Drab.Browser.set_url socket, "http://google.com/"
{:error,
 "Failed to execute 'pushState' on 'History': A history state object ...'
  cannot be created in a document with origin 'http://localhost:4000' ..."}
Link to this function set_url!(socket, url)
set_url!(Phoenix.Socket.t(), String.t()) :: any()

Exception throwing version of set_url/2.

iex> Drab.Browser.set_url! socket, "/servers/1"
nil

iex> Drab.Browser.set_url! socket, "http://google.com/"
** (Drab.JSExecutionError) Failed to execute 'pushState' on 'History' ...
Link to this function user_agent(socket)
user_agent(Phoenix.Socket.t()) :: Drab.Core.result()

Returns browser information (userAgent).

Examples:

iex> Drab.Browser.user_agent(socket)
{:ok, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 ..."}
Link to this function user_agent!(socket)
user_agent!(Phoenix.Socket.t()) :: String.t()

Bang version of user_agent/1.

Examples:

iex> Drab.Browser.user_agent!(socket)
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) ..."
Link to this function utc_offset(socket)
utc_offset(Phoenix.Socket.t()) :: Drab.Core.result()

Returns utc offset (the difference between local browser time and UTC time), in seconds.

Examples:

iex> Drab.Browser.utc_offset(socket)
{:ok, 7200} # UTC + 02:00
Link to this function utc_offset!(socket)
utc_offset!(Phoenix.Socket.t()) :: integer()

Returns utc offset (the difference between local browser time and UTC time), in seconds. Raises exception on error.

Examples:

iex> Drab.Browser.utc_offset!(socket)
7200 # UTC + 02:00