Skip to content

Commit

Permalink
Improve desktop device detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mneudert committed Sep 25, 2024
1 parent b5b6c91 commit cad4ec3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/ua_inspector/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmodule UAInspector.Parser do
@is_android_vr Util.Regex.build_base_regex("Android( [\.0-9]+)?; Mobile VR;| VR")
@is_chrome Util.Regex.build_base_regex("Chrome/[\.0-9]*")
@is_chrome_smartphone Util.Regex.build_base_regex("(?:Mobile|eliboM)")
@is_desktop Util.Regex.build_regex("Desktop (x(?:32|64)|WOW64)")
@is_generic_tv Util.Regex.build_base_regex("\\(TV;")
@is_misc_tv Util.Regex.build_base_regex("SmartTV|Tizen.+ TV .+$")
@is_opera_tv_store Util.Regex.build_base_regex("Opera TV Store| OMI/")
Expand Down Expand Up @@ -310,7 +311,7 @@ defmodule UAInspector.Parser do
defp maybe_fix_desktop(%{device: %{type: "desktop"}} = result), do: result

defp maybe_fix_desktop(%{device: device, user_agent: ua} = result) do
if Util.Fragment.desktop?(ua) do
if Regex.match?(@is_desktop, ua) do
%{result | device: %{device | type: "desktop"}}
else
result
Expand Down
9 changes: 8 additions & 1 deletion lib/ua_inspector/parser/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ defmodule UAInspector.Parser.Device do

@behaviour UAInspector.Parser.Behaviour

@desktop_pos Util.Regex.build_regex("(?:Windows (?:NT|IoT)|X11; Linux x86_64)")
@desktop_neg Util.Regex.build_regex(
"CE-HTML| Mozilla/|Andr[o0]id|Tablet|Mobile|iPhone|Windows Phone|ricoh|OculusBrowser|PicoBrowser|Lenovo|compatible; MSIE|Trident/|Tesla/|XBOX|FBMD/|ARM; ?([^)]+)"
)

@frozen_android Regex.compile!("Android 10[.\d]*; K(?: Build/|[;)])", [:caseless])

@hbbtv Util.Regex.build_regex("HbbTV/([1-9]{1}(?:\.[0-9]{1}){1,2})")
Expand Down Expand Up @@ -51,8 +56,10 @@ defmodule UAInspector.Parser.Device do
@spec shelltv?(String.t()) :: boolean
def shelltv?(ua), do: Regex.match?(@shelltv, ua)

defp desktop?(ua), do: Regex.match?(@desktop_pos, ua) && !Regex.match?(@desktop_neg, ua)

defp parse_device(%{model: :unknown} = hints_result, client_hints, ua) do
if Util.Fragment.desktop?(ua) or Regex.match?(@frozen_android, ua) do
if desktop?(ua) or Regex.match?(@frozen_android, ua) do
hints_result
else
parse_device_details(hints_result, client_hints, ua)
Expand Down
13 changes: 0 additions & 13 deletions lib/ua_inspector/util/fragment.ex

This file was deleted.

0 comments on commit cad4ec3

Please sign in to comment.