-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-banner.js
More file actions
118 lines (103 loc) · 3.35 KB
/
Copy pathupdate-banner.js
File metadata and controls
118 lines (103 loc) · 3.35 KB
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
window.PaperDockUpdateBanner = (() => {
function checkAndRender({ container, language = "system", compact = false } = {}) {
if (!container) return Promise.resolve(null);
return sendMessage({ type: "checkForUpdate", force: false })
.then((result) => {
render(container, result, language, compact);
return result;
})
.catch(() => {
hide(container);
return null;
});
}
function renderResult({ container, result, language = "system", compact = false } = {}) {
if (!container) return;
render(container, result, language, compact);
}
function render(container, result, language, compact) {
if (!result?.updateAvailable || !result.latestZipUrl) {
hide(container);
return;
}
container.hidden = false;
container.dataset.updateAvailable = "true";
container.classList.toggle("is-compact", Boolean(compact));
container.replaceChildren();
const body = document.createElement("div");
body.className = "pd-update-body";
const title = document.createElement("strong");
title.className = "pd-update-title";
title.textContent = t(language, "updateBannerTitle", {
version: result.latestVersion,
tag: result.latestTag
});
const text = document.createElement("p");
text.className = "pd-update-text";
text.textContent = t(language, "updateBannerBody");
const actions = document.createElement("div");
actions.className = "pd-update-actions";
const download = document.createElement("a");
download.className = "pd-update-download";
download.href = result.latestZipUrl;
download.target = "_blank";
download.rel = "noreferrer";
download.textContent = t(language, "downloadLatestStable");
const guide = document.createElement("button");
guide.className = "pd-update-guide";
guide.type = "button";
guide.textContent = t(language, "viewUpgradeGuide");
guide.addEventListener("click", openOptionsPage);
actions.append(download, guide);
body.append(title, text);
container.append(body, actions);
}
function hide(container) {
container.hidden = true;
container.dataset.updateAvailable = "false";
container.replaceChildren();
}
function openOptionsPage() {
try {
if (chrome.runtime?.openOptionsPage) {
chrome.runtime.openOptionsPage();
return;
}
} catch {}
try {
chrome.runtime?.sendMessage?.({ type: "openOptions" });
return;
} catch {}
try {
window.open(chrome.runtime.getURL("options.html"), "_blank", "noopener");
} catch {}
}
function sendMessage(payload) {
return new Promise((resolve, reject) => {
try {
chrome.runtime.sendMessage(payload, (response) => {
const lastError = chrome.runtime.lastError;
if (lastError) {
reject(new Error(lastError.message));
return;
}
if (!response?.ok) {
reject(new Error(response?.error || "Extension background returned no valid response."));
return;
}
resolve(response.data);
});
} catch (error) {
reject(error);
}
});
}
function t(language, key, vars = {}) {
const i18n = window.PaperDockI18n;
return i18n?.t ? i18n.t(language, key, vars) : key;
}
return {
checkAndRender,
renderResult
};
})();