0327f49f4e
Co-authored-by: Cursor <cursoragent@cursor.com>
90 lines
2.2 KiB
JavaScript
90 lines
2.2 KiB
JavaScript
(function (global) {
|
|
var deferredPrompt = null;
|
|
|
|
function isStandalone() {
|
|
return (
|
|
(global.matchMedia && global.matchMedia("(display-mode: standalone)").matches) ||
|
|
global.navigator.standalone === true
|
|
);
|
|
}
|
|
|
|
function btn() {
|
|
return document.getElementById("hub-install-btn");
|
|
}
|
|
|
|
function setHint(msg) {
|
|
var el = document.getElementById("hub-install-hint");
|
|
if (el) el.textContent = msg || "";
|
|
}
|
|
|
|
function showInstall() {
|
|
var el = btn();
|
|
if (!el || isStandalone()) return;
|
|
el.hidden = false;
|
|
}
|
|
|
|
function hideInstall() {
|
|
var el = btn();
|
|
if (el) el.hidden = true;
|
|
setHint("");
|
|
}
|
|
|
|
function downloadShortcut() {
|
|
var origin = global.location.origin;
|
|
var body = "[InternetShortcut]\r\nURL=" + origin + "/\r\n";
|
|
var blob = new Blob([body], { type: "application/internet-shortcut" });
|
|
var a = document.createElement("a");
|
|
a.href = URL.createObjectURL(blob);
|
|
a.download = "期权中控.url";
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
setTimeout(function () {
|
|
URL.revokeObjectURL(a.href);
|
|
}, 1000);
|
|
}
|
|
|
|
async function onInstallClick() {
|
|
setHint("");
|
|
if (deferredPrompt) {
|
|
deferredPrompt.prompt();
|
|
try {
|
|
var choice = await deferredPrompt.userChoice;
|
|
deferredPrompt = null;
|
|
if (choice && choice.outcome === "accepted") {
|
|
hideInstall();
|
|
return;
|
|
}
|
|
} catch (e) {
|
|
deferredPrompt = null;
|
|
}
|
|
}
|
|
downloadShortcut();
|
|
setHint("已下载快捷方式,拖到桌面即可。Chrome / Edge 也可在地址栏右侧点「安装」。");
|
|
}
|
|
|
|
function boot() {
|
|
if (isStandalone()) {
|
|
hideInstall();
|
|
return;
|
|
}
|
|
showInstall();
|
|
global.addEventListener("beforeinstallprompt", function (ev) {
|
|
ev.preventDefault();
|
|
deferredPrompt = ev;
|
|
showInstall();
|
|
});
|
|
global.addEventListener("appinstalled", function () {
|
|
deferredPrompt = null;
|
|
hideInstall();
|
|
});
|
|
var el = btn();
|
|
if (el) el.addEventListener("click", onInstallClick);
|
|
if ("serviceWorker" in navigator) {
|
|
navigator.serviceWorker.register("/sw.js", { scope: "/" }).catch(function () {});
|
|
}
|
|
}
|
|
|
|
global.HubPwa = { boot: boot };
|
|
})(window);
|