Git Product home page Git Product logo

elixir-browser's Introduction

elixir-browser

Build Status

Browser detection for Elixir. This is a port from the Ruby browser library.

All the detection features have been ported, but not the meta and the language ones.

Installation

Add browser to your list of dependencies in mix.exs:

def deps do
  [{:browser, "~> 0.4.4"}]
end

Usage

ua = "some string"
Browser.name(ua)            # readable browser name
Browser.version(ua)         # major version number
Browser.full_version(ua)
Browser.full_browser_name(ua) # Chrome 5.0.375.99
Browser.full_display(ua)    # example: Chrome 5.0.375.99 on MacOS 10.6.4 Snow Leopard
Browser.safari?(ua)
Browser.opera?(ua)
Browser.chrome?(ua)
Browser.chrome_os?(ua)
Browser.mobile?(ua)
Browser.tablet?(ua)
Browser.console?(ua)
Browser.firefox?(ua)
Browser.ie?(ua)
Browser.ie?(ua, 6)          # detect specific IE version
Browser.edge?(ua)           # Newest MS browser
Browser.modern?(ua)         # Webkit, Firefox 17+, IE 9+ and Opera 12+
Browser.platform(ua)        # return :ios, :android, :mac, :windows, :linux or :other
Browser.full_platform_name(ua) # example: MacOS 10.6.4 Snow Leopard
Browser.device_type(ua)     # return :mobile, :tablet, :desktop, :console, :unknown
Browser.ios?(ua)            # detect iOS
Browser.ios?(ua, 9)         # detect specific iOS version
Browser.mac?(ua)
Browser.mac_version(ua)     # display version of Mac OSX. i.e. High Sierra
Browser.windows?(ua)
Browser.windows_x64?(ua)
Browser.windows_version_name # display version of Windows.  i.e. Windows 10
Browser.linux?(ua)
Browser.blackberry?(ua)
Browser.blackberry?(ua, 10) # detect specific BlackBerry version
Browser.bot?(ua)
Browser.search_engine?(ua)
Browser.phantom_js?(ua)
Browser.quicktime?(ua)
Browser.core_media?(ua)
Browser.silk?(ua)
Browser.android?(ua)
Browser.android?(ua, 4.2)   # detect Android Jelly Bean 4.2
Browser.known?(ua)          # has the browser been successfully detected?

See the original Ruby library for more information.

Elixir addition

You can also pass Plug.Conn instead of a string, the user-agent header will be extracted and used.

Browser.bot?(conn)

Configuration

You can specify custom bots.txt file in your project's config.

config :browser,
  bots_file: Path.join(File.cwd!, "bots.txt")  # bots.txt in project's root

Please note that option set as mobule attribute during compile time, so make sure you recompiled elixir-browser after changing this option or bots file itself.

elixir-browser's People

Contributors

chrism2671 avatar crbelaus avatar danhper avatar derkobe avatar ewitchin avatar gamache avatar giraphme avatar jcsrb avatar lukaszsamson avatar praveenperera avatar rblack avatar safwank avatar the-guitarman avatar xinz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

elixir-browser's Issues

protocol Enumerable not implemented for nil

For some reason some people in my application are getting a weird UA string which then causes and exception to be raised for "protocol Enumerable not implemented for nil" when calling Browser.full_version

Here is the UA string:
Mozilla/5.0 (iPad; CPU OS 9_3_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) GSA/13.1.72140 Mobile/13F69 Safari/600.1.4

The library fails to detect modern Edge on Mac

Modern Edge on Mac is detected as Chrome. Example:

iex(1)> Browser.edge?("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.50")
false

The user agent in the example above is a real user agent string for a freshly installed Edge on Mac.

Browser.search_engine?(%Plug.Conn{}) fails

Hi, I checked out your package to detect bots and search engines.

At first I tested for bots:

iex> Browser.bot?(%Plug.Conn{})
false

Ist returned false like I expected, because it's an empty struct.

Then I tried to detect search engines and got an error:

iex> Browser.search_engine?(%Plug.Conn{})
** (FunctionClauseError) no function clause matching in String.contains?/2    
    
    The following arguments were given to String.contains?/2:
     
        # 1
        %Plug.Conn{
          adapter: {Plug.MissingAdapter, :...},
          assigns: %{},
          before_send: [],
          body_params: %Plug.Conn.Unfetched{aspect: :body_params},
          cookies: %Plug.Conn.Unfetched{aspect: :cookies},
          halted: false,
          host: "www.example.com",
          method: "GET",
          owner: nil,
          params: %Plug.Conn.Unfetched{aspect: :params},
          path_info: [],
          path_params: %{},
          peer: nil,
          port: 0,
          private: %{},
          query_params: %Plug.Conn.Unfetched{aspect: :query_params},
          query_string: "",
          remote_ip: nil,
          req_cookies: %Plug.Conn.Unfetched{aspect: :cookies},
          req_headers: [],
          request_path: "",
          resp_body: nil,
          resp_cookies: %{},
          resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}],
          scheme: :http,
          script_name: [],
          secret_key_base: nil,
          state: :unset,
          status: nil
        }
    
        # 2
        "Baidu"
    
    Attempted function clauses (showing 3 out of 3):
    
        def contains?(string, []) when is_binary(string)
        def contains?(string, contents) when is_binary(string) and is_list(contents)
        def contains?(string, contents) when is_binary(string)
    
    (elixir) lib/string.ex:2009: String.contains?/2
    (elixir) lib/enum.ex:2838: Enum.any_list/2

The same happened in my real app at dev mode.
May be this could be interesting for you.

Possibly invalid detections for some useragents

Hi!
I've recently tested elixir-browser and found some possibly invalid detections. Useragents in this list are detected neither as mobile nor tablet. Most of them seem completely valid.
I thought you may find this info usefull, otherwise I'm sorry and you may close the issue.

Android detection and Opera

Hi! Have some question regarding android detection.

  def android?(input, version \\ nil) do
    ua = Ua.to_ua(input)
    String.match?(ua, ~r/Android/) and not opera?(ua) and detect_version?(android_version(ua), version)
  end

Can't find out reason why and not opera?(ua) is there. I removed this part and tests didn't fail. Also looked up android detection in original ruby library and couldn't find opera exclusion there.

Mozilla/5.0 (Linux; Android 8.0; ZE620KL Build/OPR1.170623.032) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.158 Mobile Safari/537.36

This useragent is not detected as android because build number contains OPR. So is there any reason for opera check and can it be safely removed?

Browser.modern? fails on many user agents

I'm noticing that Browser.modern? (in version 0.2.0) is failing on many user agent strings. For example:

`ua = "Mozilla/5.0 (MSIE 10.0; Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"

Browser.modern?(ua)
** (ArgumentError) argument error
:erlang.not(nil)
(browser) lib/browser.ex:126: Browser.modern_ie_version?/1
(elixir) lib/enum.ex:2511: Enum.do_any?/2
`

It seems that this is stemming from browser.ex on lines 125-127:

def modern_ie_version?(ua) do Browser.ie?(ua) and to_int(Browser.version(ua)) >= 9 and not Browser.compatibility_view?(ua) end

Browser.compatibility_view?(ua) can return nil and this statement does not account for it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.