Identify Your Visitors’ Devices Using The Various Devices Library

Category: Javascript , Recommended | April 1, 2024
Author:message163
Views Total:13 views
Official Page:Go to website
Last Update:April 1, 2024
License:MIT

Preview:

Identify Your Visitors’ Devices Using The Various Devices Library

Description:

Various Devices is a tiny JavaScript library that enables developers to easily detect the current environment (Electron, browser, Node.js, or web worker), identify the desktop operating system (Windows, macOS, or Linux), and even retrieve detailed information about mobile devices.

This library goes beyond basic OS detection. It also has the ability to gather comprehensive system information.

The getPlatformElectron() function provides a wealth of data about the Electron platform, including version details, node version, V8 engine version, and information about various modules and libraries.

Additionally, the getOs() function returns an extensive list of system details, such as the operating system name, version, architecture, CPU model, CPU speed, number of CPUs, total memory, and free memory.

Various Devices can be helpful for developers who are creating cross-platform applications that need to adapt to different devices and environments.

How to use it:

1. Install and import the Various Devices.

# NPM
$ npm install various-devices
// ESM
import { 
  getEnv, 
  getDesktop,
  isWindows,
  isMacOS,
  getMobileDeviceInfo,
  isAndroid,
  isIOS,
  getPlatformElectron,
  getOs,
} from 'various-devices';
// CJS
const { getEnv } = require('various-devices');
// Browser
<script src="path/to/dist/various-devices.iife.js"></script>

2. This library offers a range of functions to access specific device information. The getEnv() tells you whether your code is running in a browser, Node.js environment, Electron, or web worker.

// returns 'electron', 'browser', 'node', or 'webworker'
const env = getEnv()

3. Identify the user’s desktop OS with getDesktop(). You can also use specific functions like isWindows(), isMacOS(), and isLinux() for targeted checks.

// returns 'macos', 'windows', or 'linux'
const device = getDesktop()
// true or false
isWindows()
isMacOS()
isLinux()

4. The getMobileDeviceInfo() API retrieves valuable information on mobile devices, including whether it’s an Android device and the current screen size. Specific checks for Android and iOS are available with isAndroid() and isIOS().

/*
  Returns:
  {
    platform: "android"; // 'ios'
    screenWidth: number;
    screenHeight: number;
  }
*/
const device = getMobileDeviceInfo()
// true or false
isIOS()
isAndroid()

5. If your application runs on Electron, the getPlatformElectron() API provides detailed information about the platform, including version numbers for Electron itself, Node.js, V8 engine, and other components.

/*
  Returns:
  {
    platform: string;
    version: string;
    node: string;
    v8: string;
    chrome: string;
    zlib: string;
    openssl: string;
    modules: string;
    nghttp2: string;
    napi: string;
    llhttp: string;
  }
*/
const electronInformation = getPlatformElectron()

6. The asynchronous function getOs() returns comprehensive details about the user’s operating system, including name, version, platform, architecture, CPU model and speed, number of CPUs, and memory information.

/*
  Returns:
  {
    name: string
    version: string
    platform: string
    arch: string
    cpuModel: string
    cpuSpeed: number
    cpus: number
    totalMemory: string
    freeMemory: string
  }
*/
getOs().then(res=> {
  console.log(res)
})

You Might Be Interested In:


Leave a Reply