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/
Broadcast version of redirect_to/2
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 raising version of set_cookie/4
Sets the cookie
Exception throwing version of set_url/2
Replaces the URL in the browser navigation bar for the given URL
Bang version of user_agent/1
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
broadcast_console(Phoenix.Socket.t(), String.t()) :: Drab.Core.bcast_result()
Broadcasts the log to the browser consoles for debugging/
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.
broadcast_set_url(Phoenix.Socket.t(), String.t()) :: Drab.Core.bcast_result()
Like set_url/2
, but broadcasting the change to all connected browsers.
console!(Phoenix.Socket.t(), String.t()) :: Phoenix.Socket.t()
Exception raising version of console/2
console(Phoenix.Socket.t(), String.t()) :: Drab.Core.result()
Sends the log to the browser console for debugging.
cookie!(Phoenix.Socket.t(), String.t(), Keyword.t()) :: any() | no_return()
Exception raising version of cookie/3
cookie(Phoenix.Socket.t(), String.t(), Keyword.t()) :: Drab.Core.result()
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"}
cookies!(Phoenix.Socket.t(), Keyword.t()) :: map() | no_return()
Exception raising version of cookies/2
.
cookies(Phoenix.Socket.t(), Keyword.t()) :: Drab.Core.result()
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"}}
delete_cookie!(Phoenix.Socket.t(), String.t()) :: String.t() | no_return()
Exception raising version of delete_cookie/2
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"
language(Phoenix.Socket.t()) :: Drab.Core.result()
Returns browser language.
Example:
iex> Drab.Browser.language(socket)
{:ok, "en-GB"}
Bang version of language/1
.
Example:
iex> Drab.Browser.languages!(socket)
["en-US", "en", "pl"]
languages(Phoenix.Socket.t()) :: Drab.Core.result()
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]
now(Phoenix.Socket.t()) :: {Drab.Core.status(), NaiveDateTime.t()}
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]}
redirect_to!(Phoenix.Socket.t(), String.t()) :: any()
Exception raising version of redirect_to/2
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.
Exception raising version of set_cookie/4
.
set_cookie(Phoenix.Socket.t(), String.t(), term(), Keyword.t()) :: Drab.Core.result()
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
(defaultDrab.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"}
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' ...
set_url(Phoenix.Socket.t(), String.t()) :: Drab.Core.result()
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' ..."}
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) ..."
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 ..."}
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
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