drab v0.10.5 Drab.Browser View Source

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

Exception raising version of console/2

Sends the log to the browser console for debugging

Exception raising version of cookie/3

Gets a named cookie from the browser

Exception raising version of cookies/2

Gets cookies from the browser

Exception raising version of delete_cookie/2

Delete the named cookie

Returns string with the browser unique id

Returns string with the browser unique id

Bang version of language/1

Returns browser language

Bang version of language/1

Returns a list of browser supported languages

Bang version of now/1. Raises exception on error

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

Exception raising version of redirect_to/2

Redirects to the given url

Exception throwing version of set_url/2

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

Returns browser information (userAgent)

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

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

Link to this section Functions

Link to this function broadcast_console(socket, log) View Source
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) View Source
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) View Source
broadcast_set_url(Phoenix.Socket.t(), String.t()) :: Drab.Core.bcast_result()

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

Exception raising version of console/2

Sends the log to the browser console for debugging.

Link to this function cookie!(socket, name, options \\ []) View Source
cookie!(Phoenix.Socket.t(), String.t(), Keyword.t()) :: any() | no_return()

Exception raising version of cookie/3

Link to this function cookie(socket, name, options \\ []) View Source

Gets a named cookie from the browser.

Values are decoded using, by default, Drab.Coder.URL. You may change this by giving :decoder option:

iex> Drab.Browser.set_cookie(socket, "mycookie", "42")
{:ok, "mycookie=42"}
iex> Drab.Browser.cookie(socket, "mycookie")
{:ok, "42"}
Link to this function cookies!(socket, options \\ []) View Source
cookies!(Phoenix.Socket.t(), Keyword.t()) :: map() | no_return()

Exception raising version of cookies/2.

Link to this function cookies(socket, options \\ []) View Source

Gets cookies from the browser.

Returns map of %{name => value}. Notice that in case of the multiple cookies with the same name, returns only the one which browser returns first. It should be a cookie with the longest path.

Cookie names are decoded using Drab.Coder.URL.

Values are decoded using, by default, Drab.Coder.URL. You may change this by giving :decoder option:

iex> Drab.Browser.cookies(socket, decoder: Drab.Coder.Cipher)
{:ok, %{"exp" => 42, "mycookie" => "value"}}
Link to this function delete_cookie!(socket, name) View Source
delete_cookie!(Phoenix.Socket.t(), String.t()) :: String.t() | no_return()

Exception raising version of delete_cookie/2

Link to this function delete_cookie(socket, name) View Source
delete_cookie(Phoenix.Socket.t(), String.t()) :: Drab.Core.result()

Delete the named cookie.

iex> Drab.Browser.set_cookie(socket, "mycookie", "42")
{:ok, "mycookie=42"}
iex> Drab.Browser.cookie(socket, "mycookie")
{:ok, "42"}
iex> Drab.Browser.delete_cookie(socket, "mycookie")
{:ok, _}
iex> Drab.Browser.cookie(socket, "mycookie")
{:error, "Cookie "mycookie" not found."}

Returns string with the browser unique id.

Bang version of id/1.

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"}

Bang version of language/1.

Example:

iex> Drab.Browser.language!(socket)
"en-GB"

Returns browser language.

Example:

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

Bang version of language/1.

Example:

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

Returns a list of browser supported languages.

Example:

iex> Drab.Browser.languages(socket)
{:ok, ["en-US", "en", "pl"]}

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

Examples:

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

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]}
Link to this function redirect_to!(socket, url) View Source
redirect_to!(Phoenix.Socket.t(), String.t()) :: any()

Exception raising version of redirect_to/2

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_cookie!(socket, name, value, options \\ []) View Source
set_cookie!(Phoenix.Socket.t(), String.t(), term(), Keyword.t()) ::
  String.t() | no_return()

Exception raising version of set_cookie/4.

Link to this function set_cookie(socket, name, value, options \\ []) View Source

Sets the cookie.

Cookie names are encoded using Drab.Coder.URL.

Values are encoded using, by default, Drab.Coder.URL. You may change this by giving :encoder option. Notice that some encoders (Drab.Coder.URL and Drab.Coder.Base64) accept only string as an argument. To set the cookie with any term, use Drab.Coder.String or Drab.Coder.Cipher.

Options:

  • :encoder (default Drab.Coder.URL) - encode the cookie value with the given coder
  • :path
  • :domain - must contain two dots, like “.example.org”
  • :secure - if true, cookie will be https only
  • :max_age - number of seconds for when a cookie will be deleted

Returns {:ok, cookie} where cookie is the string returned by the browsers, or {:error, reason} if something goes wrong.

To delete a cookie, set the :max_age to -1.

Examples:

iex> set_cookie(socket, "mycookie", "value")
{:ok, "mycookie=value"}

iex> set_cookie(socket, "mycookie", "value", max_age: 10)
{:ok, "mycookie=value; expires=Thu, 19 Jul 2018 19:47:09 GMT"}

iex(69)> set_cookie(socket, "mycookie", %{any: "term"}, max_age: 10, encoder: Drab.Coder.Cipher)
{:ok, "mycookie=QTEyO...Yinr17vXhHQ; expires=Thu, 19 Jul 2018 19:48:53 GMT"}
Link to this function set_url!(socket, url) View Source
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' ...

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 user_agent!(socket) View Source
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) ..."

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 utc_offset!(socket) View Source
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

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