Skip to content

Commit cc8fce3

Browse files
committed
chore: remove device detector to minimize js execution time
1 parent 0848830 commit cc8fce3

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"@fortawesome/vue-fontawesome": "^3.0.3",
1919
"@pivot-lang/create-monaco": "link:src/hooks/monaco",
2020
"axios": "^1.3.6",
21-
"device-detector-js": "^3.0.3",
2221
"echarts": "^5.4.2",
2322
"prismjs": "^1.29.0",
2423
"vite-plugin-top-level-await": "^1.3.0",

pnpm-lock.yaml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hooks/detectDeviceType.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
import { Ref, ref } from 'vue';
2-
import DeviceDetector from 'device-detector-js';
32

43
type osType = 'linux' | 'windows' | 'apple' | 'other';
54

6-
const deviceDetector = new DeviceDetector();
75

8-
export const useDetectDeviceType = () => {
9-
const deviceType = ref<osType>('other');
10-
const useAgent = navigator.userAgent;
11-
const name = deviceDetector.parse(useAgent).os?.name;
12-
if (name === 'Mac') {
13-
deviceType.value = 'apple';
14-
} else if (name?.includes('Windows')) {
15-
deviceType.value = 'windows';
16-
} else if (name?.includes('Linux') || name?.includes('Debian') || name?.includes('Ubuntu')) {
17-
deviceType.value = 'linux';
6+
7+
function getOS(): osType {
8+
const userAgent = window.navigator.userAgent,
9+
10+
platform = (window.navigator as any)?.userAgentData?.platform || window.navigator.platform,
11+
macosPlatforms = ['macOS', 'Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
12+
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
13+
iosPlatforms = ['iPhone', 'iPad', 'iPod'];
14+
let os:osType = 'other';
15+
16+
if (macosPlatforms.indexOf(platform) !== -1) {
17+
os = 'apple';
18+
} else if (iosPlatforms.indexOf(platform) !== -1) {
19+
os = 'other';
20+
} else if (windowsPlatforms.indexOf(platform) !== -1) {
21+
os = 'windows';
22+
} else if (/Android/.test(userAgent)) {
23+
os = 'other';
24+
} else if (/Linux/.test(platform)) {
25+
os = 'linux';
1826
}
27+
28+
return os;
29+
}
30+
31+
export const useDetectDeviceType = () => {
32+
const deviceType = ref<osType>(getOS());
1933
return deviceType;
2034
};

0 commit comments

Comments
 (0)