forked from evilbutcher/Scriptables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Env.js
216 lines (199 loc) · 4.93 KB
/
Env.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: yellow; icon-glyph: magic;
/**
* Author: GideonSenku
* Github: https://github.com/GideonSenku
*/
const request = new Request('')
const dict = FileManager.iCloud().documentsDirectory()
const defaultHeaders = {
"Accept": "*/*",
"Content-Type": "application/json"
}
/**
* @description GET,返回String数据
* @param {*} param0 request信息
* @param {*} callback 回调返回response和JSON对象
*/
const get = async ({ url, headers = {} }, callback = () => {} ) => {
request.url = url
request.method = 'GET'
request.headers = {
...headers,
...defaultHeaders
}
const data = await request.loadJSON()
callback(request.response, data)
return data
}
/**
* @description GET,返回String数据
* @param {*} param0 request信息
* @param {*} callback 回调返回response和String对象
*/
const getStr = async ({ url, headers = {} }, callback = () => {} ) => {
request.url = url
request.method = 'GET'
request.headers = {
...headers,
...defaultHeaders
}
const data = await request.loadString()
callback(request.response, data)
return data
}
/**
* @description POST,返回String数据
* @param {*} param0 request信息
* @param {*} callback 回调返回response和String
*/
const post = async ({ url, body, headers = {} }, callback = () => {} ) => {
request.url = url
request.body = body
request.method = 'POST'
request.headers = {
...defaultHeaders,
...headers
}
const data = await request.loadString()
callback(request.response, data)
return data
}
/**
* @description POST,返回JSON数据
* @param {*} param0 request信息
* @param {*} callback 回调返回response和JSON
*/
const _post = async ({ url, body, headers = {} }, callback = () => {} ) => {
request.url = url
request.body = body
request.method = 'POST'
request.headers = {
...defaultHeaders,
...headers
}
const data = await request.loadJSON()
callback(request.response, data)
return data
}
/**
* @description 下载文件
* @param {*} param0
*/
const getFile = async ({moduleName, url}) => {
log(`开始下载文件: 🌝 ${moduleName}`)
const header = `// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: file-code;\n`;
const content = await getStr({url})
const fileHeader = content.includes('icon-color') ? `` : header
write(`${moduleName}`, `${fileHeader}${content}`)
log(`文件下载完成: 🌚 ${moduleName}`)
}
/**
*
* @description 导入模块,不存在即下载模块,也可传入forceDownload: true 强制更新模块
* @param {*} param0
*/
const require = ({
moduleName,
url = '',
forceDownload = false
}) => {
if (isFileExists(moduleName) && !forceDownload) {
log(`导入模块: 🪐${moduleName}`)
return importModule(moduleName)
} else {
getFile({ moduleName, url })
log(`导入模块: 🪐${moduleName}`)
return importModule(moduleName)
}
}
/**
*
* @description 将数据写入文件
* @param {*} fileName 要写入的文件名,默认JS文件,可选其他,加上文件名后缀即可
* @param {*} content 要写入的文件内容
*/
const write = (fileName, content) => {
let file = initFile(fileName)
const filePath = `${dict}/${file}`
FileManager.iCloud().writeString(filePath, content)
return true
}
/**
*
* @description 判断文件是否存在
* @param {*} fileName
*/
const isFileExists = (fileName) => {
let file = initFile(fileName)
return FileManager.iCloud().fileExists(`${dict}/${file}`)
}
const initFile = (fileName) => {
const hasSuffix = fileName.lastIndexOf('.') + 1
return !hasSuffix ? `${fileName}.js` : fileName
}
/**
*
* @description 读取文件内容
* @param {*} fileName 要读取的文件名,默认JS文件,可选其他,加上文件名后缀即可
* @return 返回文件内容,字符串形式
*/
const read = (fileName) => {
const file = initFile(fileName)
return FileManager.iCloud().readString(`${dict}/${file}`)
}
/**
*
* @description 提示框
* @param {*} title 提示框标题
* @param {*} message 提示框内容
* @param {*} btnMes 提示框按钮标题,默认Cancel
*/
const msg = (title, message, btnMes = 'Cancel') => {
if (!config.runsInWidget) {
const alert = new Alert()
alert.title = title
alert.message = message
alert.addAction(btnMes)
alert.present()
}
}
const setdata = (Val, Key) => {
Keychain.set(Val, Key)
return true
}
const getdata = (Key) => {
return Keychain.get(Key)
}
const hasdata = (Key) => {
return Keychain.contains(Key)
}
const rmdata = (Key) => {
Keychain.remove(Key)
return true
}
const logErr = (e, messsage) => {
console.error(e)
}
module.exports = {
dict,
get,
getStr,
post,
_post,
getFile,
require,
write,
isFileExists,
initFile,
read,
setdata,
getdata,
hasdata,
rmdata,
msg,
logErr
}