🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

super-device-detect

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

super-device-detect

A device detector

1.1.2
latest
Version published
Weekly downloads
9
80%
Maintainers
0
Weekly downloads
 
Created

super-device-detect

super-device-detect is a precise user agent parser and device detector written in TypeScript, backed by the largest and most up-to-date open-source user agent database.

This is a javascript port of Matomo device-detector (4.2.3).

Features

  • No dependencies
  • TypeScript support
  • Thoroughly tested
  • New iphone version

Getting Started

Installation

To use super-device-detect in your project, run:

npm install super-device-detect

Usage

ES2015 import:
import DeviceDetector from "super-device-detect";
TypeScript import:
import DeviceDetector = require("super-device-detect");

Example - user agent detection:

import DeviceDetector from "super-device-detect";

const deviceDetector = new DeviceDetector();
const userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36";
const device = deviceDetector.parse(userAgent);

console.log(device);

Output:

{
  "client": {
    "type": "browser",
    "name": "Chrome",
    "version": "69.0",
    "engine": "Blink",
    "engineVersion": ""
  },
  "os": {
    "name": "Mac",
    "version": "10.13",
    "platform": ""
  },
  "device": {
    "type": "desktop",
    "brand": "Apple",
    "model": ""
  },
  "bot": null
}

Example - bot detection:

import BotDetector from "super-device-detect/dist/parsers/bot";

const botDetector = new BotDetector();
const userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)";
const bot = botDetector.parse(userAgent);

if (bot) {
  console.log(bot);
}

Output:

{
  "name": "Googlebot",
  "category": "Search bot",
  "url": "http://www.google.com/bot.html",
  "producer": {
    "name": "Google Inc.",
    "url": "http://www.google.com"
  }
}

API Documentation

new DeviceDetector([options])

  • options <[Object]> Options object which might have the following properties:
    • skipBotDetection <[boolean]> If true, bot detection will completely be skipped (bots will be detected as regular devices). Defaults to false.
    • versionTruncation <[0 | 1 | 2 | 3 | null]> Passing null disables version truncation, so full versions will be returned. Defaults to 1, only minor versions will be returned (e.g. X.Y).

TypeScript

Type definitions are included in this library and exposed via:

import { DeviceDetectorResult, DeviceDetectorOptions } from "super-device-detect";

License

This is a free/libre library under license LGPL v3 or later.

FAQs

Package last updated on 27 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts