-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
94 lines (85 loc) · 2.68 KB
/
main.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
// @ts-check
import "./style.css";
import { detectPlatform } from "./src/platforms.js";
import {
getAppStoreUrl,
getConfig,
getRemoteConfigUrl,
getGooglePlayStoreUrl,
getMicrosoftStoreUrl,
} from "./src/config.js";
import { resolveAndParseRemoteConfigToUrl } from "./src/remoteConfig.js";
function renderFallBack(config) {
let badges = ``;
const appStoreUrl = getAppStoreUrl(config);
if (appStoreUrl) {
badges += `<a href="${appStoreUrl}" rel="noopener noreferrer">
<img src="/us/Download_on_the_App_Store_Badge_US-UK_RGB_blk_092917.svg" class="logo" alt="Download on the App Store" style="width: 150px; height: auto;" />
</a>`;
}
const googlePlayUrl = getGooglePlayStoreUrl(config);
if (googlePlayUrl) {
badges += `<a href="${googlePlayUrl}" rel="noopener noreferrer">
<img src="/us/google-play-badge.png" class="logo" alt="Get it on Google Play" style="width: 200px; height: auto;" />
</a>`;
}
const windowsStoreUrl = getMicrosoftStoreUrl(config);
if (windowsStoreUrl) {
badges += `<a href="${windowsStoreUrl}" rel="noopener noreferrer">
<img src="/us/en-gb dark.svg" class="logo" alt="Get it on the Microsoft Store" style="width: 150px; height: auto;" />
</a>`;
}
if (badges === "") {
badges = `<p>Sorry, this app link does not support your platform.</p>`;
}
// @ts-ignore
document.querySelector("#app").innerHTML = `
<div class="badges">
${badges}
</div>
`;
}
const executeRedirect = async () => {
// detect platform
const platform = detectPlatform(navigator.userAgent);
// determine configuration type
const remoteUrl = getRemoteConfigUrl(location.href);
if (remoteUrl) {
const targetUrl = await resolveAndParseRemoteConfigToUrl(
remoteUrl,
platform,
Intl.DateTimeFormat().resolvedOptions().timeZone
);
if (targetUrl) {
location.href = targetUrl;
} else {
renderFallBack({});
}
} else {
// simple link
const config = getConfig(location.href);
if (
!config.appleAppId &&
!config.googlePackageName &&
!config.microsoftStoreId
) {
renderFallBack(config);
} else {
switch (platform) {
case "Apple":
location.href = `https://apps.apple.com/app/id${config.appleAppId}`;
break;
case "Google":
location.href = `https://play.google.com/store/apps/details?id=${config.googlePackageName}`;
break;
case "Microsoft":
location.href = `https://www.microsoft.com/store/apps/${config.microsoftStoreId}`;
break;
default:
renderFallBack(config);
break;
}
}
}
};
executeRedirect();