Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
231 lines (209 loc) · 8.76 KB
/
Copy pathscript.js
File metadata and controls
231 lines (209 loc) · 8.76 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
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
// Texts per language
const APP_VERSION = "0.2.7";
const CHANGELOG_URL = "https://github.com/a4ds5t/almost-xmas/blob/main/CHANGELOG.md";
const APP_BUILD_ISO = typeof BUILD_TIMESTAMP_ISO !== "undefined" ? BUILD_TIMESTAMP_ISO : null;
const NOTO_SERIF_SC_HREF =
"https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@500;700&display=swap";
var TEXT = {
nl: {
locale: "nl-NL", langLabel: "Taal",
today: "Vandaag is het ", headline: "Bijna Kerst", xmasHeadline: "Fijne Kerst",
before: "Nog ", after: ", of precies:",
week: ["week", "weken"], day: ["dag", "dagen"], and: " en ",
units: [["dag", "dagen"], ["uur", "uur"], ["minuut", "minuten"], ["seconde", "seconden"]],
christmas: "Het is zover. Fijne feestdagen!",
months: ["januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"],
footer: "Het is bijna kerst, zelfs in {month}.",
copyright: "De Kerstman. Alle rechten voorbehouden.",
versionUpdated: "Bijgewerkt",
versionChangelog: "Release notes",
versionTagAria: "Toon versie {v} en release notes"
},
en: {
locale: "en-GB", langLabel: "Language",
today: "Today is ", headline: "Almost Christmas", xmasHeadline: "Merry Christmas",
before: "Just ", after: " to go, or exactly:",
week: ["week", "weeks"], day: ["day", "days"], and: " and ",
units: [["day", "days"], ["hour", "hours"], ["minute", "minutes"], ["second", "seconds"]],
christmas: "It’s here. Happy holidays!",
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
footer: "It’s almost Christmas, even in {month}.",
copyright: "Santa Claus. All rights reserved.",
versionUpdated: "Updated",
versionChangelog: "Release notes",
versionTagAria: "Show version {v} and release notes"
},
es: {
locale: "es-ES", langLabel: "Idioma",
today: "Hoy es ", headline: "Casi Navidad", xmasHeadline: "Feliz Navidad",
before: "Faltan ", after: ", o exactamente:",
week: ["semana", "semanas"], day: ["día", "días"], and: " y ",
units: [["día", "días"], ["hora", "horas"], ["minuto", "minutos"], ["segundo", "segundos"]],
christmas: "¡Ya está aquí! ¡Felices fiestas!",
months: ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"],
footer: "Es casi Navidad, incluso en {month}.",
copyright: "Papá Noel. Todos los derechos reservados.",
versionUpdated: "Actualizado",
versionChangelog: "Notas de la versión",
versionTagAria: "Mostrar versión {v} y notas de la versión"
},
zh: {
locale: "zh-CN", htmlLang: "zh-CN", langLabel: "语言",
today: "今天是", headline: "圣诞将至", xmasHeadline: "圣诞快乐",
before: "还有 ", after: ",确切地说:",
week: ["周", "周"], day: ["天", "天"], and: "零 ",
units: [["天", "天"], ["小时", "小时"], ["分钟", "分钟"], ["秒", "秒"]],
christmas: "圣诞节到了!节日快乐!",
months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
footer: "即使是{month},也快到圣诞节了。",
copyright: "圣诞老人。保留所有权利。",
versionUpdated: "更新于",
versionChangelog: "发布说明",
versionTagAria: "显示版本 {v} 和发布说明"
}
};
// Language from the link (?lang=en), then the saved choice, then the first browser language
// that is nl, en, es or zh; otherwise English
function urlLanguage() {
var match = /[?&]lang=([a-z]{2})/i.exec(location.search);
var code = match && match[1].toLowerCase();
return TEXT[code] ? code : null;
}
function detectLanguage() {
var fromUrl = urlLanguage();
if (fromUrl) return fromUrl;
try {
var saved = localStorage.getItem("lang");
if (TEXT[saved]) return saved;
} catch (e) {}
var list = navigator.languages && navigator.languages.length ? navigator.languages : [navigator.language || ""];
for (var i = 0; i < list.length; i++) {
var code = String(list[i]).slice(0, 2).toLowerCase();
if (TEXT[code]) return code;
}
return "en";
}
var lang = detectLanguage();
var versionTagHandle = null;
// Noto Serif SC is only painted for zh. Keep it out of the default render-blocking
// Google Fonts link and inject the stylesheet the first time Chinese is active.
function ensureNotoSerifSC() {
if (document.getElementById("fonts-noto-sc")) return;
var link = document.createElement("link");
link.id = "fonts-noto-sc";
link.rel = "stylesheet";
link.href = NOTO_SERIF_SC_HREF;
document.head.appendChild(link);
}
if (lang === "zh") ensureNotoSerifSC();
function versionTagStrings(t) {
return {
updatedLabel: t.versionUpdated,
changelogLabel: t.versionChangelog,
tagAria: t.versionTagAria
};
}
function copyrightLine(t, today) {
return "© " + today.getFullYear() + " " + t.copyright;
}
function applyVersionTag(today) {
var root = document.getElementById("app-version-root");
if (!root || typeof initVersionTag !== "function") return;
var t = TEXT[lang];
var label = copyrightLine(t, today || new Date());
if (!versionTagHandle) {
versionTagHandle = initVersionTag({
root: root,
version: APP_VERSION,
buildIso: APP_BUILD_ISO,
changelogUrl: CHANGELOG_URL,
label: label,
strings: versionTagStrings(t)
});
} else {
versionTagHandle.update({ label: label, strings: versionTagStrings(t) });
}
}
function plural(pair, n) {
return pair[n == 1 ? 0 : 1];
}
function two(n) {
return n < 10 ? "0" + n : "" + n;
}
function setText(id, text) {
document.getElementById(id).textContent = text;
}
// Footer names the visitor's current local month, the same Date the countdown uses.
function footerLine(t, today) {
return t.footer.replace("{month}", t.months[today.getMonth()]);
}
// Christmas we count down to — 25 December, local time — comes from countdown.js,
// which also holds the generic nextOccurrence() primitive the Easter/Pentecost pages
// will reuse. From 26 December it rolls to next year; second Christmas day is not
// treated as Christmas, because countries differ on it.
function update() {
var t = TEXT[lang];
var today = new Date();
var christmas = XmasCountdown.isChristmasDay(today);
document.documentElement.lang = t.htmlLang || lang;
setText("current_date", t.today + today.toLocaleDateString(t.locale, { weekday: "long", day: "numeric", month: "long", year: "numeric" }));
setText("headline", christmas ? t.xmasHeadline : t.headline);
document.title = christmas ? t.xmasHeadline : t.headline;
setText("christmas", t.christmas);
setText("footer", footerLine(t, today));
applyVersionTag(today);
document.getElementById("countdown").hidden = christmas;
document.getElementById("christmas").hidden = !christmas;
if (christmas) return;
// DST-safe wall-clock split, computed in countdown.js
var remaining = XmasCountdown.getRemaining(today);
var days = remaining.days;
var weeks = Math.floor(days / 7);
var rest = days % 7;
setText("before", t.before);
setText("weeks", weeks + " " + plural(t.week, weeks) + (rest ? t.and + rest + " " + plural(t.day, rest) : ""));
setText("after", t.after);
setText("days", "" + days);
setText("hours", two(remaining.hours));
setText("minutes", two(remaining.minutes));
setText("seconds", two(remaining.seconds));
setText("days_label", plural(t.units[0], days));
setText("hours_label", plural(t.units[1], remaining.hours));
setText("minutes_label", plural(t.units[2], remaining.minutes));
setText("seconds_label", plural(t.units[3], remaining.seconds));
}
function markLanguage() {
var links = document.querySelectorAll(".lang-switch a");
for (var i = 0; i < links.length; i++) {
if (links[i].dataset.lang == lang) {
links[i].setAttribute("aria-current", "true");
} else {
links[i].removeAttribute("aria-current");
}
}
document.querySelector(".lang-switch").setAttribute("aria-label", TEXT[lang].langLabel);
}
// Switch without reloading, but keep the address bar on the shareable ?lang= link
function setLanguage(code) {
lang = code;
if (code === "zh") ensureNotoSerifSC();
try {
localStorage.setItem("lang", code);
} catch (e) {}
try {
history.replaceState(null, "", "?lang=" + code + location.hash);
} catch (e) {}
markLanguage();
update();
}
document.querySelector(".lang-switch").addEventListener("click", function (e) {
var link = e.target.closest("a[data-lang]");
if (!link || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
e.preventDefault();
setLanguage(link.dataset.lang);
});
// Show the page in the detected language without saving it as a choice
markLanguage();
update();
// Update the count down every 1 second
setInterval(update, 1000);