mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2026-01-02 04:14:41 +03:00
Break some methods in helpers as new submodules: bot-detection, browser-detection, device-detection
`isAIBot()` & `isBot()` => `bot-detection` `isChromeFamily()`, `isElectron()`, `isFromEU()`, & `isStandalonePWA()` => `browser-detection` `getDeviceVendor()` & `isAppleSilicon()` => `device-detection`
This commit is contained in:
8
src/device-detection/device-detection.d.ts
vendored
Normal file
8
src/device-detection/device-detection.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
// Type definitions for device-detection submodule of UAParser.js v2.0.7
|
||||
// Project: https://github.com/faisalman/ua-parser-js
|
||||
// Definitions by: Faisal Salman <https://github.com/faisalman>
|
||||
|
||||
import type { IResult } from "../main/ua-parser";
|
||||
|
||||
export function getDeviceVendor(model: string): string | undefined;
|
||||
export function isAppleSilicon(resultOrUA: IResult | string): boolean;
|
||||
43
src/device-detection/device-detection.js
Normal file
43
src/device-detection/device-detection.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/////////////////////////////////////////////////////
|
||||
/* device-detection submodule of UAParser.js v2.0.7
|
||||
https://github.com/faisalman/ua-parser-js
|
||||
Author: Faisal Salman <f@faisalman.com>
|
||||
AGPLv3 License */
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
/*jshint esversion: 6 */
|
||||
|
||||
const { UAParser } = require('../main/ua-parser');
|
||||
const { CPUArch, OSName } = require('../enums/ua-parser-enums');
|
||||
|
||||
const getDeviceVendor = (model) => new UAParser(`Mozilla/5.0 (Linux; Android 10; ${model}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36`).getDevice().vendor;
|
||||
|
||||
const isAppleSilicon = (val) => {
|
||||
const { os, cpu } = typeof val !== 'string' ? val : {
|
||||
os: new UAParser(val).getOS(),
|
||||
cpu: new UAParser(val).getCPU()
|
||||
};
|
||||
if (os.is(OSName.MACOS)) {
|
||||
if (cpu.is(CPUArch.ARM)) {
|
||||
return true;
|
||||
} else if (typeof window !== 'undefined') {
|
||||
try {
|
||||
const canvas = document.createElement('canvas');
|
||||
const webgl = canvas.getContext('webgl2') ||
|
||||
canvas.getContext('webgl') ||
|
||||
canvas.getContext('experimental-webgl');
|
||||
return webgl
|
||||
.getParameter(webgl.getExtension('WEBGL_debug_renderer_info').UNMASKED_RENDERER_WEBGL)
|
||||
.match(/apple m\d/i);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getDeviceVendor,
|
||||
isAppleSilicon
|
||||
}
|
||||
47
src/device-detection/device-detection.mjs
Normal file
47
src/device-detection/device-detection.mjs
Normal file
@@ -0,0 +1,47 @@
|
||||
// Generated ESM version of ua-parser-js/device-detection
|
||||
// DO NOT EDIT THIS FILE!
|
||||
// Source: /src/device-detection/device-detection.js
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
/* device-detection submodule of UAParser.js v2.0.7
|
||||
https://github.com/faisalman/ua-parser-js
|
||||
Author: Faisal Salman <f@faisalman.com>
|
||||
AGPLv3 License */
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
/*jshint esversion: 6 */
|
||||
|
||||
import { UAParser } from '../main/ua-parser.mjs';
|
||||
import { CPUArch, OSName } from '../enums/ua-parser-enums.mjs';
|
||||
|
||||
const getDeviceVendor = (model) => new UAParser(`Mozilla/5.0 (Linux; Android 10; ${model}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36`).getDevice().vendor;
|
||||
|
||||
const isAppleSilicon = (val) => {
|
||||
const { os, cpu } = typeof val !== 'string' ? val : {
|
||||
os: new UAParser(val).getOS(),
|
||||
cpu: new UAParser(val).getCPU()
|
||||
};
|
||||
if (os.is(OSName.MACOS)) {
|
||||
if (cpu.is(CPUArch.ARM)) {
|
||||
return true;
|
||||
} else if (typeof window !== 'undefined') {
|
||||
try {
|
||||
const canvas = document.createElement('canvas');
|
||||
const webgl = canvas.getContext('webgl2') ||
|
||||
canvas.getContext('webgl') ||
|
||||
canvas.getContext('experimental-webgl');
|
||||
return webgl
|
||||
.getParameter(webgl.getExtension('WEBGL_debug_renderer_info').UNMASKED_RENDERER_WEBGL)
|
||||
.match(/apple m\d/i);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export {
|
||||
getDeviceVendor,
|
||||
isAppleSilicon
|
||||
}
|
||||
Reference in New Issue
Block a user