-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateCheck.js
129 lines (115 loc) · 3.77 KB
/
updateCheck.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
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
let updateScript = function(scriptData) {
let name = scriptData.scriptName.text
let url = scriptData.data.url
let icon = scriptData.data.icon
let installUrl = `jsbox://install?url=${encodeURI(url)}&name=${encodeURI(name)}&icon=${encodeURI(icon)}`
$app.openURL(installUrl)
}
let renderUI = function (UIData) {
$ui.render({
props: {
title: "脚本更新"
},
views: [{
type: "list",
props: {
id: "mainList",
data: UIData,
template: {
views: [{
type: 'label',
props: {
id: 'scriptName',
},
layout: (make, view) => {
make.height.equalTo(view.super)
make.left.equalTo(view.super).offset(10)
}
}, {
type: 'label',
props: {
id: 'scriptCanUpdate'
},
layout: (make, view) => {
make.height.equalTo(view.super)
make.right.equalTo(view.super).offset(-10)
}
}]
}
},
layout: $layout.fill,
events: {
didSelect: (sender, indexPath, data) => {
console.log(data.scriptName.text)
if (data.newData) {
updateScript(data)
}
// let scriptUrl = data.data.url
// data.scriptCanUpdate = ''
// $("mainList").delete(indexPath)
// $("mainList").insert({
// indexPath: indexPath,
// value: data
// })
}
}
}]
})
}
$app.tips("更新完后重新运行即可检查是否成功")
renderUI([])
let scripts = $addin.list.filter(item => item.url)
let presentData = []
let checkScript = function (scriptInfo) {
return new Promise((resolve, reject) => {
$http.get({
url: scriptInfo.data.url,
handler: function (resp) {
let response = resp.response
let scriptData = resp.data
if (scriptInfo.data.data.string && scriptInfo.data.data.string != scriptData) {
console.log()
scriptInfo.scriptCanUpdate = { text: '可更新' }
scriptInfo.newData = scriptData
}
resolve(scriptInfo)
// if (response.statusCode == 200) {
//
// // $console.info(scriptInfo.data)
// resolve(scriptInfo)
// } else {
// reject()
// }
}
})
})
}
for (let k in scripts) {
presentData.push({
scriptName: { text: scripts[k].name },
scriptCanUpdate: { text: '' },
data: scripts[k]
})
}
let updatedData = []
let promiseData = presentData.map(item => checkScript(item))
$ui.loading(true)
for (let i = 0, p = Promise.resolve(); i < promiseData.length; i++) {
p = p.then(d => {
updatedData.push(d)
return promiseData[i]
})
if (i == promiseData.length - 1) {
p.then((d) => {
setTimeout(() => {
updatedData.push(d)
$ui.loading(false)
$("mainList").data = updatedData
}, 1);
}).catch(() => {
setTimeout(() => {
$ui.loading(false)
}, 1);
})
}
}