forked from sunyymq/JSBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiCloudSync.js
107 lines (97 loc) · 2.18 KB
/
iCloudSync.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
/**
* JSBox脚本迁移工具 V5
* 使用时请保证iCloud账户的一致性
*
* By AbleCats
* https://t.me/Flow_Script
**/
const Path = "drive://ACJSBuckup/"
const SPath = "drive://ACJSBuckup/DEC/"
function saveError(sign) {
$ui.alert({
title: "备份失败",
message: sign ? "抱歉\n检测到JSBox没有iCloud权限\n" : "抱歉\n你的JSBox版本还不支持该本脚本\n",
actions: [{
title: "确定",
handler: function() {
$file.delete(Path);
$app.close();
}
}]
})
}
function saveSuccess() {
$ui.alert({
title: "备份成功",
message: "\n请保持手机网络畅通\n上传速度根据你的网络速度而定",
actions: [{
title: "确定",
handler: function() {
$app.close();
}
}]
})
}
function readSuccess() {
$ui.alert({
title: "恢复成功",
message: "\niCloud脚本恢复到本地成功\n",
actions: [{
title: "确定",
handler: function() {
$app.close();
}
}]
})
}
function saveFile() {
let Names = []
$file.delete(Path)
$file.mkdir(Path)
$file.mkdir(SPath)
$addin.list.map(function(item) {
if (item.data) {
$file.write({
data: item.data,
path: SPath + item.name
})
Names.push(item.name)
return true;
} else {
return false;
}
})
let ALL = $addin.list.length
if ($file.list(SPath).length) {
$file.write({
data: $data({ string: JSON.stringify(Names) }),
path: Path + "Names.conf"
})
let PNL = JSON.parse($file.read(Path + "Names.conf").string).length
if(PNL == ALL) saveSuccess();
else saveError(1);
}
}
function readFile() {
let Names = JSON.parse($file.read(Path + "Names.conf").string)
for (let value of Names) {
$addin.save({
name: value,
data: $file.read(SPath + value)
})
}
readSuccess()
}
async function initiCoudSync() {
if ($file.exists("drive://")) {
if ($file.exists(SPath))
$ui.menu({
items: ["备份", "恢复"],
handler: function(title, idx) {
idx ? readFile() : saveFile();
}
})
else await saveFile();
} else saveError(0);
}
initiCoudSync()