forked from nc3266/FRIScreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
242 lines (193 loc) · 9.69 KB
/
index.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
233
234
235
236
237
238
239
240
241
242
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FRIScreen</title>
<script>
const dateTimeRegex = /[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}/g;
const fileNameTimeRegex = "(?<=_)[0-9]+(?=s\\.[a-z]*)";
const imageFormats = ["apng", "avif", "bmp", "gif", "tiff", "ico", "cur", "jpg", "jpeg", "jfif", "pjpeg", "pjp", "png", "svg", "webp"];
const videoFormats = ["mp4", "webm", "ogg"];
let config = {};
let data = {};
const UPDATE_DELAY = 200;
const CONF_MEDIA_DURATION = "present";
const CONF_RESIZE_FULLSCREEN = 'resize_to_fullscreen';
const CONF_REFRESH_MEDIA = 'refresh';
const CONF_REFRESH_CONF = 'config_refresh';
const CONF_RELOAD_PAGE = 'reload_page';
const CONF_FADE = 'fade_ms';
const CONF_URLS = 'urls';
// Config constants (if you change them in config, you only need to update them here)
</script>
<style>
body {
padding: 0;
margin: 0;
}
img, video {
position: absolute;
left: 0;
top: 0;
height: 100vh;
width: 100vw;
}
</style>
</head>
<body>
<div id="container" style="height: 100vh; width: 100vw; display: flex; justify-content: center; align-items: center; overflow: hidden"></div>
<script>
const container = document.getElementById("container");
let setNextMediaItemTimeout = null;
let configurationUpdating = false;
let mediaLibraryUpdating = false;
let mediaItemUpdating = false;
let currentElement = null;
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay));
const ISOFromString = (name) => Array.from(name.replace(/\./g, ':').matchAll(dateTimeRegex)).map(x => Date.parse(x[0]));
function createMediaItem(name, target) {
const regexTime = name.match(fileNameTimeRegex);
const tPi = regexTime === null ? config[CONF_MEDIA_DURATION] : parseInt(regexTime[0]);
return {"url": target, "name": name, "tPi": tPi};
}
function createMediaElement(tag, id, target) {
const element = document.createElement(tag);
element.setAttribute("id", id);
element.setAttribute("src", target);
element.style.objectFit = config[CONF_RESIZE_FULLSCREEN] ? "fill" : "contain";
element.style.display = "block";
element.style.opacity = "0.0";
element.addEventListener("error", handleMediaError);
return element;
}
async function crossFade(oldElement, newElement, duration) {
const step = 0.01;
const sleepDelay = duration * step;
while (parseFloat(newElement.style.opacity) < 1.0) {
oldElement.style.opacity = (parseFloat(oldElement.style.opacity) - step).toString();
newElement.style.opacity = (parseFloat(newElement.style.opacity) + step).toString();
await sleep(sleepDelay);
}
oldElement.style.opacity = "0.0";
newElement.style.opacity = "1.0";
}
async function updateConfiguration() {
while (configurationUpdating || mediaLibraryUpdating || mediaItemUpdating) {
await sleep(UPDATE_DELAY);
}
configurationUpdating = true;
const searchParams = new URLSearchParams(window.location.search);
const configurationUrl = searchParams.get("config") ??"./config.json";
const response = await fetch(configurationUrl, {headers: {"cache-control": "no-cache"}});
if (response.status === 200) {
config = await response.json();
}
configurationUpdating = false;
setTimeout(updateConfiguration, (config[CONF_REFRESH_CONF] ?? 1) * 1000);
}
async function updateMediaLibrary() {
while (configurationUpdating || mediaLibraryUpdating || mediaItemUpdating) {
await sleep(UPDATE_DELAY);
}
mediaLibraryUpdating = true;
for (const url of config[CONF_URLS] ?? []) {
data = await fetchMediaItems(url);
if (Object.values(data).findIndex(x => x.media.length > 0) !== -1) {
break;
}
}
mediaLibraryUpdating = false
setTimeout(updateMediaLibrary, config[CONF_REFRESH_MEDIA] * 1000);
}
async function fetchMediaItems(mediaUrl, parent="global", startTime=0, endTime=0) {
const response = await fetch(mediaUrl);
if (response.status !== 200) {
return [];
}
const responseText = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(responseText, "text/html");
let result = {};
result[parent] = {start: startTime, end: endTime, media: []};
for (const anchor of doc.getElementsByTagName("a")) {
if (!anchor.hasAttribute("href")) {
continue;
}
const href = anchor.getAttribute("href");
const target = href.startsWith("http") ? href : `${mediaUrl}/${href}`.replace(/([^:])(\/\/+)/g, '$1/');
// const fname = anchor.innerText;
const fname = target.replace(/\/$/, '').replace(/.*\//, '');
const fileExtension = fname.split(".").reverse()[0].toLowerCase();
const dates = ISOFromString(fname);
if (dates.length === 2) {
const subfolder = await fetchMediaItems(target, fname, dates[0], dates[1]);
if (subfolder[fname].media.length !== 0) {
result = Object.assign({}, result, subfolder);
}
} else if (imageFormats.includes(fileExtension) || videoFormats.includes(fileExtension)) {
result[parent].media.push(createMediaItem(fname, target));
}
}
return result;
}
async function setNextMediaItem() {
while (configurationUpdating || mediaLibraryUpdating || mediaItemUpdating) {
await sleep(UPDATE_DELAY);
}
mediaItemUpdating = true;
const parent = Object.keys(data).find(key =>
Date.now() >= data[key].start && Date.now() <= data[key].end
) ?? "global";
if (!Object.keys(data).includes(parent) || data[parent].media.length === 0) {
mediaItemUpdating = false;
setNextMediaItemTimeout = setTimeout(setNextMediaItem, 1000);
return;
}
let index = data[parent].media.findIndex(x => x["url"] === currentElement?.getAttribute("src")) + 1;
if (index >= data[parent].media.length) {
index = 0;
}
const fileExtension = data[parent].media[index]["name"].split(".").reverse()[0].toLowerCase();
const element = imageFormats.includes(fileExtension) ?
createMediaElement("img", "image-container", data[parent].media[index]["url"]) :
createMediaElement("video", "video-container", data[parent].media[index]["url"]);
container.appendChild(element);
if (container.contains(currentElement)) {
await crossFade(currentElement, element, config[CONF_FADE]);
container.removeChild(currentElement);
} else {
element.style.opacity = "1.0";
}
if (element.tagName === "VIDEO") {
try {
element.muted = false;
await element.play();
} catch {
element.muted = true;
await element.play();
}
data[parent].media[index]["tPi"] = element.duration;
}
currentElement = element;
mediaItemUpdating = false;
setNextMediaItemTimeout = setTimeout(setNextMediaItem, data[parent].media[index]["tPi"] * 1000);
}
async function handleMediaError() {
await updateMediaLibrary();
clearTimeout(setNextMediaItemTimeout);
await setNextMediaItem();
}
async function start() {
await updateConfiguration();
while (Object.keys(config).length === 0) {
console.log("Trying to fetch configuration file");
await sleep(1000);
}
await updateMediaLibrary();
await setNextMediaItem();
setInterval(() => window.location.reload(), config[CONF_RELOAD_PAGE] * 1000);
}
start();
</script>
</body>
</html>