blob: 4ef3e73dd974602fd743557b48e035bd170c1822 [file] [log] [blame]
Adrià Vilanova Martínez5f7640e2021-06-25 01:28:47 +02001// Detect the current OS
2function detectOS() {
3 var linuxRegex = /Linux/i;
4 var winRegex = /Win/i;
5 var winNT7 = /NT 7/i;
6 var winNT6 = /NT 6/i;
7 var macRegex = /Mac/i;
8 var crosRegex = /CrOS/i;
9
10 var uaPromise = null;
11 if (navigator.userAgentData) {
12 uaPromise = navigator.userAgentData
13 .getHighEntropyValues(['platform', 'platformVersion'])
14 .then(ua => {
15 return ua?.platform + ' ' + ua?.platformVersion;
16 });
17 } else {
18 uaPromise = new Promise((res, rej) => {
19 res(navigator.userAgent ?? '');
20 });
21 }
22
23 return uaPromise.then(ua => {
24 if (linuxRegex.test(ua)) return 'linux';
25 if (winRegex.test(ua)) {
26 if (winNT7.test(ua) || winNT6.test(ua)) return 'win_7_or_less';
27 return 'win_latest';
28 }
29 if (macRegex.test(ua)) return 'mac';
30 if (crosRegex.test(ua)) return 'cros';
31 return null;
32 });
33}
34
35// Get the OS from the 'os' GET parameter if set
36function getOSGETParam() {
37 var sp = new URLSearchParams(location.search);
38 if (sp.has('os')) {
39 var os = sp.get('os');
40 switch (os) {
41 case 'win':
42 switch (sp.get('v')) {
43 case '7-':
44 return 'win_7_or_less';
45
46 case 'latest':
47 default:
48 return 'win_latest';
49 }
50 break;
51 case 'linux':
52 return 'linux';
53
54 case 'mac':
55 return 'mac';
56
57 case 'cros':
58 return 'cros';
59
60 default:
61 console.warn('Requested OS "' + os + '" unknown.');
62 }
63 }
64
65 return null;
66}
67
68// Get the target OS
69function getOS() {
70 // See whether a OS is being requested via a GET parameter
71 var os = getOSGETParam();
72 if (os !== null)
73 return new Promise((res, rej) => {
74 res(os);
75 });
76
77 // Otherwise, detect the browser
78 return detectOS();
79}
80
81// Transform the OS into the label id
82function getOSLabel() {
83 return getOS().then(os => {
84 switch (os) {
85 case 'win_latest':
86 return '1';
87
88 case 'win_7_or_less':
89 return '2';
90
91 case 'mac':
92 return '3';
93
94 case 'linux':
95 return '4';
96
97 case 'cros':
98 return '5';
99
100 default:
101 return null;
102 }
103 });
104}
105
106// Open the section corresponding to the target OS
107function openAppropriateSection() {
108 var tabs = document.querySelector('.md-content .tabbed-set');
109
110 getOSLabel().then(labelId => {
111 if (labelId === null) return;
112
113 var label = tabs.querySelector('label[for="__tabbed_1_' + labelId + '"]');
114 label.click();
115 });
116}
117
118window.addEventListener('load', _ => {
119 openAppropriateSection();
120});