-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
146 lines (130 loc) · 4.9 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
137
138
139
140
141
142
143
144
145
146
if ("serviceWorker" in navigator && location.hostname !== "localhost") {
window.addEventListener("load", () => {
navigator.serviceWorker.register("service-worker.js");
});
}
import "../less/style.less";
import LiquidWallet from "./LiquidWallet.js";
import UI from "./ui/UI.js";
import Html from "./ui/Html.js";
import LinkOpener from "./utils/LinkOpener.js";
/**
* The entry point for the Liquid Wallet APP
*/
async function versionCheck(ui) {
try {
const currentVersion = (
await fetch("version.txt?rand=" + Date.now() + "_" + Math.random(), {
cache: "no-store",
}).then((r) => r.text())
).trim();
const latestGithubReleaseData = await fetch(
"https://api.github.com/repos/riccardobl/anser-liquid/releases/latest",
{
cache: "no-store",
},
).then((r) => r.json());
if (!latestGithubReleaseData.tag_name) throw new Error("Cannot get latest version from github");
const latestVersion = latestGithubReleaseData.tag_name.trim();
console.log("Current version", currentVersion);
console.log("Latest version", latestVersion);
if (currentVersion != latestVersion) {
const alertEl = ui.info(
"New version available: " + latestVersion + ". Click here to visit the release page.",
);
alertEl.addEventListener("click", () => {
LinkOpener.navigate(latestGithubReleaseData.html_url);
});
}
} catch (e) {
console.log(e);
}
}
function capturePWAPrompt() {
window.addEventListener("beforeinstallprompt", (e) => {
e.preventDefault();
window.installPWA = () => {
e.prompt();
e.userChoice.then(function (choiceResult) {
try {
console.log("PWA outcome", choiceResult.outcome);
if (choiceResult.outcome == "dismissed") {
console.log("PWA User cancelled home screen install");
} else {
console.log("PWA User added to home screen");
}
window.installPWA = null;
} catch (e) {
console.error(e);
}
});
};
});
}
function renderPWAInstallPrompt(ui) {
if (window.installPWA && !localStorage.getItem("pwaInstallPrompt")) {
const installPromptEl = ui.perma(`
Do you want to install this app on your device for a better experience?
`);
const acceptButtonEl = document.createElement("button");
acceptButtonEl.classList.add("button");
installPromptEl.appendChild(acceptButtonEl);
acceptButtonEl.innerText = "Install";
acceptButtonEl.addEventListener("click", () => {
console.log("PWA", "Install");
if (window.installPWA) window.installPWA();
});
const cancelButtonEl = document.createElement("button");
installPromptEl.appendChild(cancelButtonEl);
cancelButtonEl.classList.add("button");
cancelButtonEl.innerText = "Continue without installing";
cancelButtonEl.addEventListener("click", () => {
console.log("PWA", "cancel");
localStorage.setItem("pwaInstallPrompt", "dismissed");
});
} else {
console.log("PWA", "custom dialog not supported or already installed");
}
}
async function main() {
capturePWAPrompt();
try {
// Get the wallet element
const walletEl = document.body.querySelector("#liquidwallet");
if (!walletEl) alert("No wallet element found");
// A container that is vertical in portrait and horizontal in landscape
const containerEl = Html.$list(walletEl, ["p$v", "l$h"], "container");
containerEl.classList.add("popupContainer");
// Create and start the wallet
const lq = new LiquidWallet();
await lq.start();
// create the UI
const ui = new UI(containerEl, walletEl, lq);
ui.captureOutputs();
renderPWAInstallPrompt(ui);
window.dbui = ui;
window.dblq = lq;
try {
ui.useBrowserHistory(); // allow ui to control the browser history (this is used to support the back button)
ui.setStage("wallet"); // set the initial stage
lq.addRefreshCallback(() => {
try {
ui.reload();
} catch (e) {
console.error(e);
}
}); // refresh the ui when the wallet data changes
} catch (e) {
console.error(e);
}
versionCheck(ui);
} catch (e) {
console.error(e);
if (e.cause == "liquid_not_available") {
window.location.href = "liquidnotfound.html";
} else {
alert(e);
}
}
}
window.addEventListener("load", main);