-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpasscode.html
232 lines (217 loc) · 7.72 KB
/
passcode.html
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>403 Forbidden</title>
<style>
html,
body {
margin: 0;
height: 100%;
}
body {
display: flex;
flex-direction: column;
background: #eeeeee;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
cursor: default;
}
.centerouter {
display: flex;
position: relative;
flex-direction: column;
align-items: center;
justify-content: center;
}
.flex-1 {
flex: 1;
}
form {
background: white;
border: solid 1px #777777;
box-shadow: 0 0 5px #ccc;
border-radius: 5px;
padding: 15px;
min-width: 300px;
text-align: center;
}
h1 {
margin: 10px;
}
input {
display: block;
width: 100%;
box-sizing: border-box;
height: 40px;
margin: 15px 0;
}
.textbox {
font-size: 15px;
padding: 15px;
}
.footer {
margin: 10px;
color: #666666;
}
a {
color: inherit;
text-decoration: none;
}
a:hover {
color: black;
}
</style>
</head>
<body>
<div class="centerouter flex-1">
<form action="/passcode" method="post">
<h1 class="i18ne">Access Forbidden</h1>
<p class="i18ne">A passcode is required.</p>
<input id="passcode" type="password" class="textbox" name="passcode" placeholder="Passcode" autofocus>
<input id="btnSubmit" type="submit" value="Proceed">
</form>
</div>
<div class="footer">
© 2020 - <a href="https://yuuza.net/">Yuuza</a>
- <a href="http://beian.miit.gov.cn/" target="_blank">桂ICP备19010850号-1</a>
</div>
<script>
class I18n {
constructor() {
this.data = {};
this.curLang = 'en';
this.missing = new Map();
}
/** Get i18n string for `key`, return `key` when not found. */
get(key, arg) {
return this.get2(key, arg) || key;
}
/** Get i18n string for `key`, return `null` when not found. */
get2(key, arg, lang) {
lang = lang || this.curLang;
var langObj = this.data[lang];
if (!langObj) {
console.log('i18n missing lang: ' + lang);
return null;
}
var r = langObj[key];
if (!r) {
if (!this.missing.has(key)) {
this.missing.set(key, 1);
console.log('i18n missing key: ' + key);
}
return null;
}
if (arg) {
for (const key in arg) {
if (arg.hasOwnProperty(key)) {
const val = arg[key];
r = r.replace('{' + key + '}', val);
// Note that it only replaces the first occurrence.
}
}
}
return r;
}
/** Fills data with an 2darray */
add2dArray(array) {
const langObjs = [];
const langs = array[0];
for (const lang of langs) {
langObjs.push(this.data[lang] = this.data[lang] || {});
}
for (let i = 1; i < array.length; i++) {
const line = array[i];
const key = line[0];
for (let j = 0; j < line.length; j++) {
const val = line[j];
langObjs[j][key] = val;
}
}
}
renderElements(elements) {
console.log('i18n elements rendering');
elements.forEach(x => {
for (const node of x.childNodes) {
if (node.nodeType == Node.TEXT_NODE) {
// console.log('node', node);
var r = this.get2(node.beforeI18n || node.textContent);
if (r) {
node.beforeI18n = node.beforeI18n || node.textContent;
node.textContent = r;
}
else {
if (node.beforeI18n) {
node.textContent = node.beforeI18n;
}
console.log('missing key for node', node);
}
}
}
});
}
/**
* Detect the best available language using
* the user language preferences provided by the browser.
* @param langs Available languages
*/
static detectLanguage(langs) {
var cur;
var curIdx = -1;
var languages = [];
// ['en-US'] -> ['en-US', 'en']
(navigator.languages || [navigator.language]).forEach(lang => {
languages.push(lang);
if (lang.indexOf('-') > 0)
languages.push(lang.substr(0, lang.indexOf('-')));
});
langs.forEach((l) => {
var idx = languages.indexOf(l);
if (!cur || (idx !== -1 && idx < curIdx)) {
cur = l;
curIdx = idx;
}
});
return cur;
}
}
var i18n = new I18n();
function I(literals, ...placeholders) {
if (placeholders.length == 0) {
return i18n.get(literals[0]);
}
// Generate format string from template string:
var formatString = '';
for (var i = 0; i < literals.length; i++) {
var lit = literals[i];
formatString += lit;
if (i < placeholders.length) {
formatString += '{' + i + '}';
}
}
var r = i18n.get(formatString);
for (var i = 0; i < placeholders.length; i++) {
r = r.replace('{' + i + '}', placeholders[i]);
}
return r;
}
// Use JSON.parse(a_big_json) for faster JavaScript runtime parsing
i18n.add2dArray(JSON.parse(`[
["en", "zh"],
["Access Forbidden", "拒绝访问"],
["A passcode is required.", "需要口令"],
["Proceed", "确认"],
["Passcode", "口令"]
]`));
i18n.curLang = I18n.detectLanguage(['en', 'zh']);
i18n.renderElements(document.querySelectorAll('.i18ne'));
function renderProperty(obj, key) {
obj[key] = i18n.get(obj[key]);
}
renderProperty(document.getElementById('btnSubmit'), 'value');
renderProperty(document.getElementById('passcode'), 'placeholder');
</script>
</body>
</html>