# Detectizr
[![CI](https://github.com/barisaydinoglu/Detectizr/actions/workflows/ci.yml/badge.svg)](https://github.com/barisaydinoglu/Detectizr/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/barisaydinoglu/Detectizr/branch/master/graph/badge.svg)](https://codecov.io/gh/barisaydinoglu/Detectizr)
[![jsDelivr Hits](https://data.jsdelivr.com/v1/package/npm/detectizr/badge?style=rounded)](https://www.jsdelivr.com/package/npm/detectizr)


Detectizr is a Modernizr extension to detect
* device
* device model
* device orientation
* screen size
* operating system
* operating system version
* operating system version full
* browser
* browser version
* browser engine
* browser plugins

Detection of these sets are optional and can be disabled.

Detectable device types are: tv (includes smart tv and game console), mobile, tablet, and desktop.
Device models of tv, mobile and tablet are being detected.

Code quality of Detectizr is validated via [JSHint](https://jshint.com "JSHint") and a test suite with 100% code coverage, run in Node and in headless Chrome.

## Installation

Load Detectizr after Modernizr.

**CDN (jsDelivr)**

<!-- x-release-please-start-version -->
```html
<script src="https://cdn.jsdelivr.net/gh/barisaydinoglu/Detectizr@2.5.1/dist/detectizr.min.js"></script>
```
<!-- x-release-please-end -->

The version above is updated with every release. To always get the newest 2.x release instead, use `@2`; it can take a few days to update because of CDN caching.

**npm**

```sh
npm install detectizr
```

With a bundler (webpack, Vite, Rollup, esbuild), import Modernizr first. Detectizr runs as soon as it is imported:

```js
import "./modernizr"; // your Modernizr build
import Detectizr from "detectizr";

console.log(Detectizr.device.type, Detectizr.os.name);
```

If Modernizr is loaded later, call `Detectizr.detect()` once it is there to add the classes. Importing Detectizr during server rendering is safe; it only detects in the browser.

**Download**

Every [GitHub release](https://github.com/barisaydinoglu/Detectizr/releases) has `detectizr.js`, `detectizr.min.js` and its source map attached.

## Sample Usage

This is a library that uses your trusty little Modernizr to give you the possibility of specifying required features for a certain stylesheet.
Using it is a simple matter, illustrated in the following excerpt:

```html
<html>
  <head>
    <script src="modernizr.js"></script>
    <script src="detectizr.js"></script>
  </head>
  <body>
    <script>
      Detectizr.detect({detectScreen:false});
    </script>
  </body>
</html>
```
Just remember to include Modernizr before Detectizr, and you're good to go!

### User-Agent Client Hints

Chromium browsers (Chrome, Edge, Opera, Samsung Internet) freeze parts of their user agent: Windows 11 reports itself as Windows 10, and Android always reads "Android 10; K". Where [User-Agent Client Hints](https://developer.mozilla.org/docs/Web/API/User-Agent_Client_Hints_API) are available (secure contexts only), Detectizr uses them to correct the result:

* Android phone or tablet, from the `mobile` hint, right away
* Windows 11, the Android and macOS versions, and `32bit`/`64bit`, a few milliseconds later. The results use the same fields as before (`os.version`, `os.major`, `os.minor`, `os.patch`, `os.addressRegisterSize`); nothing new is added

`detect()` stays synchronous: classes are written from the user agent first and updated when the hints arrive. To react to that update (or to an orientation change), register a listener:

```js
Detectizr.onChange(function (detectizr) {
  console.log(detectizr.os.name, detectizr.os.version);
});
```

Hints describe the real browser, so they are not used when `Detectizr.browser.userAgent` was set to another user agent. Turn them off with `Detectizr.detect({detectClientHints: false})`.

Licensed under MIT license.

## When to use Detectizr

Feature detection (Modernizr, `@supports`) and media queries should be your first choice. Detectizr is for the cases they cannot answer: when you need to know *which* device, operating system or browser it is, not what it supports. Real examples:

* **Platform conventions.** Show `⌘` or `Ctrl` in keyboard shortcuts, the right download button for macOS, Windows or Linux, or the App Store badge on iOS and Google Play on Android. Every platform supports the feature; they just do it differently.

  ```js
  var mod = Detectizr.os.name === "mac os" || Detectizr.os.name === "ios" ? "⌘" : "Ctrl";
  ```

* **Add to Home Screen.** Android browsers fire `beforeinstallprompt`, so you can offer an install button there. Safari on iOS never fires it, and the absence of an event cannot be detected. On `.ios`, show the "Share → Add to Home Screen" instructions instead.

* **In-app browsers.** Links opened inside Instagram, Facebook or TikTok load in an embedded browser. Google sign-in refuses to run there (`disallowed_useragent`), and downloads and payments often fail. Detect it and ask the user to open the page in their browser:

  ```js
  if (Detectizr.browser.app) { // "instagram", "facebook" or "tiktok"
    showOpenInBrowserHint();
  }
  ```

  The `inapp` and `webview` classes let you do the same in CSS.

* **Engine bugs no feature test can see.** Safari on iOS zooms into any form field whose font size is below 16px when it gets focus; `.ios input, .ios select, .ios textarea { font-size: 16px; }` stops it. An older example is Safari 6.0 crashing when `calc()` and a `transition` are used on the same element ([WebKit bug 100568](https://bugs.webkit.org/show_bug.cgi?id=100568)), which only browser sniffing could work around.

* **TVs and game consoles.** They are big screens without touch and usually without a mouse, so media queries can mistake them for a desktop. On `.tv`, make focus outlines larger and keep everything reachable with the arrow keys of a remote.

* **Heavy effects on phones.** Phones support CSS animations and WebGL, but full-page transforms or a 3D hero can still stutter on them. Skip initialising heavy scripts with `if (!Modernizr.desktop)`, and combine it with `prefers-reduced-motion`.

* **Bug reports and support.** Put `Detectizr.device`, `Detectizr.os` and `Detectizr.browser` into a support form or error log, so you know where a problem happened without asking.

Use something else for:

* **whether a feature works:** Modernizr or `@supports`
* **layout:** media queries on width and height
* **hover styles:** `@media (hover: hover)`. It keeps hover on touch-screen laptops and turns it off on tablets, which device classes cannot tell apart.
* **security or access control:** never, a user agent can be changed by anyone

Have another real case? [Open an issue](https://github.com/barisaydinoglu/Detectizr/issues/new) and it can be added here.

## Note for Modernizr 3.x

You will need to have a build of Modernizr with the addTest (Modernizr.addTest()) option.
If done from the website, you can find it 

## Development

```sh
npm install
npm test           # lint + browser tests (headless Chrome) + Node tests with coverage
npm run test:node  # Node tests only; coverage report in ./coverage
```

`dist/` is generated: do not edit or commit it in pull requests.

### Releasing

Releases are automated with [release-please](https://github.com/googleapis/release-please). Use [Conventional Commits](https://www.conventionalcommits.org/) (`feat: ...`, `fix: ...`); release-please keeps a release pull request up to date with the next version, the changelog and a fresh `dist/`. Merging that pull request tags the release, publishes the GitHub release and attaches the dist files.

## Other interesting projects

* [Modernizr](https://github.com/Modernizr/Modernizr "Modernizr") is required for Detectizer. Remember to put Modernizr before Detectizr.
* [Categorizr](https://github.com/bjankord/Categorizr "Categorizr") is a server side device and OS detection script wirtten in PHP. Detectizr inspired by its device detection.
* [CSS Browser Selector] (http://rafael.adm.br/css_browser_selector/ "CSS Browser Selector") is a client side browser detection script. Detectizr inspired by its browser detection.
