forked from JARVIS-AI/mini-diary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
91 lines (79 loc) · 2.34 KB
/
index.ts
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
const API_URL = "https://api.github.com/repos/samuelmeuli/mini-diary/releases/latest";
const APP_STORE_URL = "https://itunes.apple.com/app/mini-diary/id1450296884";
const SNAPCRAFT_URL = "https://snapcraft.io/mini-diary";
interface Asset {
browser_download_url: string;
content_type: string;
created_at: string;
download_count: number;
id: number;
label: string;
name: string;
node_id: string;
size: number;
state: string;
updated_at: string;
uploader: any;
url: string;
}
type Extension = "dmg" | "exe" | "AppImage";
enum Platform {
Mac = "mac",
Mas = "mas",
Windows = "windows",
Linux = "linux",
}
const FIXED_URLS: Partial<Record<Platform, string>> = {
[Platform.Mas]: APP_STORE_URL,
[Platform.Linux]: SNAPCRAFT_URL,
};
const PLATFORM_EXTENSIONS: Partial<Record<Platform, Extension>> = {
[Platform.Mac]: "dmg",
[Platform.Windows]: "exe",
};
function getCurrentPlatform(): Platform {
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.match(/(mac|os x)/)) {
return Platform.Mas;
}
if (userAgent.match(/windows/)) {
return Platform.Windows;
}
if (userAgent.match(/linux/)) {
return Platform.Linux;
}
throw Error("Unrecognized platform");
}
function updateVariableDownloadLinks(): void {
const platform = getCurrentPlatform();
const links = document.getElementsByClassName("download");
Array.from(links).forEach(a => {
a.classList.replace("download", `download-${platform}`);
});
}
async function insertDownloadUrls(): Promise<void> {
// Fetch asset list
const apiResponse = await fetch(API_URL);
const assets = (await apiResponse.json()).assets as Asset[];
// Build map of file extensions and asset URLs
const assetMap: Partial<Record<Extension, string>> = {};
assets.forEach(asset => {
const extension = asset.browser_download_url.split(".").slice(-1)[0] as Extension;
assetMap[extension] = asset.browser_download_url;
});
const platforms = Object.values(Platform) as Platform[];
platforms.forEach((platform): void => {
const extension = PLATFORM_EXTENSIONS[platform] as Extension;
const url = FIXED_URLS[platform] || assetMap[extension];
const links = Array.from(
document.getElementsByClassName(`download-${platform}`),
) as HTMLAnchorElement[];
links.forEach((a): void => {
if (url) {
a.setAttribute("href", url);
}
});
});
}
updateVariableDownloadLinks();
insertDownloadUrls();