| 279 | |
| 280 | // @key Think @wuhu. |
| 281 | async function update() { |
| 282 | const fm = FileManager.iCloud() |
| 283 | const dict = fm.documentsDirectory() |
| 284 | // const scriptName = Script.name() |
| 285 | const scriptName = 'SurgeModuleTool' |
| 286 | let version |
| 287 | let resp |
| 288 | try { |
| 289 | const url = 'https://raw.githubusercontent.com/Script-Hub-Org/Script-Hub/main/SurgeModuleTool.js?v=' + Date.now() |
| 290 | let req = new Request(url) |
| 291 | req.method = 'GET' |
| 292 | req.headers = { |
| 293 | 'Cache-Control': 'no-cache', |
| 294 | Pragma: 'no-cache', |
| 295 | } |
| 296 | resp = await req.loadString() |
| 297 | |
| 298 | const regex = /let ToolVersion = "([\d.]+)"/ |
| 299 | const match = resp.match(regex) |
| 300 | version = match ? match[1] : '' |
| 301 | } catch (e) { |
| 302 | console.error(e) |
| 303 | } |
| 304 | |
| 305 | if (!version) { |
| 306 | let alert = new Alert() |
| 307 | alert.title = 'Surge 模块工具' |
| 308 | alert.message = '无法获取在线版本' |
| 309 | alert.addCancelAction('关闭') |
| 310 | await alert.presentAlert() |
| 311 | return |
| 312 | } else { |
| 313 | let needUpdate = version > ToolVersion |
| 314 | if (!needUpdate) { |
| 315 | let alert = new Alert() |
| 316 | alert.title = 'Surge 模块工具' |
| 317 | alert.message = `当前版本: ${ToolVersion}\n在线版本: ${version}\n无需更新` |
| 318 | alert.addDestructiveAction('强制更新') |
| 319 | alert.addCancelAction('关闭') |
| 320 | idx = await alert.presentAlert() |
| 321 | if (idx === 0) { |
| 322 | needUpdate = true |
| 323 | } |
| 324 | } |
| 325 | if (needUpdate) { |
| 326 | fm.writeString(`${dict}/${scriptName}.js`, resp) |
| 327 | console.log('更新成功: ' + version) |
| 328 | let notification = new Notification() |
| 329 | notification.title = 'Surge 模块工具 更新成功: ' + version |
| 330 | notification.subtitle = '点击通知跳转' |
| 331 | notification.sound = 'default' |
| 332 | notification.openURL = `scriptable:///open/${scriptName}` |
| 333 | notification.addAction('打开脚本', `scriptable:///open/${scriptName}`, false) |
| 334 | await notification.schedule() |
| 335 | } |
| 336 | } |
| 337 | } |