forked from TurboWarp/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavigator.js
More file actions
133 lines (124 loc) · 3.57 KB
/
navigator.js
File metadata and controls
133 lines (124 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Name: Navigator
// ID: navigatorinfo
// Description: Details about the user's browser and operating system.
// Context: "Navigator" refers to someone's browser
// License: MIT AND MPL-2.0
(function (Scratch) {
"use strict";
class NavigatorInfo {
getInfo() {
return {
id: "navigatorinfo",
name: Scratch.translate("Navigator Info"),
blocks: [
{
opcode: "getOS",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("operating system"),
},
{
opcode: "getBrowser",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("browser"),
},
{
opcode: "getMemory",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("device memory in GB"),
},
{
opcode: "getPreferredColorScheme",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("user prefers [THEME] color scheme?"),
arguments: {
THEME: {
type: Scratch.ArgumentType.STRING,
menu: "THEME",
defaultValue: "dark",
},
},
},
{
opcode: "getPreferredReducedMotion",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("user prefers reduced motion?"),
},
{
opcode: "getPreferredContrast",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("user prefers more contrast?"),
},
],
menus: {
THEME: {
acceptReporters: true,
items: [
{
text: Scratch.translate("light"),
value: "light",
},
{
text: Scratch.translate("dark"),
value: "dark",
},
],
},
},
};
}
getOS() {
const userAgent = navigator.userAgent;
if (userAgent.includes("Windows")) {
return "Windows";
} else if (userAgent.includes("Android")) {
return "Android";
} else if (
userAgent.includes("iPhone") ||
userAgent.includes("iPod") ||
userAgent.includes("iPad")
) {
return "iOS";
} else if (userAgent.includes("Linux")) {
return "Linux";
} else if (userAgent.includes("CrOS")) {
return "ChromeOS";
} else if (userAgent.includes("Mac OS")) {
return "macOS";
}
return "Other";
}
getBrowser() {
const userAgent = navigator.userAgent;
if (userAgent.includes("Chrome")) {
return "Chrome";
} else if (userAgent.includes("Firefox")) {
return "Firefox";
} else if (userAgent.includes("Safari")) {
return "Safari";
}
return "Other";
}
getMemory() {
// @ts-expect-error
if (navigator.deviceMemory == undefined) {
return "Unsupported";
} else {
// @ts-expect-error
return navigator.deviceMemory;
}
}
getPreferredColorScheme(args) {
return (
window.matchMedia("(prefers-color-scheme: dark)").matches ===
(args.THEME === "dark")
);
}
getPreferredReducedMotion() {
return !!window.matchMedia("(prefers-reduced-motion: reduce)").matches;
}
getPreferredContrast() {
return !!window.matchMedia("(prefers-contrast: more)").matches;
}
}
Scratch.extensions.register(new NavigatorInfo());
})(Scratch);