Packages
drab
0.5.0
0.10.5
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.3
0.8.2
0.8.1
0.8.0
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.6.0-pre.1
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.1
0.1.0
Remote controlled frontend framework for Phoenix.
Current section
Files
Jump to
Current section
Files
lib/drab/browser.ex
defmodule Drab.Browser do
import Drab.Core
@moduledoc """
Browser related functions.
Provides information about connected browser, such as local datetime, user agent.
"""
@doc """
Returns local browser time as NaiveDateTime. Timezone information is not included.
Examples:
iex> Drab.Browser.now(socket)
~N[2017-04-01 15:07:57.027000]
"""
def now(socket) do
js = """
var d = new Date()
var retval = {
year: d.getFullYear(),
month: d.getMonth(),
day: d.getDate(),
hour: d.getHours(),
minute: d.getMinutes(),
second: d.getSeconds(),
millisecond: d.getMilliseconds()
}
retval
"""
{:ok, browser_now} = exec_js(socket, js)
{:ok, now} = NaiveDateTime.new(
browser_now["year"],
browser_now["month"],
browser_now["day"],
browser_now["hour"],
browser_now["minute"],
browser_now["second"],
browser_now["millisecond"] * 1000
)
now
end
@doc """
Returns utc offset (the difference between local browser time and UTC time), in seconds.
Examples:
iex> Drab.Browser.utc_offset(socket)
7200 # UTC + 02:00
"""
def utc_offset(socket) do
{:ok, offset} = exec_js(socket, "new Date().getTimezoneOffset()")
-60 * offset
end
# def utc_now(socket) do
# browser_utc = exec_js!(socket, "new Date().toISOString()")
# {:ok, now, _offset} = DateTime.from_iso8601(browser_utc)
# now
# end
@doc """
Returns browser information (userAgent).
Examples:
iex> Drab.Browser.user_agent(socket)
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) ..."
"""
def user_agent(socket) do
{:ok, agent} = exec_js(socket, "navigator.userAgent")
agent
end
@doc """
Returns browser language.
Example:
iex> Drab.Browser.language(socket)
"en-GB"
"""
def language(socket) do
{:ok, lang} = exec_js(socket, "navigator.language")
lang
end
@doc """
Returns a list of browser supported languages.
Example:
iex> Drab.Browser.languages(socket)
["en-US", "en", "pl"]
"""
def languages(socket) do
{:ok, langs} = exec_js(socket, "navigator.languages")
langs
end
@doc """
Redirects to the given url.
WARNING: redirection will disconnect the current websocket, so it should be the last function launched in the
handler.
"""
def redirect_to(socket, url) do
{:ok, _} = exec_js(socket, "window.location = '#{url}'")
end
@doc """
Broadcast version of `redirect_to`.
WARNING: redirection will disconnect the current websocket, so it should be the last function launched in the
handler.
"""
def redirect_to!(socket, url) do
broadcast_js(socket, "window.location = '#{url}'")
end
@doc """
Sends the log to the browser console for debugging.
"""
def console(socket, log) do
do_console(socket, log, &Drab.push/5)
socket
end
@doc """
Broadcasts the log to the browser consoles for debugging/
"""
def console!(socket, log) do
do_console(socket, log, &Drab.broadcast/5)
end
defp do_console(socket, log, push_or_broadcast_function) do
push_or_broadcast_function.(socket, self(), nil, "console", log: log)
end
end