Skip to content

Commit

Permalink
Merge branch 'master' into feat/quark-detect
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalman authored Jul 31, 2024
2 parents c160b3e + 62fb6c2 commit 1daa8f5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/enums/ua-parser-enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const Browser = Object.freeze({
FLOW: 'Flow',
GO: 'GoBrowser',
GOOGLE_SEARCH: 'GSA',
HELIO: 'Helio',
HEYTAP: 'HeyTap',
HUAWEI: 'Huawei Browser',
ICAB: 'iCab',
Expand Down Expand Up @@ -343,7 +344,6 @@ const OS = Object.freeze({
UBUNTU: 'Ubuntu',
UNIX: 'Unix',
VECTORLINUX: 'VectorLinux',
VIERA: 'Viera',
WATCHOS: 'watchOS',
WEBOS: 'WebOS',
WINDOWS: 'Windows',
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/ua-parser-helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

import { IResult } from "../main/ua-parser";

declare function getDeviceVendor(model: string): string | undefined;
declare function isAppleSilicon(res: IResult): boolean;
declare function isChromeFamily(res: IResult): boolean;
declare function isFrozenUA(ua: string): boolean;
declare function isStandalonePWA(): boolean;

export {
getDeviceVendor,
isAppleSilicon,
isChromeFamily,
isFrozenUA,
Expand Down
24 changes: 23 additions & 1 deletion src/helpers/ua-parser-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,29 @@
/*jshint esversion: 6 */

const { CPU, OS, Engine } = require('../enums/ua-parser-enums');
const { UAParser } = require('../main/ua-parser');

const isAppleSilicon = (res) => res.os.is(OS.MACOS) && res.cpu.is(CPU.ARM);
const getDeviceVendor = (model) => UAParser(`Mozilla/5.0 (Linux; Android 10; ${model}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36`).device.vendor;

const isAppleSilicon = (res) => {
if (res.os.is(OS.MACOS)) {
if (res.cpu.is(CPU.ARM)) {
return true;
}
try {
const canvas = document.createElement('canvas');
const webgl = canvas.getContext('webgl2') || canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
const debug = webgl.getExtension('WEBGL_debug_renderer_info');
const renderer = webgl.getParameter(debug.UNMASKED_RENDERER_WEBGL);
if (renderer.match(/apple m\d/i)) {
return true;
}
} catch {
return false;
}
}
return false;
}

const isChromeFamily = (res) => res.engine.is(Engine.BLINK);

Expand All @@ -26,6 +47,7 @@ const isStandalonePWA = () => window && (window.matchMedia('(display-mode: stand
document.referrer.startsWith('app-info://platform/microsoft-store'));

module.exports = {
getDeviceVendor,
isAppleSilicon,
isChromeFamily,
isFrozenUA,
Expand Down
19 changes: 18 additions & 1 deletion test/mocha-test-helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
const assert = require('assert');
const { UAParser } = require('../src/main/ua-parser');
const { isAppleSilicon, isChromeFamily } = require('../src/helpers/ua-parser-helpers');
const { getDeviceVendor, isAppleSilicon, isChromeFamily } = require('../src/helpers/ua-parser-helpers');

describe('getDeviceVendor', () => {
it('Can guess the device vendor from a model name', () => {

const modelSM = 'SM-A605G';
const modelRedmi = 'Redmi Note 8';
const modelNexus = 'Nexus 6P';
const modelQuest = 'Quest 3';
const modelAquos = 'AQUOS-TVX19B';

assert.equal(getDeviceVendor(modelSM), 'Samsung');
assert.equal(getDeviceVendor(modelRedmi), 'Xiaomi');
assert.equal(getDeviceVendor(modelNexus), 'Huawei');
assert.equal(getDeviceVendor(modelQuest), 'Facebook');
assert.equal(getDeviceVendor(modelAquos), 'Sharp');
});
});

describe('isAppleSilicon', () => {
it('Can detect Apple Silicon device', () => {
Expand Down
10 changes: 10 additions & 0 deletions test/specs/browser-all.json
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,16 @@
"major" : "1"
}
},
{
"desc" : "Helio",
"ua" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Helio/0.98.20",
"expect" :
{
"name" : "Helio",
"version" : "0.98.20",
"major" : "0"
}
},
{
"desc" : "HeyTap",
"ua" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.61 Safari/537.36 HeyTapBrowser/40.8.10.1",
Expand Down

0 comments on commit 1daa8f5

Please sign in to comment.