-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathSurgeModuleTool.js
337 lines (317 loc) · 10.1 KB
/
SurgeModuleTool.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: cloud-download-alt;
// prettier-ignore
let ToolVersion = "2.04";
//用于自定义发送请求的请求头
const reqHeaders = {
headers: {
'User-Agent': 'script-hub/1.0.0',
},
}
async function delay(milliseconds) {
var before = Date.now()
while (Date.now() < before + milliseconds) {}
return true
}
function convertToValidFileName(str) {
// 替换非法字符为下划线
const invalidCharsRegex = /[\/:*?"<>|]/g
const validFileName = str.replace(invalidCharsRegex, '_')
// 删除多余的点号
const multipleDotsRegex = /\.{2,}/g
const fileNameWithoutMultipleDots = validFileName.replace(multipleDotsRegex, '.')
// 删除文件名开头和结尾的点号和空格
const leadingTrailingDotsSpacesRegex = /^[\s.]+|[\s.]+$/g
const finalFileName = fileNameWithoutMultipleDots.replace(leadingTrailingDotsSpacesRegex, '')
return finalFileName
}
function addLineAfterLastOccurrence(text, addition) {
const regex = /^#!.+?$/gm
const matchArray = text.match(regex)
const lastIndex = matchArray ? matchArray.length - 1 : -1
if (lastIndex >= 0) {
const lastMatch = matchArray[lastIndex]
const insertIndex = text.indexOf(lastMatch) + lastMatch.length
const newText = text.slice(0, insertIndex) + addition + text.slice(insertIndex)
return newText
}
return text
}
let idx
let fromUrlScheme
let checkUpdate
// if (args.queryParameters.url && args.queryParameters.name) {
if (args.queryParameters.url) {
fromUrlScheme = true
}
if (fromUrlScheme) {
idx = 1
} else {
let alert = new Alert()
alert.title = 'Surge 模块工具'
//alert.addDestructiveAction("更新文件夹内全部文件")
alert.addDestructiveAction('更新本脚本')
alert.addAction('从链接创建')
alert.addAction('更新单个模块')
alert.addAction('更新全部模块')
alert.addCancelAction('取消')
idx = await alert.presentAlert()
}
let folderPath
let files = []
let contents = []
const fm = FileManager.iCloud()
if (idx == 3) {
folderPath = await DocumentPicker.openFolder()
files = fm.listContents(folderPath)
} else if (idx == 2) {
const filePath = await DocumentPicker.openFile()
folderPath = filePath.substring(0, filePath.lastIndexOf('/'))
files = [filePath.substring(filePath.lastIndexOf('/') + 1)]
} else if (idx == 1) {
let url
let name
if (fromUrlScheme) {
url = args.queryParameters.url
name = args.queryParameters.name
} else {
alert = new Alert()
alert.title = '将自动添加后缀 .sgmodule'
alert.addTextField('链接(必填)', '')
alert.addTextField('名称(选填)', '')
alert.addAction('下载')
alert.addCancelAction('取消')
await alert.presentAlert()
url = alert.textFieldValue(0)
name = alert.textFieldValue(1)
}
if (url) {
if (!name) {
const plainUrl = url.split('?')[0]
const fullname = plainUrl.substring(plainUrl.lastIndexOf('/') + 1)
if (fullname) {
name = fullname.replace(/\.sgmodule$/, '')
}
if (!name) {
name = `untitled-${new Date().toLocaleString()}`
}
}
name = convertToValidFileName(name)
files = [`${name}.sgmodule`]
contents = [`#SUBSCRIBED ${url}`]
}
} else if (idx == 0) {
console.log('检查更新')
checkUpdate = true
await update()
}
let report = {
success: 0,
fail: [],
noUrl: 0,
}
for await (const [index, file] of files.entries()) {
if (file && !/\.(conf|txt|js|list)$/i.test(file)) {
// console.log(file);
let originalName
let originalDesc
let noUrl
try {
let content
let filePath
if (contents.length > 0) {
content = contents[index]
} else {
filePath = `${folderPath}/${file}`
content = fm.readString(filePath)
}
const originalNameMatched = `${content}`.match(/^#\!name\s*?=\s*(.*?)\s*(\n|$)/im)
if (originalNameMatched) {
originalName = originalNameMatched[1]
}
const originalDescMatched = `${content}`.match(/^#\!desc\s*?=\s*(.*?)\s*(\n|$)/im)
if (originalDescMatched) {
originalDesc = originalDescMatched[1]
if (originalDesc) {
originalDesc = originalDesc.replace(/^🔗.*?]\s*/i, '')
}
}
const matched = `${content}`.match(/^#SUBSCRIBED\s+(.*?)\s*(\n|$)/im)
if (!matched) {
noUrl = true
throw new Error('无订阅链接')
}
const subscribed = matched[0]
const url = matched[1]
if (!url) {
noUrl = true
throw new Error('无订阅链接')
}
const req = new Request(url)
req.timeoutInterval = 10
req.method = 'GET'
req.headers = reqHeaders.headers
let res = await req.loadString()
const statusCode = req.response.statusCode
if (statusCode < 200 || statusCode >= 400) {
throw new Error(`statusCode: ${statusCode}`)
}
if (!res) {
throw new Error(`未获取到模块内容`)
}
const nameMatched = `${res}`.match(/^#\!name\s*?=\s*?\s*(.*?)\s*(\n|$)/im)
if (!nameMatched) {
throw new Error(`不是合法的模块内容`)
}
const name = nameMatched[1]
if (!name) {
throw new Error('模块无名称字段')
}
const descMatched = `${res}`.match(/^#\!desc\s*?=\s*?\s*(.*?)\s*(\n|$)/im)
let desc
if (descMatched) {
desc = descMatched[1]
}
if (!desc) {
res = `#!desc=\n${res}`
}
res = res.replace(/^(#SUBSCRIBED|# 🔗 模块链接)(.*?)(\n|$)/gim, '')
// console.log(res);
res = addLineAfterLastOccurrence(res, `\n\n# 🔗 模块链接\n${subscribed.replace(/\n/g, '')}\n`)
content = `${res}`.replace(/^#\!desc\s*?=\s*/im, `#!desc=🔗 [${new Date().toLocaleString()}] `)
// console.log(content);
if (filePath) {
fm.writeString(filePath, content)
} else {
await DocumentPicker.exportString(content, file)
}
// }
let nameInfo = `${name}`
let descInfo = `${desc}`
if (originalName && name !== originalName) {
nameInfo = `${originalName} -> ${name}`
}
if (originalDesc && desc !== originalDesc) {
descInfo = `${originalDesc} -> ${desc}`
}
console.log(`\n✅ ${nameInfo}\n${descInfo}\n${file}`)
report.success += 1
await delay(1 * 1000)
if (fromUrlScheme) {
alert = new Alert()
alert.title = `✅ ${nameInfo}`
alert.message = `${descInfo}\n${file}`
alert.addDestructiveAction('重载 Surge')
alert.addAction('打开 Surge')
alert.addCancelAction('关闭')
idx = await alert.presentAlert()
if (idx == 0) {
const req = new Request('http://script.hub/reload')
req.timeoutInterval = 10
req.method = 'GET'
let res = await req.loadString()
} else if (idx == 1) {
Safari.open('surge://')
}
}
} catch (e) {
if (noUrl) {
report.noUrl += 1
} else {
report.fail.push(originalName || file)
}
if (noUrl) {
console.log(`\n🈚️ ${originalName || ''}\n${file}`)
console.log(e)
} else {
console.log(`\n❌ ${originalName || ''}\n${file}`)
console.error(`${originalName || file}: ${e}`)
}
if (fromUrlScheme) {
alert = new Alert()
alert.title = `❌ ${originalName || ''}\n${file}`
alert.message = `${e.message || e}`
alert.addCancelAction('关闭')
await alert.presentAlert()
}
}
}
}
if (!checkUpdate && !fromUrlScheme) {
alert = new Alert()
let upErrk = report.fail.length > 0 ? `❌ 更新失败: ${report.fail.length}` : '',
noUrlErrk = report.noUrl > 0 ? `🈚️ 无链接: ${report.noUrl}` : ''
alert.title = `📦 模块总数: ${report.success + report.fail.length + report.noUrl}`
alert.message = `${noUrlErrk}\n✅ 更新成功: ${report.success}\n${upErrk}${
report.fail.length > 0 ? `\n${report.fail.join(', ')}` : ''
}`
alert.addDestructiveAction('重载 Surge')
alert.addAction('打开 Surge')
alert.addCancelAction('关闭')
idx = await alert.presentAlert()
if (idx == 0) {
const req = new Request('http://script.hub/reload')
req.timeoutInterval = 10
req.method = 'GET'
let res = await req.loadString()
} else if (idx == 1) {
Safari.open('surge://')
}
}
// @key Think @wuhu.
async function update() {
const fm = FileManager.iCloud()
const dict = fm.documentsDirectory()
// const scriptName = Script.name()
const scriptName = 'SurgeModuleTool'
let version
let resp
try {
const url = 'https://raw.githubusercontent.com/Script-Hub-Org/Script-Hub/main/SurgeModuleTool.js?v=' + Date.now()
let req = new Request(url)
req.method = 'GET'
req.headers = {
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
}
resp = await req.loadString()
const regex = /let ToolVersion = "([\d.]+)"/
const match = resp.match(regex)
version = match ? match[1] : ''
} catch (e) {
console.error(e)
}
if (!version) {
let alert = new Alert()
alert.title = 'Surge 模块工具'
alert.message = '无法获取在线版本'
alert.addCancelAction('关闭')
await alert.presentAlert()
return
} else {
let needUpdate = version > ToolVersion
if (!needUpdate) {
let alert = new Alert()
alert.title = 'Surge 模块工具'
alert.message = `当前版本: ${ToolVersion}\n在线版本: ${version}\n无需更新`
alert.addDestructiveAction('强制更新')
alert.addCancelAction('关闭')
idx = await alert.presentAlert()
if (idx === 0) {
needUpdate = true
}
}
if (needUpdate) {
fm.writeString(`${dict}/${scriptName}.js`, resp)
console.log('更新成功: ' + version)
let notification = new Notification()
notification.title = 'Surge 模块工具 更新成功: ' + version
notification.subtitle = '点击通知跳转'
notification.sound = 'default'
notification.openURL = `scriptable:///open/${scriptName}`
notification.addAction('打开脚本', `scriptable:///open/${scriptName}`, false)
await notification.schedule()
}
}
}