-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
132 lines (122 loc) · 4.84 KB
/
Copy pathbackground.js
File metadata and controls
132 lines (122 loc) · 4.84 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
const GITHUB_RELEASES_LATEST_URL = 'https://api.github.com/repos/Arthas1811/ArthasMod/releases/latest';
const GITHUB_MANIFEST_URL = 'https://raw.githubusercontent.com/Arthas1811/ArthasMod/main/manifest.json';
const CURRENT_VERSION = chrome.runtime.getManifest().version;
function compareSemver(a, b) {
const toParts = (v) => (v || '').split('.').map((n) => Number.parseInt(n, 10) || 0);
const aParts = toParts(a);
const bParts = toParts(b);
const len = Math.max(aParts.length, bParts.length);
for (let i = 0; i < len; i += 1) {
const aVal = aParts[i] ?? 0;
const bVal = bParts[i] ?? 0;
if (aVal > bVal) return 1;
if (aVal < bVal) return -1;
}
return 0;
}
function normalizeVersion(value) {
if (typeof value !== 'string') return null;
const trimmed = value.trim();
if (!trimmed) return null;
return trimmed.replace(/^v/i, '');
}
async function fetchLatestVersion() {
// Prefer the main branch manifest (source of truth for unpacked installs),
// then fall back to the latest GitHub release tag if needed.
let manifestError = null;
try {
const manifestResponse = await fetch(GITHUB_MANIFEST_URL, { cache: 'no-store' });
if (manifestResponse.ok) {
const manifest = await manifestResponse.json();
const versionFromManifest = normalizeVersion(manifest?.version);
if (versionFromManifest) {
return {
version: versionFromManifest,
source: 'manifest-main',
url: GITHUB_MANIFEST_URL
};
}
manifestError = 'Manifest missing version field.';
} else {
manifestError = `GitHub responded ${manifestResponse.status}`;
}
} catch (error) {
manifestError = error?.message || 'Manifest fetch failed.';
}
try {
const releaseResponse = await fetch(GITHUB_RELEASES_LATEST_URL, { cache: 'no-store' });
if (releaseResponse.ok) {
const data = await releaseResponse.json();
const versionFromTag = normalizeVersion(data?.tag_name || data?.name);
if (versionFromTag) {
return {
version: versionFromTag,
source: 'github-release',
url: data?.html_url || null
};
}
}
} catch {
// Ignore; will surface manifest error instead.
}
throw new Error(manifestError || 'Could not determine latest version.');
}
chrome.runtime.onMessage.addListener((r, s, sendResponse) => {
if (r.action === 'SYNC_THEME') {
chrome.cookies.get({ url: 'https://isy-api.ksr.ch', name: 'token' }, (c) => {
if (c) {
console.log(`Isy Sync token: ${c.value}`);
}
});
return;
}
if (r.action === 'CHECK_UPDATE') {
(async () => {
try {
const latest = await fetchLatestVersion();
const latestVersion = latest?.version || null;
const hasUpdate = latestVersion ? compareSemver(latestVersion, CURRENT_VERSION) > 0 : false;
sendResponse({
ok: true,
currentVersion: CURRENT_VERSION,
latestVersion,
hasUpdate,
source: latest?.source || 'unknown',
releaseUrl: latest?.url || null
});
} catch (error) {
sendResponse({ ok: false, error: error?.message || 'Update check failed.' });
}
})();
return true;
}
if (r.action === 'APPLY_UPDATE') {
(async () => {
try {
const latest = await fetchLatestVersion();
const latestVersion = latest?.version || null;
if (!latestVersion || compareSemver(latestVersion, CURRENT_VERSION) <= 0) {
sendResponse({
ok: true,
status: 'no_update',
latestVersion,
currentVersion: CURRENT_VERSION
});
return;
}
chrome.runtime.requestUpdateCheck((status, details) => {
if (status === 'update_available') {
// Chrome will download the update package; reload to apply once ready.
chrome.runtime.reload();
sendResponse({ ok: true, status, details, latestVersion });
} else {
sendResponse({ ok: true, status, details, latestVersion });
}
});
} catch (error) {
sendResponse({ ok: false, error: error?.message || 'Update apply failed.' });
}
})();
return true;
}
});