-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannouncement.js
More file actions
297 lines (265 loc) · 9.05 KB
/
Copy pathannouncement.js
File metadata and controls
297 lines (265 loc) · 9.05 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// ============================================
// AI Chat Pro Client - Remote Announcement Banner
// Fetches a JSON message from a remote endpoint and renders it
// as a dismissable banner above the chat area.
// ============================================
(function () {
"use strict";
const ENDPOINT = "https://schiller.pw/AIChatProClientMessage/main.json";
const CACHE_TTL_MS = 60 * 60 * 1000; // 1 hour
const STORAGE_KEY = "announcementCache";
const DISMISSED_KEY = "dismissedAnnouncementId";
const HISTORY_KEY = "announcementHistory";
const HISTORY_MAX = 30;
/**
* Compare two semver-ish version strings ("1.2.3").
* Returns negative if a<b, 0 if equal, positive if a>b.
* Non-numeric / missing parts are treated as 0.
*/
function compareVersions(a, b) {
const pa = String(a || "0").split(".").map((n) => parseInt(n, 10) || 0);
const pb = String(b || "0").split(".").map((n) => parseInt(n, 10) || 0);
const len = Math.max(pa.length, pb.length);
for (let i = 0; i < len; i++) {
const da = pa[i] || 0;
const db = pb[i] || 0;
if (da !== db) return da - db;
}
return 0;
}
/**
* Pick a localized field from either a plain string or an object
* keyed by language code. Falls back to English, then to the first
* available value.
*/
function pickLocalized(field, lang) {
if (field == null) return "";
if (typeof field === "string") return field;
if (typeof field !== "object") return String(field);
if (field[lang]) return field[lang];
if (field.en) return field.en;
const keys = Object.keys(field);
return keys.length ? field[keys[0]] : "";
}
/**
* Decide whether the announcement should be shown for the given
* installed extension version and dismissed-id state.
*/
function shouldShow(announcement, installedVersion, dismissedId) {
if (!announcement || typeof announcement !== "object") return false;
if (!announcement.id) return false;
// Expiry
if (announcement.expiresAt) {
const exp = Date.parse(announcement.expiresAt);
if (!isNaN(exp) && Date.now() > exp) return false;
}
// minVersion: installed must be >= minVersion
if (
announcement.minVersion &&
compareVersions(installedVersion, announcement.minVersion) < 0
) {
return false;
}
// targetVersion: only show if installed < targetVersion (user is outdated)
if (
announcement.targetVersion &&
compareVersions(installedVersion, announcement.targetVersion) >= 0
) {
return false;
}
// Dismissed: hide only if dismissable AND id matches the dismissed one
if (announcement.dismissable !== false && dismissedId === announcement.id) {
return false;
}
return true;
}
// Use window.Storage (shared/storage.js) which auto-detects
// chrome.storage.local (extension) vs localStorage (web app).
function getStore() {
return window.Storage || null;
}
/**
* Fetch the announcement JSON, using a TTL-based cache.
* Returns null on failure (network, JSON parse, etc).
*/
async function fetchAnnouncement(force) {
const store = getStore();
if (!store) return null;
try {
const stored = await store.get(STORAGE_KEY);
const cached = stored[STORAGE_KEY];
if (
!force &&
cached &&
cached.fetchedAt &&
Date.now() - cached.fetchedAt < CACHE_TTL_MS
) {
return cached.data;
}
const resp = await fetch(ENDPOINT, { cache: "no-store" });
if (!resp.ok) {
return cached ? cached.data : null;
}
const data = await resp.json();
await store.set({ [STORAGE_KEY]: { data, fetchedAt: Date.now() } });
if (data && data.id) {
await appendHistory(data);
}
return data;
} catch (err) {
try {
const store2 = getStore();
if (!store2) return null;
const stored = await store2.get(STORAGE_KEY);
return stored[STORAGE_KEY] ? stored[STORAGE_KEY].data : null;
} catch (_) {
return null;
}
}
}
async function getDismissedId() {
const store = getStore();
if (!store) return null;
const stored = await store.get(DISMISSED_KEY);
return stored[DISMISSED_KEY] || null;
}
async function markDismissed(id) {
const store = getStore();
if (!id || !store) return;
await store.set({ [DISMISSED_KEY]: id });
}
async function getHistory() {
const store = getStore();
if (!store) return [];
const stored = await store.get(HISTORY_KEY);
const list = stored[HISTORY_KEY];
return Array.isArray(list) ? list : [];
}
async function appendHistory(announcement) {
const store = getStore();
if (!announcement || !announcement.id || !store) return;
try {
const history = await getHistory();
const filtered = history.filter((entry) => entry.id !== announcement.id);
filtered.unshift({
id: announcement.id,
type: announcement.type || "info",
title: announcement.title || null,
body: announcement.body || null,
link: announcement.link || null,
linkLabel: announcement.linkLabel || null,
receivedAt: Date.now(),
});
await store.set({ [HISTORY_KEY]: filtered.slice(0, HISTORY_MAX) });
} catch (_) {
// History is best-effort; never break the fetch path.
}
}
async function clearHistory() {
const store = getStore();
if (store) await store.remove(HISTORY_KEY);
}
/**
* Build the banner DOM and inject it into `container`. Returns the element
* or null if nothing should be shown.
*/
function renderBanner(container, announcement, lang, onDismiss) {
if (!container) return null;
container.innerHTML = "";
container.classList.add("hidden");
if (!announcement) return null;
const type = ["info", "update", "warning"].includes(announcement.type)
? announcement.type
: "info";
const title = pickLocalized(announcement.title, lang);
const body = pickLocalized(announcement.body, lang);
const linkLabel = pickLocalized(announcement.linkLabel, lang);
const banner = document.createElement("div");
banner.className = `announcement-banner-inner announcement-${type}`;
const iconSpan = document.createElement("span");
iconSpan.className = "announcement-icon";
iconSpan.textContent =
type === "warning" ? "⚠️" : type === "update" ? "⬆️" : "ℹ️";
banner.appendChild(iconSpan);
const textWrap = document.createElement("div");
textWrap.className = "announcement-text";
if (title) {
const t = document.createElement("div");
t.className = "announcement-title";
t.textContent = title;
textWrap.appendChild(t);
}
if (body) {
const b = document.createElement("div");
b.className = "announcement-body";
b.textContent = body;
textWrap.appendChild(b);
}
if (announcement.link) {
const a = document.createElement("a");
a.className = "announcement-link";
a.href = announcement.link;
a.target = "_blank";
a.rel = "noopener noreferrer";
a.textContent = linkLabel || announcement.link;
textWrap.appendChild(a);
}
banner.appendChild(textWrap);
if (announcement.dismissable !== false) {
const closeBtn = document.createElement("button");
closeBtn.className = "announcement-close";
closeBtn.type = "button";
const dismissLabel =
window.i18n && window.i18n.t
? window.i18n.t("announcement.dismiss")
: "Dismiss";
closeBtn.setAttribute("aria-label", dismissLabel);
closeBtn.title = dismissLabel;
closeBtn.textContent = "×";
closeBtn.addEventListener("click", async () => {
await markDismissed(announcement.id);
container.classList.add("hidden");
container.innerHTML = "";
if (typeof onDismiss === "function") onDismiss();
});
banner.appendChild(closeBtn);
}
container.appendChild(banner);
container.classList.remove("hidden");
return banner;
}
/**
* One-shot init: fetch (or use cache), filter against current version + dismissed,
* and render into the given container element.
*/
async function initAnnouncement(container) {
if (!container) return;
try {
const manifest = chrome.runtime.getManifest();
const installedVersion = manifest.version;
const lang =
(window.i18n && window.i18n.i18nGetLang && window.i18n.i18nGetLang()) ||
"en";
const [announcement, dismissedId] = await Promise.all([
fetchAnnouncement(false),
getDismissedId(),
]);
if (shouldShow(announcement, installedVersion, dismissedId)) {
renderBanner(container, announcement, lang);
}
} catch (err) {
// Silent fail — the banner is purely informational and must never block the chat
console.debug("[announcement] init failed:", err);
}
}
window.announcement = {
init: initAnnouncement,
fetch: fetchAnnouncement,
shouldShow,
pickLocalized,
compareVersions,
getHistory,
clearHistory,
ENDPOINT,
};
})();