blob: 8f443c39fabe84b18f03685ea87566e905af4420 [file] [log] [blame]
Adrià Vilanova Martínez849f9dd2021-06-25 20:40:10 +02001// @Author: avm99963 (https://www.avm99963.com)
2/* @License: The MIT License (MIT)
3
4Copyright (c) 2021 Adrià Vilanova Martínez
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE. */
23
Adrià Vilanova Martínez5f7640e2021-06-25 01:28:47 +020024// Detect the current OS
25function detectOS() {
26 var linuxRegex = /Linux/i;
27 var winRegex = /Win/i;
28 var winNT7 = /NT 7/i;
29 var winNT6 = /NT 6/i;
30 var macRegex = /Mac/i;
31 var crosRegex = /CrOS/i;
32
33 var uaPromise = null;
34 if (navigator.userAgentData) {
35 uaPromise = navigator.userAgentData
36 .getHighEntropyValues(['platform', 'platformVersion'])
37 .then(ua => {
38 return ua?.platform + ' ' + ua?.platformVersion;
39 });
40 } else {
41 uaPromise = new Promise((res, rej) => {
42 res(navigator.userAgent ?? '');
43 });
44 }
45
46 return uaPromise.then(ua => {
47 if (linuxRegex.test(ua)) return 'linux';
48 if (winRegex.test(ua)) {
49 if (winNT7.test(ua) || winNT6.test(ua)) return 'win_7_or_less';
50 return 'win_latest';
51 }
52 if (macRegex.test(ua)) return 'mac';
53 if (crosRegex.test(ua)) return 'cros';
54 return null;
55 });
56}
57
58// Get the OS from the 'os' GET parameter if set
59function getOSGETParam() {
60 var sp = new URLSearchParams(location.search);
61 if (sp.has('os')) {
62 var os = sp.get('os');
63 switch (os) {
64 case 'win':
65 switch (sp.get('v')) {
66 case '7-':
67 return 'win_7_or_less';
68
69 case 'latest':
70 default:
71 return 'win_latest';
72 }
73 break;
74 case 'linux':
75 return 'linux';
76
77 case 'mac':
78 return 'mac';
79
80 case 'cros':
81 return 'cros';
82
83 default:
84 console.warn('Requested OS "' + os + '" unknown.');
85 }
86 }
87
88 return null;
89}
90
91// Get the target OS
92function getOS() {
93 // See whether a OS is being requested via a GET parameter
94 var os = getOSGETParam();
95 if (os !== null)
96 return new Promise((res, rej) => {
97 res(os);
98 });
99
100 // Otherwise, detect the browser
101 return detectOS();
102}
103
104// Transform the OS into the label id
105function getOSLabel() {
106 return getOS().then(os => {
107 switch (os) {
108 case 'win_latest':
109 return '1';
110
111 case 'win_7_or_less':
112 return '2';
113
114 case 'mac':
115 return '3';
116
117 case 'linux':
118 return '4';
119
120 case 'cros':
121 return '5';
122
123 default:
124 return null;
125 }
126 });
127}
128
129// Open the section corresponding to the target OS
130function openAppropriateSection() {
131 var tabs = document.querySelector('.md-content .tabbed-set');
132
133 getOSLabel().then(labelId => {
134 if (labelId === null) return;
135
136 var label = tabs.querySelector('label[for="__tabbed_1_' + labelId + '"]');
137 label.click();
138 });
139}
140
141window.addEventListener('load', _ => {
142 openAppropriateSection();
143});