-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
563 lines (477 loc) · 16.9 KB
/
Copy pathbackground.js
File metadata and controls
563 lines (477 loc) · 16.9 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Background script for TRMNL New Tab Display extension
chrome.storage.onChanged.addListener((changes, namespace) => {
if (changes.environment) {
console.log("Environment changed:", {
oldValue: changes.environment.oldValue,
newValue: changes.environment.newValue,
});
}
});
let API_URL = `${HOSTS.production}/api/current_screen`; // Default to production
const DEFAULT_REFRESH_RATE = 30; // seconds
const LOGIN_PROMPT_STATE_KEY = "loginPromptState";
const LOGIN_PROMPT_RETRY_MS = 15 * 60 * 1000;
async function clearLoginPromptState() {
await chrome.storage.local.remove(LOGIN_PROMPT_STATE_KEY);
}
async function promptLoginTab(reason) {
const now = Date.now();
const { [LOGIN_PROMPT_STATE_KEY]: loginPromptState } =
await chrome.storage.local.get(LOGIN_PROMPT_STATE_KEY);
if (
loginPromptState?.pending &&
now - loginPromptState.lastOpenedAt < LOGIN_PROMPT_RETRY_MS
) {
console.log(
`Skipping login tab open (${reason}) - already prompted ${Math.floor((now - loginPromptState.lastOpenedAt) / 1000)}s ago`,
);
return false;
}
await chrome.storage.local.set({
[LOGIN_PROMPT_STATE_KEY]: {
pending: true,
lastOpenedAt: now,
reason,
},
});
const loginUrl = await getLoginUrl();
chrome.tabs.create({ url: loginUrl });
return true;
}
async function fetchAndStoreDevices() {
try {
// First check if we already have stored devices
const { devices: storedDevices } =
await chrome.storage.local.get("devices");
const devicesUrl = await getDevicesUrl();
const response = await fetch(devicesUrl);
// Only redirect to login if we have no stored devices AND got a 401/403
if (
(response.status === 401 || response.status === 403) &&
(!storedDevices || storedDevices.length === 0)
) {
await promptLoginTab("fetchAndStoreDevices unauthorized");
return storedDevices || null;
}
if (!response.ok) {
// If we have stored devices, return them instead of failing
if (storedDevices && storedDevices.length > 0) {
console.log("Using stored devices due to fetch error");
return storedDevices;
}
throw new Error(`HTTP error: ${response.status}`);
}
const devices = await response.json();
await clearLoginPromptState();
// Always save the newly fetched devices
await chrome.storage.local.set({ devices });
// If no device is selected, select the first one
const { selectedDevice } = await chrome.storage.local.get("selectedDevice");
if (!selectedDevice && devices.length > 0) {
await chrome.storage.local.set({ selectedDevice: devices[0] });
}
return devices;
} catch (error) {
console.error("Error fetching devices:", error);
// Return stored devices if available
const { devices: storedDevices } =
await chrome.storage.local.get("devices");
return storedDevices || null;
}
}
// Fetch devices list and get first API key
async function getFirstDeviceApiKey() {
try {
// First check if we already have stored devices
const { devices: storedDevices } =
await chrome.storage.local.get("devices");
const devicesUrl = await getDevicesUrl();
const response = await fetch(devicesUrl);
// Only redirect to login if we have no stored devices AND got a 401/403
if (
(response.status === 401 || response.status === 403) &&
(!storedDevices || storedDevices.length === 0)
) {
await promptLoginTab("getFirstDeviceApiKey unauthorized");
return null;
}
if (!response.ok) {
// If we have stored devices, use them instead of failing
if (storedDevices && storedDevices.length > 0) {
console.log("Using stored devices due to fetch error");
await chrome.storage.local.set({
selectedDevice: storedDevices[0],
environment: "production",
});
return storedDevices[0].api_key;
}
throw new Error(`HTTP error: ${response.status}`);
}
const devices = await response.json();
if (devices && devices.length > 0) {
await clearLoginPromptState();
// Store the devices
await chrome.storage.local.set({
devices,
selectedDevice: devices[0],
environment: "production",
});
return devices[0].api_key;
}
// If no devices found in response but we have stored devices, use those
if (storedDevices && storedDevices.length > 0) {
return storedDevices[0].api_key;
}
return null;
} catch (error) {
console.error("Error fetching devices:", error);
// Check for stored devices
const { devices: storedDevices } =
await chrome.storage.local.get("devices");
if (storedDevices && storedDevices.length > 0) {
return storedDevices[0].api_key;
}
return null;
}
}
// Send the current image to the requester
async function sendCurrentImage(sendResponse) {
const storage = await chrome.storage.local.get([
"currentImage",
"lastFetch",
"refreshRate",
]);
sendResponse(storage);
}
// Initialize when extension is installed or updated
chrome.runtime.onInstalled.addListener(async () => {
console.log("TRMNL New Tab Display extension installed");
// Set default environment and get devices
await chrome.storage.local.set({
environment: "production",
[LOGIN_PROMPT_STATE_KEY]: {
pending: false,
lastOpenedAt: 0,
reason: "install",
},
});
API_URL = await getApiUrl();
const devices = await fetchAndStoreDevices();
if (devices && devices.length > 0) {
// Set the first device and its API key
await chrome.storage.local.set({
selectedDevice: devices[0],
lastFetch: 0,
nextFetch: 0,
refreshRate: DEFAULT_REFRESH_RATE,
retryCount: 0,
retryAfter: null,
});
// Attempt to fetch an image with the device's API key
fetchTrmnlImage();
}
// Set up alarm for periodic image fetching
setupRefreshAlarm(DEFAULT_REFRESH_RATE);
});
// Listen for alarms to trigger image refresh
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "refreshTrmnlImage") {
fetchTrmnlImage();
} else if (alarm.name === "retryTrmnlImage") {
console.log("Retry alarm triggered");
fetchTrmnlImage();
}
});
// Listen for messages from popup or new tab page
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "saveApiKey") {
saveApiKey(request.apiKey)
.then(() => {
if (sendResponse) sendResponse({ success: true });
})
.catch((err) => {
console.error("Error saving API key:", err);
if (sendResponse) sendResponse({ success: false, error: err.message });
});
return true; // Required for async response
} else if (request.action === "getCurrentImage") {
sendCurrentImage(sendResponse);
return true; // Required for async response
} else if (request.action === "forceRefresh") {
fetchTrmnlImage(true) // Pass true to force a refresh regardless of cache
.then((result) => {
if (sendResponse) sendResponse({ success: !!result });
})
.catch((err) => {
console.error("Error during forced refresh:", err);
if (sendResponse) sendResponse({ success: false, error: err.message });
});
return true; // Important for async response
}
if (request.action === "refreshDevices") {
fetchAndStoreDevices()
.then((devices) => {
if (devices && devices.length > 0) {
// Refresh the current image with the new device info
return fetchTrmnlImage(true);
}
})
.then(() => {
if (sendResponse) sendResponse({ success: true });
})
.catch((error) => {
console.error("Error refreshing devices:", error);
if (sendResponse)
sendResponse({ success: false, error: error.message });
});
return true;
}
return false; // Not handling this message
});
// Save API key and immediately fetch an image
async function saveApiKey(apiKey) {
await chrome.storage.local.set({
apiKey,
// Reset any retry information when changing API key
retryCount: 0,
retryAfter: null,
});
console.log("API key saved");
// Fetch a new image with the updated API key
return fetchTrmnlImage();
}
// Set up the refresh alarm
function setupRefreshAlarm(seconds) {
// Clear any existing alarm
chrome.alarms.clear("refreshTrmnlImage");
// Create new alarm
chrome.alarms.create("refreshTrmnlImage", {
periodInMinutes: seconds / 60, // Convert seconds to minutes
});
console.log(`Refresh alarm set for every ${seconds} seconds`);
}
// Handle storage limits for data URLs
// Chrome storage has limits, so we need to be careful with large data URLs
async function checkStorageUsage() {
// Firefox does not support this method
if (chrome?.storage?.local?.getBytesInUse) {
// Get current storage usage
const storageUsage = await chrome.storage.local.getBytesInUse(null);
const storageLimit = chrome.storage.local.QUOTA_BYTES || 10485760; // 10MB default
const percentUsed = (storageUsage / storageLimit) * 100;
console.log(
`Storage usage: ${(storageUsage / 1024 / 1024).toFixed(2)}MB / ${(storageLimit / 1024 / 1024).toFixed(2)}MB (${percentUsed.toFixed(2)}%)`,
);
// If we're using more than 80% of our quota, we might want to clean up old images
return percentUsed > 80;
}
return false; // Assume no storage limits if method is not available
}
let fetchInProgress = false;
// Fetch an image from the TRMNL API
async function fetchTrmnlImage(forceRefresh = false) {
console.log("Fetching TRMNL image");
// Prevent concurrent fetches
if (fetchInProgress) {
console.log("Fetch already in progress, skipping");
return null;
}
fetchInProgress = true;
// Get the current API URL and initialize environment if needed
const environment = await chrome.storage.local.get("environment");
if (!environment.environment) {
await chrome.storage.local.set({ environment: "production" });
}
API_URL = await getApiUrl();
console.log("Fetching TRMNL image");
const storage = await chrome.storage.local.get([
"selectedDevice",
"lastFetch",
"refreshRate",
"nextFetch",
"retryCount",
"retryAfter",
"currentImage",
]);
const apiKey = storage.selectedDevice?.api_key;
const currentTime = Date.now();
// If no API key, try to get one from devices first
if (!apiKey) {
console.log("No API key set, attempting to fetch from devices");
const deviceApiKey = await getFirstDeviceApiKey();
if (deviceApiKey) {
await saveApiKey(deviceApiKey);
return fetchTrmnlImage(true);
}
console.log("Could not get API key from devices, skipping fetch");
return null;
}
// Check if we're in a retry backoff period
if (storage.retryAfter && currentTime < storage.retryAfter) {
console.log(
`In retry backoff period. Next attempt in ${Math.ceil((storage.retryAfter - currentTime) / 1000)}s`,
);
return null;
}
try {
const response = await fetch(API_URL, {
headers: {
"access-token": apiKey,
"Cache-Control": "no-cache", // Prevent browser caching
},
});
// Handle unauthorized (redirect to login only if we have no devices)
if (response.status === 401 || response.status === 403) {
const { devices } = await chrome.storage.local.get("devices");
if (!devices || devices.length === 0) {
await promptLoginTab("fetchTrmnlImage unauthorized");
fetchInProgress = false;
return null;
} else {
console.log(
"API key unauthorized, but have stored devices. Will retry with another key.",
);
// Could implement logic here to try the next device
}
}
// Handle rate limiting (429)
if (response.status === 429) {
await clearLoginPromptState();
console.warn("Rate limited by the API (429)");
// Get retry delay from header or use exponential backoff
let retryAfter = 0;
if (response.headers.has("Retry-After")) {
// Server specified retry time in seconds
retryAfter = parseInt(response.headers.get("Retry-After")) * 1000;
} else {
// Calculate exponential backoff
const retryCount = (storage.retryCount || 0) + 1;
// Base delay of 60 seconds with exponential increase and some randomness
retryAfter = Math.min(
60000 * Math.pow(1.5, retryCount - 1) + Math.random() * 10000,
3600000,
);
await chrome.storage.local.set({ retryCount: retryCount });
}
console.log(`Setting retry after ${retryAfter / 1000} seconds`);
const retryTime = currentTime + retryAfter;
await chrome.storage.local.set({ retryAfter: retryTime });
// Schedule a retry
chrome.alarms.create("retryTrmnlImage", {
when: retryTime,
});
fetchInProgress = false;
return null;
}
// Reset retry count on successful requests
if (storage.retryCount > 0) {
await chrome.storage.local.set({ retryCount: 0, retryAfter: null });
}
if (!response.ok) {
throw new Error(
`HTTP error: ${response.status} - ${response.statusText}`,
);
}
await clearLoginPromptState();
const data = await response.json();
console.log("TRMNL API response:", data);
// Use fixed 30 second refresh rate
const refreshRate = DEFAULT_REFRESH_RATE;
console.log(`Using fixed refresh rate: ${refreshRate} seconds`);
// Check if the image URL has changed
const currentImageData = storage.currentImage || {};
if (!forceRefresh && currentImageData.originalUrl === data.image_url) {
console.log("Image has not changed, updating refresh time only");
// Update just the next fetch time without downloading the image again
const nextFetch = currentTime + refreshRate * 1000;
await chrome.storage.local.set({
refreshRate: refreshRate,
nextFetch: nextFetch,
lastFetch: currentTime,
// Reset any retry information
retryCount: 0,
retryAfter: null,
});
// Update the refresh alarm if needed
if (refreshRate !== storage.refreshRate) {
setupRefreshAlarm(refreshRate);
}
fetchInProgress = false;
return currentImageData.url;
}
// Get the image as a blob
const imageResponse = await fetch(data.image_url, {
headers: {
"Cache-Control": "no-cache",
},
});
if (!imageResponse.ok) {
throw new Error(`Failed to fetch image: ${imageResponse.status}`);
}
// Instead of using Blob URLs (which aren't supported in service workers),
// convert the image to a base64 data URL or store the raw data
const imageBlob = await imageResponse.blob();
// Create a FileReader to convert the blob to a data URL
const imageDataUrl = await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(imageBlob);
});
// Store the new image and update timestamps
const nextFetch = currentTime + refreshRate * 1000;
// Check storage usage before storing potentially large data URL
const isStorageLimited = await checkStorageUsage();
// If storage is limited, we might want to compress the image or handle differently
// For now, we'll just log a warning and continue
if (isStorageLimited) {
console.warn(
"Storage usage is high - consider implementing image compression",
);
}
await chrome.storage.local.set({
currentImage: {
url: imageDataUrl,
originalUrl: data.image_url,
filename: data.filename || "display.jpg",
timestamp: currentTime,
},
lastFetch: currentTime,
refreshRate: refreshRate,
nextFetch: nextFetch,
});
// Update the refresh alarm if needed
if (refreshRate !== storage.refreshRate) {
setupRefreshAlarm(refreshRate);
}
// Notify any open tabs that a new image is available
try {
chrome.runtime.sendMessage({ action: "imageUpdated" }).catch(() => {
// This is normal if no listeners are active
console.log("No active listeners for imageUpdated message");
});
} catch (e) {
// Ignore message sending errors, which are expected if no tabs are open
}
setTimeout(() => {
fetchInProgress = false;
}, 1000);
return imageDataUrl;
} catch (error) {
console.error("Error fetching TRMNL image:", error);
// Schedule a retry with backoff
const retryCount = (storage.retryCount || 0) + 1;
const retryDelay = Math.min(60000 * Math.pow(2, retryCount - 1), 3600000); // Max 1 hour
const retryTime = currentTime + retryDelay;
await chrome.storage.local.set({
retryCount: retryCount,
retryAfter: retryTime,
});
chrome.alarms.create("retryTrmnlImage", {
when: retryTime,
});
console.log(`Scheduled retry in ${retryDelay / 1000} seconds`);
fetchInProgress = false;
return null;
}
}