-
Notifications
You must be signed in to change notification settings - Fork 5
/
check_update.js
64 lines (55 loc) · 2.64 KB
/
check_update.js
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
import { printLog } from './shared/functions.js';
const github_repo = 'supechicken/ChromeOS-AutoStart',
update_url = `https://raw.githubusercontent.com/${github_repo}/main/update.json`,
release_url = `https://github.com/${github_repo}/releases/latest`,
url_params = new URLSearchParams(location.search),
manifest = chrome.runtime.getManifest(),
statusText = document.getElementById('statusText'),
description = document.getElementById('description'),
btnList = document.getElementById('btnList'),
askBtnList = document.getElementById('askBtnList'),
rejectBtn = document.getElementById('rejectBtn'),
agreeBtn = document.getElementById('agreeBtn'),
closeBtn = document.getElementById('closeBtn'),
latestBtn = document.getElementById('latestBtn');
rejectBtn.onclick = () => chrome.storage.local.set({ autoCheckUpdate: false }, () => window.close());
agreeBtn.onclick = () => chrome.storage.local.set({ autoCheckUpdate: true }, () => window.close());
closeBtn.onclick = () => window.close();
latestBtn.onclick = () => chrome.tabs.create({ url: release_url }, () => window.close());
window.onload = async () => {
// ask for enabling update check instead of check update
if (url_params.get('askUpdateCheck') === '1') {
statusText.innerText = `Auto update check`;
description.innerText = `Check update automatically at startup?`;
askBtnList.style.display = 'initial';
btnList.style.display = 'none';
return;
}
// fetch latest version info
const latest = await fetch(update_url).then(response => response.json()).catch(err => {
statusText.innerText = `Failed to check update`;
description.innerText = `Failed to fetch release infomation from GitHub.`;
if (url_params.get('autoclose') === '1') {
window.close();
} else {
throw new Error(err);
}
});
if (url_params.get('forceAvailableUpdate') != '1' && latest.latest_version === manifest.version) {
statusText.innerText = `No update found`;
description.innerText = `You are using the latest version of ChromeOS AutoStart.`;
// close this page if no update available
if (url_params.get('autoclose') === '1') window.close();
} else {
// update available
// unminimize this window
chrome.windows.getCurrent(currentWindow => chrome.windows.update(currentWindow.id, {
height: 250,
width: 450,
focused: true
}));
statusText.innerText = 'Update available!';
description.innerText = `ChromeOS AutoStart version ${latest.latest_version} is available now.`
printLog(`Update available: version ${latest.latest_version}`);
}
};