-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrt-background.js
More file actions
64 lines (58 loc) · 2.23 KB
/
rt-background.js
File metadata and controls
64 lines (58 loc) · 2.23 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
// ReflecTube — Background Service Worker
// Handles: Update badge notification
const USER = "Manz-bot";
const REPO = "ReflecTube-SourceCode";
const BRANCH = "main";
const MANIFEST_URL = `https://raw.githubusercontent.com/${USER}/${REPO}/${BRANCH}/manifest.json`;
// Compare version strings numerically (e.g. "0.8.6" vs "0.9.0")
function isNewerVersion(local, remote) {
const lp = local.split('.').map(Number);
const rp = remote.split('.').map(Number);
for (let i = 0; i < Math.max(lp.length, rp.length); i++) {
const l = lp[i] || 0;
const r = rp[i] || 0;
if (r > l) return true;
if (r < l) return false;
}
return false;
}
async function checkForUpdate() {
try {
const localVersion = chrome.runtime.getManifest().version;
const response = await fetch(MANIFEST_URL, { cache: 'no-cache' });
if (!response.ok) return;
const remoteManifest = await response.json();
const remoteVersion = remoteManifest.version;
if (isNewerVersion(localVersion, remoteVersion)) {
// Update available — show red badge dot
chrome.action.setBadgeText({ text: '!' });
chrome.action.setBadgeBackgroundColor({ color: '#f44336' });
chrome.action.setBadgeTextColor({ color: '#ffffff' });
// Store the flag so popup knows
chrome.storage.local.set({ rt_update_available: true, rt_update_version: remoteVersion });
} else {
chrome.action.setBadgeText({ text: '' });
chrome.storage.local.set({ rt_update_available: false });
}
} catch (e) {
// Network error — silent fail, keep existing badge state
console.log('ReflecTube: Update check failed', e.message);
}
}
// Check on install/update
chrome.runtime.onInstalled.addListener(() => {
checkForUpdate();
});
/*
// Check periodically (every 6 hours)
chrome.alarms.create('rt-update-check', { periodInMinutes: 360 });
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'rt-update-check') {
checkForUpdate();
}
});
*/
// Also check on browser startup
chrome.runtime.onStartup.addListener(() => {
checkForUpdate();
});