-
Notifications
You must be signed in to change notification settings - Fork 139
/
widget.Install.js
176 lines (165 loc) · 5.2 KB
/
widget.Install.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: brown; icon-glyph: download;
// version:'1.0.0';
const Files = FileManager.iCloud();
const RootPath = Files.documentsDirectory();
const saveFileName = (fileName) => {
const hasSuffix = fileName.lastIndexOf(".") + 1;
return !hasSuffix ? `${fileName}.js` : fileName;
};
const write = (fileName, content) => {
let file = saveFileName(fileName);
const filePath = Files.joinPath(RootPath, file);
Files.writeString(filePath, content);
return true;
};
const saveFile = async ({ moduleName, url }) => {
const req = new Request(encodeURI(url));
const content = await req.loadString();
write(`${moduleName}`, content);
return true;
};
const notify = async (title, body, url, opts = {}) => {
let n = new Notification();
n = Object.assign(n, opts);
n.title = title;
n.body = body;
if (url) n.openURL = url;
return await n.schedule();
};
const renderTableList = async (data) => {
try {
const table = new UITable();
// 如果是节点,则先远程获取
const req = new Request(data.subscription);
const subscription = await req.loadJSON();
const apps = subscription.apps;
apps.forEach((item) => {
const r = new UITableRow();
r.height = 75;
const imgCell = UITableCell.imageAtURL(item.thumb);
imgCell.centerAligned();
r.addCell(imgCell);
const nameCell = UITableCell.text(item.title);
nameCell.centerAligned();
r.addCell(nameCell);
const downloadCell = UITableCell.button("下载");
downloadCell.centerAligned();
downloadCell.dismissOnTap = true;
downloadCell.onTap = async () => {
if (item.depend) {
try {
for (let i = 0; i < item.depend.length; i++) {
const relyItem = item.depend[i];
const _isWrite = await saveFile({
moduleName: relyItem.name,
url: relyItem.scriptURL,
});
if (_isWrite) {
notify("下载提示", `依赖插件:${relyItem.name}下载/更新成功`);
}
}
} catch (e) {
console.log(e);
}
}
const isWrite = await saveFile({
moduleName: item.name,
url: item.scriptURL,
});
if (isWrite) {
notify("下载提示", `插件:${item.title}下载/更新成功`);
}
};
r.addCell(downloadCell);
table.addRow(r);
});
table.present(false);
} catch (e) {
console.log(e);
notify("错误提示", "订阅获取失败");
}
};
const Run = async () => {
try {
const mainAlert = new Alert();
mainAlert.title = "组件下载";
mainAlert.message = "可以自行添加订阅地址";
const cacheKey = "subscriptionList";
const render = async () => {
let subscriptionList = [];
if (Keychain.contains(cacheKey)) {
subscriptionList = JSON.parse(Keychain.get(cacheKey));
}
const _actions = [];
console.log(subscriptionList);
subscriptionList.forEach((item) => {
const { author } = item;
mainAlert.addAction("作者:" + author);
_actions.push(async () => {
await renderTableList(item);
});
});
_actions.push(async () => {
const a = new Alert();
a.title = "订阅地址";
a.addTextField(
"URL",
"https://raw.githubusercontent.com/dompling/Scriptable/master/install.json"
);
a.addAction("确定");
a.addCancelAction("取消");
const id = await a.presentAlert();
if (id === -1) return;
try {
const url = a.textFieldValue(0);
const response = await new Request(url).loadJSON();
delete response.apps;
const data = [];
let isPush = true;
for (let i in subscriptionList) {
const item = subscriptionList[i];
if (response.author === item.author) {
isPush = false;
data.push({ ...response, subscription: url });
} else {
data.push(item);
}
}
if (isPush) data.push({ author: response.author, subscription: url });
Keychain.set(cacheKey, JSON.stringify(data));
notify("更新成功", "请重新运行本脚本");
} catch (e) {
console.log(e);
notify("错误提示", "订阅地址错误,不是一个 JSON 格式");
}
});
mainAlert.addAction("添加订阅");
mainAlert.addCancelAction("取消操作");
const _actionsIndex = await mainAlert.presentSheet();
if (_actions[_actionsIndex]) {
const func = _actions[_actionsIndex];
await func();
}
};
await render();
} catch (e) {
console.log("缓存读取错误" + e);
}
};
(async () => {
try {
console.log("🤖自更新开始");
const modules = {
moduleName: "widget.Install",
url:
"https://raw.githubusercontent.com/dompling/Scriptable/master/widget.Install.js",
};
const result = await saveFile(modules);
if (result) console.log("🤖自更新成功");
} catch (e) {
console.log(e);
}
})();
await Run();