-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
84 lines (73 loc) · 3.02 KB
/
Copy pathbackground.js
File metadata and controls
84 lines (73 loc) · 3.02 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
// background.js
console.log('Background script loaded');
chrome.runtime.onInstalled.addListener(() => {
console.log('Extension installed or updated');
checkForUpdates();
chrome.alarms.create("checkForUpdates", { periodInMinutes: 1440 });
});
chrome.alarms.onAlarm.addListener(alarm => {
if (alarm.name === "checkForUpdates") {
console.log('Alarm triggered: checkForUpdates');
checkForUpdates();
}
});
function checkForUpdates() {
const repoUrl = 'https://api.github.com/repos/uncrypt3d/Ylis/releases/latest';
console.log('Checking for updates from:', repoUrl);
chrome.storage.local.get('skipUntil', function(data) {
const currentTime = Date.now();
if (data.skipUntil && currentTime < data.skipUntil) {
console.log('Update check skipped until:', new Date(data.skipUntil).toLocaleString());
return;
}
fetch(repoUrl)
.then(response => {
if (!response.ok) {
console.error('Failed to fetch release information:', response.statusText);
throw new Error('Failed to fetch release information');
}
return response.json();
})
.then(data => {
const latestVersion = data.tag_name.replace(/^v/, '');
const currentVersion = chrome.runtime.getManifest().version;
console.log('Latest version:', latestVersion);
console.log('Current version:', currentVersion);
if (compareVersions(latestVersion, currentVersion)) {
console.log('New version available:', latestVersion);
chrome.storage.local.set({
latestVersion: latestVersion,
releaseUrl: data.html_url
}, () => {
chrome.tabs.create({ url: chrome.runtime.getURL('updater.html') });
});
} else {
console.log('No new version available.');
}
})
.catch(error => {
console.error('Error during update check:', error);
});
});
}
function compareVersions(latest, current) {
const latestParts = latest.split('.');
const currentParts = current.split('.');
for (let i = 0; i < latestParts.length; i++) {
const latestPart = parseInt(latestParts[i] || '0', 10);
const currentPart = parseInt(currentParts[i] || '0', 10);
if (latestPart > currentPart) return true;
if (latestPart < currentPart) return false;
}
return false;
}
chrome.notifications.onButtonClicked.addListener((notifId, btnIdx) => {
if (notifId === 'updateNotification') {
if (btnIdx === 0) {
console.log('Update button clicked. Opening release page...');
chrome.tabs.create({ url: 'https://github.com/uncrypt3d/Ylis/releases/latest' });
} else {
console.log('Later button clicked. No action taken.');
}
}
});