Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
206 lines (167 loc) · 7.02 KB
/
Copy pathindex.html
File metadata and controls
206 lines (167 loc) · 7.02 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script>
var current_date;
function init() {
// This code runs only at page load
var queries = new URLSearchParams(window.location.search);
if (queries.has('theme')) {
var query_theme = queries.get('theme').toLowerCase();
if (query_theme === 'dark') {
dark_theme();
} else {
light_theme();
}
}
// Try to guess theme preference
else if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
dark_theme();
} else {
light_theme();
}
// Check if sfw query parameter passed
var sfw = queries.has('sfw') && ['yes', 'true', '1'].includes(queries.get('sfw').toLowerCase());
if (sfw) {
try {
document.getElementById('sfw_toggle').innerHTML = '<a href="/">Include all quotes</a>';
} catch (e) {
console.log(e);
}
}
startTime(sfw);
}
function loadFile(time) {
var timeStamp = timeToTimecode(time);
return fetch("times/" + timeStamp + ".json")
.then(response => {
if (response.ok) {
return response.json();
} else {
return loadFile(oneMinuteAgo(time));
}
})
.catch(e => console.log("error"));
}
function updateText(time, sfw) {
// This runs every minute
// Load JSON file
loadFile(time).then((lit_json) => {
// Filter SFW if needed
if (sfw) {
lit_json = lit_json.filter(item => item.sfw === 'yes');
}
if (lit_json.length === 0) {
return updateText(oneMinuteAgo(time), sfw);
}
// select single random text
var lit_json_single = lit_json[Math.floor(Math.random() * lit_json.length)]
// get quote_len
var quote_len = (lit_json_single.quote_first +
lit_json_single.quote_time_case +
lit_json_single.quote_last)
.length
// Write to HTML
//document.getElementById('time').innerHTML = timeCode; // Show time
try {
document.getElementById('lit_quote').innerHTML =
lit_json_single.quote_first + '<em>' +
lit_json_single.quote_time_case + '</em>' +
lit_json_single.quote_last;
document.getElementById('book').innerHTML = lit_json_single.title;
document.getElementById('author').innerHTML = lit_json_single.author;
// set fontsize according to length
// Equation to make fontsize match
// y = 6.190864 - 0.01211676*x + 0.00001176814*x^2 - 1.969435e-9*x^3, where x is quote_len
document.getElementById('lit_quote').style.fontSize =
((6.000864 - 0.01211676 * quote_len + 0.00001176814 * quote_len ** 2 - 1.969435e-9 * quote_len ** 3) + "vw");
} catch (e) {
console.log(e);
}
});
}
function updateTime() {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const timeString = `${hours}:${minutes}`;
document.getElementById('time').textContent = timeString;
}
function startTime(sfw) {
// This code runs every second
// could be optimized to check time in miliseconds and set timeout to when minute will change next time.
var time = new Date();
if (current_date !== undefined
&& current_date.getMinutes() === time.getMinutes()) {
var t = setTimeout(startTime, 1000);
return;
}
current_date = time;
//Test longest (20:15)
//Test shortest (08:51)
//time = new Date('Tue Nov 16 2021 22:39:00'); // Test missing quote
//time = new Date('Tue Nov 16 2021 00:05:00'); // Test nsfw time
updateText(time, sfw);
// Timeout
var t = setTimeout(startTime, 1000);
}
function oneMinuteAgo(time) {
return new Date(time - 60000) // Subtract one minute
}
function timeToTimecode(time) {
var h = time.getHours();
var m = time.getMinutes();
// var s = time.getSeconds();
h = checkTime(h);
m = checkTime(m);
// s = checkTime(s);
return h + "_" + m; // fixed for windows compatibility
}
function checkTime(i) {
if (i < 10) { i = "0" + i }; // add zero in front of numbers < 10
return i;
}
function updateTimedisplay() {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const timeString = `${hours}:${minutes}`;
document.getElementById('current-time').textContent = timeString;
}
// Update the time every second
setInterval(updateTime, 1000);
</script>
<link rel="stylesheet" href="styles.css">
<link href='https://fonts.googleapis.com/css?family=Merriweather' rel='stylesheet'>
<script src="theme.js"></script>
<title>Literature Clock</title>
</head>
<body onload="init()">
<!-- <div id="time"></div> -->
<div id="main_text">
<blockquote id="lit_quote"></blockquote>
<cite>
-
<span id="book"></span>,
<span id="author"></span>
</cite>
</div>
<div id="bottom">
<div id="github">
<a href="https://github.com/yangxucmt/litclock">GitHub</a>
</div>
<span id="based_on">
Based on works by
<a href="http://www.eerlijkemedia.nl/">Jaap Meijers</a> (
<a href="https://www.instructables.com/id/Literary-Clock-Made-From-E-reader/">E-reader clock</a>),
<a href="http://litclock.mohawkhq.com/">The Guardian</a> and
<a href="https://github.com/JohannesNE/literature-clock">Johannes Enevoldsen</a>
</span>
<span>
<a id="toggle-theme" onclick="toggle_theme()" href="#">Dark Theme</a>
<a id="current-time" onclick="updateTimedisplay()" href="#">Show time</a>
</span>
</div>
</body>
</html>