-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
136 lines (121 loc) · 4.87 KB
/
index.js
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
//
// index.js
// altsource-viewer (https://github.com/therealFoxster/altsource-viewer)
//
// Copyright (c) 2023 Foxster.
// MIT License.
//
import { urlSearchParams, sourceURL } from "./common/modules/constants.js";
import { isValidHTTPURL, open, formatVersionDate, json } from "./common/modules/utilities.js";
// import sources from "./common/assets/json/sources.json" assert { type: "json" }; // Doesn't work in Safari
// const { default: sources } = await import("./common/assets/json/sources.json", {assert: { type: "json" } }); // Broken on Safari 17.2
const sources = await json("./common/assets/json/sources.json");
(async function main() {
const fetchedSources = [];
for (const url of sources) {
const source = await fetchSource(url);
if (!source) continue;
fetchedSources.push(source);
}
// Sort sources by last updated
fetchedSources.sort((a, b) => b.lastUpdated - a.lastUpdated);
for (const source of fetchedSources) {
await insertSource(source);
}
document.body.classList.remove("loading");
document.getElementById("loading")?.remove();
const textField = document.querySelector("input");
const goButton = document.getElementById("go");
const viewSource = () => {
const sourceURL = textField.value;
if (!isValidHTTPURL(sourceURL))
alert("Invalid HTTP URL.");
else open(`./view/?source=${sourceURL}`);
// else insertSource(sourceURL, "afterbegin", true);
};
// If source provided
if (urlSearchParams.has('source')) {
textField.value = urlSearchParams.get("source");
textField.focus();
}
textField.addEventListener("keypress", event => {
if (event.key === "Enter") {
event.preventDefault();
viewSource();
}
});
const toggleGoButton = () => {
if (textField.value.length) {
goButton.style.display = "block";
setTimeout(() => {
goButton.style.opacity = 1;
}, 5);
} else {
goButton.style.opacity = 0;
setTimeout(() => {
goButton.style.display = "none";
}, 125);
}
}; toggleGoButton();
textField.addEventListener("input", toggleGoButton);
goButton.addEventListener("click", viewSource);
async function fetchSource(url) {
const source = await json(url);
if (!source) return;
source.lastUpdated = new Date("1970-01-01");
source.appCount = 0;
for (const app of source.apps) {
if (app.beta || app.patreon?.hidden) return;
let appVersionDate = new Date(app.versions ? app.versions[0].date : app.versionDate);
if (appVersionDate > source.lastUpdated) {
source.lastUpdated = appVersionDate;
if (!source.iconURL)
source.iconURL = app.iconURL;
if (!source.tintColor)
source.tintColor = app.tintColor;
}
source.appCount++;
}
if (!source.iconURL)
source.iconURL = "./common/assets/img/generic_app.jpeg";
if (!source.tintColor)
source.tintColor = "var(--tint-color);";
source.url = url;
return source;
}
async function insertSource(source, position = "beforeend", flag = false) {
document.getElementById("suggestions").insertAdjacentHTML(position, `
<div class="source-container">
<a href="./view/?source=${source.url}" class="source-link">
<div class="source" style="
background-color: #${source.tintColor.replaceAll("#", "")};
margin-bottom: ${flag ? "0.75rem" : "0"};
">
<img src="${source.iconURL}" alt="source-icon">
<div class="right">
<div class="text">
<p class="title">${source.name}</p>
<p class="subtitle">Last updated: ${formatVersionDate(source.lastUpdated)}</p>
</div>
<div class="app-count">
${source.appCount} app${source.appCount === 1 ? "" : "s"}
</div>
</div>
</div>
</a>
</div>
`);
}
window.onscroll = e => {
const title = document.querySelector("h1");
const navBar = document.getElementById("nav-bar");
const navBarTitle = navBar.querySelector("#title");
if (title.getBoundingClientRect().y < 32) {
navBar.classList.remove("hide-border");
navBarTitle.classList.remove("hidden");
} else {
navBar.classList.add("hide-border");
navBarTitle.classList.add("hidden");
}
}
})();