forked from xzhhbx/jsbox_script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.js
More file actions
52 lines (44 loc) · 1.32 KB
/
Copy pathupdate.js
File metadata and controls
52 lines (44 loc) · 1.32 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
const urlUtil = require('scripts/utils/url')
const promise = require('scripts/utils/promise')
const GITHUB_RAW_PREFIX = 'https://raw.githubusercontent.com/Fndroid/jsbox_script/master/QuantumultTools'
function currentVersion() {
return $file.read('version.fndroid').string
}
async function latestVersion() {
let resp = await $http.get(urlUtil.withTS(`${GITHUB_RAW_PREFIX}/version.fndroid`))
return resp.data
}
async function latestBox() {
let resp = await $http.download({
url: urlUtil.withTS(`${GITHUB_RAW_PREFIX}/.output/QuantumultTools.box`),
showsProgress: false
})
return resp.data
}
async function updateHandler() {
let lv = versionWeight(await latestVersion())
let cv = versionWeight(currentVersion())
if (lv > cv) {
let boxData = await latestBox()
$addin.save({
name: $addin.current.name,
data: boxData,
handler: success => {
if (success) {
$ui.toast('新版本已成功安装,下次启动生效')
}
}
})
}
}
function versionWeight(versionStr) {
let pow = 1
return versionStr.split(/\./g).reverse().reduce((w, c) => {
let s = w + c * pow
pow *= 100
return s
}, 0)
}
module.exports = {
updateHandler: updateHandler
}