mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-09-27 07:58:45 +03:00
Add new package: gpu-detect
to obtain GPU info from user-agent
This commit is contained in:
parent
807dcdbded
commit
05a98aceda
@ -221,6 +221,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"workspaces": [
|
"workspaces": [
|
||||||
|
"src/gpu-detect",
|
||||||
"src/ua-client-hints",
|
"src/ua-client-hints",
|
||||||
"src/user-agent-helpers"
|
"src/user-agent-helpers"
|
||||||
]
|
]
|
||||||
|
@ -47,6 +47,12 @@ const modules = [
|
|||||||
title : 'ua-parser-js/extensions',
|
title : 'ua-parser-js/extensions',
|
||||||
replacements : []
|
replacements : []
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
src : 'src/gpu-detect/gpu-detect.js',
|
||||||
|
dest : 'src/gpu-detect/gpu-detect.mjs',
|
||||||
|
title : '@ua-parser-js/gpu-detect',
|
||||||
|
replacements : []
|
||||||
|
},
|
||||||
{
|
{
|
||||||
src : 'src/user-agent-helpers/user-agent-helpers.js',
|
src : 'src/user-agent-helpers/user-agent-helpers.js',
|
||||||
dest : 'src/user-agent-helpers/user-agent-helpers.mjs',
|
dest : 'src/user-agent-helpers/user-agent-helpers.mjs',
|
||||||
|
3
src/gpu-detect/gpu-detect.d.ts
vendored
Normal file
3
src/gpu-detect/gpu-detect.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export class GPUDetect {
|
||||||
|
static getGPU: { vendor: string, model: string }
|
||||||
|
}
|
116
src/gpu-detect/gpu-detect.js
Normal file
116
src/gpu-detect/gpu-detect.js
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
//////////////////////////////////////////////
|
||||||
|
/* Extracts GPU information from user-agent
|
||||||
|
https://github.com/faisalman/ua-parser-js
|
||||||
|
Author: Faisal Salman <f@faisalman.com>
|
||||||
|
MIT License */
|
||||||
|
/////////////////////////////////////////////
|
||||||
|
|
||||||
|
/*jshint esversion: 11 */
|
||||||
|
|
||||||
|
const rendererMap = [
|
||||||
|
[[
|
||||||
|
/(intel).*\b(hd\sgraphics\s\d{4}|iris(?:\spro)|gma\s\w+)/i, // Intel
|
||||||
|
/(nvidia)\s(geforce\s(?:gtx?\s)\d\w+|quadro)/i, // NVIDIA
|
||||||
|
/\b(sis)\s(\w+)/i // SiS
|
||||||
|
], ['vendor', 'model']],
|
||||||
|
|
||||||
|
[[
|
||||||
|
/\b(radeon[\shdr\d]+\w{4,5})/i // ATI/AMD
|
||||||
|
], ['model', ['vendor', 'AMD']]],
|
||||||
|
|
||||||
|
[[
|
||||||
|
/(adreno\s(?:\(tm\)\s)\w+)/i // Qualcomm
|
||||||
|
], [['model', /\(tm\)\s/i, ''], ['vendor', 'Qualcomm']]]
|
||||||
|
];
|
||||||
|
|
||||||
|
const vendorMap = [
|
||||||
|
[[
|
||||||
|
/\b(amd|apple|arm|ati|img|intel|nvidia|qualcomm|samsung|sis)\b/i
|
||||||
|
], ['vendor']]
|
||||||
|
];
|
||||||
|
|
||||||
|
class RegexMap {
|
||||||
|
|
||||||
|
static parse(str, mapper) {
|
||||||
|
let res = {};
|
||||||
|
if (typeof str === 'string') {
|
||||||
|
for (const [regs, props] of mapper) {
|
||||||
|
if (!Array.isArray(regs)) {
|
||||||
|
throw new Error('RegexMap: Expect Array of RegExp');
|
||||||
|
}
|
||||||
|
if (!Array.isArray(props)) {
|
||||||
|
throw new Error('RegexMap: Expect Array for Properties Mapping');
|
||||||
|
}
|
||||||
|
for (const reg of regs) {
|
||||||
|
if (!reg instanceof RegExp) {
|
||||||
|
throw new Error('RegexMap: Expect RegExp Instance');
|
||||||
|
}
|
||||||
|
const matches = reg.exec(str);
|
||||||
|
if (matches) {
|
||||||
|
props.forEach((prop, idx) => {
|
||||||
|
const val = matches[idx+1];
|
||||||
|
if (Array.isArray(prop)) {
|
||||||
|
const key = prop[0];
|
||||||
|
if (typeof key !== 'string') {
|
||||||
|
throw new Error('RegexMap: Expect String Input');
|
||||||
|
}
|
||||||
|
if (prop.length == 2) {
|
||||||
|
if (typeof prop[1] === 'string') {
|
||||||
|
res[key] = prop[1];
|
||||||
|
} else if (typeof prop[1] === 'function') {
|
||||||
|
res[key] = prop[1].call(res, val);
|
||||||
|
}
|
||||||
|
} else if (prop.length == 3) {
|
||||||
|
if (prop[1] instanceof RegExp) {
|
||||||
|
res[key] = val.replace(prop[1], prop[2]);
|
||||||
|
} else if (typeof prop[1] === 'function') {
|
||||||
|
res[key] = prop[1].call(res, val, prop[2]);
|
||||||
|
}
|
||||||
|
} else if (prop.length == 4) {
|
||||||
|
res[key] = prop[3].call(res, val.replace(prop[1], prop[2]));
|
||||||
|
} else {
|
||||||
|
res[key] = val;
|
||||||
|
}
|
||||||
|
} else if (typeof prop === 'string') {
|
||||||
|
res[prop] = val;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (res) return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
class GPUDetect {
|
||||||
|
|
||||||
|
static getGPU (strRenderer, strVendor) {
|
||||||
|
|
||||||
|
let gpuInfo = { vendor : undefined, model : undefined };
|
||||||
|
|
||||||
|
if (typeof strRenderer !== 'string') {
|
||||||
|
if (globalThis.document) {
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
const gl = canvas.getContext('webgl2') ||
|
||||||
|
canvas.getContext('webgl') ||
|
||||||
|
canvas.getContext('experimental-webgl');
|
||||||
|
if (gl) {
|
||||||
|
const debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
|
||||||
|
strVendor = gl.getParameter(debugInfo?.UNMASKED_VENDOR_WEBGL);
|
||||||
|
strRenderer = gl.getParameter(debugInfo?.UNMASKED_RENDERER_WEBGL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (strRenderer || strVendor) {
|
||||||
|
({ vendor : gpuInfo.vendor, model : gpuInfo.model } = RegexMap.parse(strRenderer, rendererMap));
|
||||||
|
gpuInfo.vendor = gpuInfo.vendor ?? RegexMap.parse(strVendor, rendererMap)?.vendor ?? RegexMap.parse(strVendor, vendorMap)?.vendor;
|
||||||
|
}
|
||||||
|
return gpuInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
GPUDetect
|
||||||
|
}
|
25
src/gpu-detect/package.json
Normal file
25
src/gpu-detect/package.json
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"title": "User-Agent GPU Info",
|
||||||
|
"name": "@ua-parser-js/gpu-detect",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"author": "Faisal Salman <f@faisalman.com>",
|
||||||
|
"description": "Extracts GPU information from user-agent",
|
||||||
|
"main": "gpu-detect.js",
|
||||||
|
"module": "gpu-detect.mjs",
|
||||||
|
"scripts": {
|
||||||
|
"test": "mocha ../../test/mocha-test-helpers"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/faisalman/ua-parser-js.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"ua-parser-js",
|
||||||
|
"gpu-detection"
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/faisalman/ua-parser-js/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/faisalman/ua-parser-js#readme"
|
||||||
|
}
|
16
src/gpu-detect/readme.md
Normal file
16
src/gpu-detect/readme.md
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# @ua-parser-js/gpu-detect
|
||||||
|
|
||||||
|
This is a [UAParser.js](https://github.com/faisalman/ua-parser-js) module that extracts GPU information from user-agent.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm i @ua-parser-js/gpu-detect
|
||||||
|
```
|
||||||
|
|
||||||
|
## Code Example
|
||||||
|
|
||||||
|
// in browser environment
|
||||||
|
const { vendor, model } = GPUDetect.getGPU();
|
||||||
|
|
||||||
|
// in non-browser environment
|
||||||
|
const { vendor, model } = GPUDetect.getGPU("AMD Radeon");
|
||||||
|
```
|
4
src/gpu-detect/test/index.js
Normal file
4
src/gpu-detect/test/index.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
const { GPUDetect } = require('../gpu-detect.js');
|
||||||
|
|
||||||
|
console.log(GPUDetect.getGPU('AMD Radeon R9 M295X OpenGL Engine'));
|
||||||
|
console.log(GPUDetect.getGPU('','ATI Technologies Inc.'));
|
Loading…
x
Reference in New Issue
Block a user