-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgit-script-download-update.js
More file actions
437 lines (373 loc) · 12.7 KB
/
Copy pathgit-script-download-update.js
File metadata and controls
437 lines (373 loc) · 12.7 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: file-download;
// share-sheet-inputs: plain-text, url;
// Type of notification to appear when finished some actions. Can be "notification", "alert" or null/undefined. Do not change anything else.
const notifyType = "notification"
const fm = FileManager.iCloud()
const baseDir = fm.documentsDirectory()
if (config.runsInWidget) {
// Run from widget
await createWidget()
Script.complete()
} else if (args.plainTexts[0]) {
// Run from share sheet
await addFromUrl(args.plainTexts[0])
Script.complete()
} else {
// Run from Scriptable
let mainMenu = makeAlert(null, null, [
"Check For Updates",
"Add Script",
"Remove Script",
"Rename Script",
"View Scripts"
],"Cancel")
switch (await mainMenu.presentSheet()) {
case 0:
await askForUpdates(await checkUpdates())
Script.complete()
break
case 1:
let urlAlert = makeAlert("Enter A URL", "Enter a URL or use clipboard", [
"Use Clipboard",
"Use Text"
])
urlAlert.addTextField("URL").setURLKeyboard()
let url =
(await urlAlert.presentAlert()) == 1
? urlAlert.textFieldValue(0)
: Pasteboard.paste()
await addFromUrl(url)
Script.complete()
break
case 2:
await removeScript()
Script.complete()
break
case 3:
await renameScript()
Script.complete()
break
case 4:
await viewScript()
Script.complete()
break
default:
Script.complete()
}
}
// utility function
function makeAlert(title, message, options,cancel) {
let alert = new Alert()
alert.message = message
alert.title = title
for (let option of options) {
alert.addAction(option)
}
if(cancel) {
alert.addCancelAction(cancel)
}
return alert
}
async function getData() {
let file = fm.joinPath(baseDir, "githubScripts.json")
if (!fm.fileExists(file)) {
return {scripts: []}
}
if (!fm.isFileDownloaded(file)) {
await fm.downloadFileFromiCloud(file)
}
const data = JSON.parse(fm.readString(file))
return data.scripts
}
function writeData(data) {
fm.writeString(
fm.joinPath(baseDir, "githubScripts.json"),
JSON.stringify({scripts: data})
)
}
async function notify(title, text) {
if (notifyType == "notification" || notifyType == "alert") {
if (notifyType == "notification") {
let notification = new Notification()
notification.title = title
notification.body = text
await notification.schedule()
} else {
let a = makeAlert(title, text, [], "OK")
await a.presentAlert()
}
}
}
async function addFromUrl(url) {
if (!url) {
let a = makeAlert("No URL Specified", "", [],"OK")
await a.presentAlert()
return
}
if (url.startsWith("https://raw.githubusercontent.com")) {
url = url
.replace("https://raw.githubusercontent.com", "https://github.com")
.replace(/(https:\/\/[^/]*\/[^/]*\/[^/]*\/)([^/]*)/, "$1blob/$2")
console.log(url)
}
if (
!/^\s*https:\/\/github.com\/.+?\/(blob|raw)\/.+?\.(js|scriptable)\s*$/.test(
url
)
) {
let a = makeAlert(
"Invalid URL",
"URL must be a GitHub blob or raw .js or .scriptable file",
[],
"OK"
)
await a.presentAlert()
return
}
const commitsUrl = url.replace(/\/(blob|raw)\//, "/commits/")
const rawUrl = url.replace(/\/blob\//, "/raw/")
const commitsHTMl = await new Request(commitsUrl).loadString()
const lastEditDate = new Date(
commitsHTMl.match(
/<relative-time datetime="(.+?)" class="no-wrap">.+?<\/relative-time>/
)[1]
).toString()
const content = await new Request(rawUrl).loadString()
let name = url.match(/([^/]+?)\.(?:js|scriptable)$/)[1]
// If it is this script, the name is the current script name
if (
/^\s*https:\/\/github.com\/Normal-Tangerine8609\/Scriptable-Git-Script-Download-Update\/(blob|raw)\/main\/git-script.js\s*$/.test(
url
)
) {
name = Script.name()
} else {
let nameAlert = makeAlert("Chose A Script Name", null, ["Done"])
nameAlert.addTextField("", name)
await nameAlert.presentAlert()
name = nameAlert.textFieldValue(0)
if (fm.fileExists(fm.joinPath(baseDir, name + ".js"))) {
let a = makeAlert(
"Duplicate Name",
"Script name must not be the same as another script name",
[],
"OK"
)
await a.presentAlert()
return
}
}
let data = await getData()
data.push({name, url, lastEditDate})
writeData(data)
fm.writeString(fm.joinPath(baseDir, name + ".js"), content)
await notify(
"Script Added",
`The script ${name} has been added to the update list and your scriptable collection`
)
}
async function removeScript() {
let data = await getData()
let alert = makeAlert("What Script Would You Like To Remove?", null, data.map(e => e.name), "Cancel")
let removedScript = await alert.presentSheet()
if (removedScript != -1) {
let oldName = data[removedScript].name
data.splice(removedScript, 1)
writeData(data)
await notify(
"Script Removed",
`The script ${oldName} has been removed from the update list`
)
}
}
async function renameScript() {
let data = await getData()
let alert = makeAlert("What Script Would You Like To Rename?", null, data.map(e => e.name), "Cancel")
let renamedScript = await alert.presentSheet()
if (renamedScript != -1) {
let script = data[renamedScript]
let nameAlert = makeAlert("Chose A Script Name", null, ["Done"])
nameAlert.addTextField("", script.name)
await nameAlert.presentAlert()
let name = nameAlert.textFieldValue(0)
if (fm.fileExists(fm.joinPath(baseDir, name + ".js"))) {
let a = makeAlert(
"Duplicate Name",
"Script name must not be the same as another script name",
[],
"OK"
)
await a.presentAlert()
return
}
data[renamedScript] = {
name,
url: script.url,
lastEditDate: script.lastEditDate
}
writeData(data)
await notify(
"Script Renamed",
`The script ${script["name"]} has been renamed to ${name}`
)
}
}
async function viewScript() {
let table = "<tr><th>Name</th><th>URL</th><th>Last Update</th></tr>"
let data = await getData()
for (let script of data) {
table += `<tr><td>${script.name}</td><td>${
script.url
}</td><td>${new Date(script.lastEditDate).toDateString()}</td></tr>`
}
let w = new WebView()
w.loadHTML(`<html><head><meta name="viewport" content="width=320, initial-scale=1"><meta charset="utf-8"><title>Git Scripts</title><style>body{margin:0px;padding:0px;text-align: center;font-family: System-ui;}@media (prefers-color-scheme: dark){body{background-color: black; color: white;}td{background-color: #1c1c1c}th,td{border: 1px solid #494949}tr:nth-child(odd) td{background-color:#252525}}@media (prefers-color-scheme: light){body{background-color: white; color: black;}td{background-color: #d3d3d3}tr:nth-child(odd) td{background-color:#b7b7b7;}tr,td{border: 1px solid black}}table{border-collapse: collapse; width: 100%;}th, td{padding: 0.25rem; text-align: left;vertical-align: middle;}.table{display: block; overflow: auto; white-space: nowrap; position: absolute; left:0; right:0; width:100%; height: 100%;}</style></head><body><div class="table"><table id="t">${table}</table></div><script>var table,rows,switching,i,x,y,shouldSwitch;for(table=document.getElementById("t"),switching=!0;switching;){for(switching=!1,rows=table.rows,i=1;i<rows.length-1;i++)if(shouldSwitch=!1,x=rows[i].getElementsByTagName("TD")[0],y=rows[i+1].getElementsByTagName("TD")[0],x.innerHTML.toLowerCase()>y.innerHTML.toLowerCase()){shouldSwitch=!0;break}shouldSwitch&&(rows[i].parentNode.insertBefore(rows[i+1],rows[i]),switching=!0)}</script></body></html>
`)
await w.present()
}
async function checkUpdates() {
const data = await getData()
let updatesNeeded = []
for (let script of data) {
const commitsUrl = script.url.replace(/\/(blob|raw)\//, "/commits/")
const commitsHTMl = await new Request(commitsUrl).loadString()
const date = new Date(
commitsHTMl.match(
/<relative-time datetime="(.+?)" class="no-wrap">.+?<\/relative-time>/
)[1]
).toString()
if (date != new Date(script.lastEditDate)) {
updatesNeeded.push({
name: script.name,
url: script.url,
newDate: date
})
}
}
return updatesNeeded
}
async function askForUpdates(list) {
if (list.length == 0) {
await notify("Up To Date", "All scripts are currently up to date")
return
}
let data = await getData()
for (let script of list) {
let a = makeAlert(
script.name,
"Would you like to update the script?",
[]
)
a.addCancelAction("No")
a.addAction("Yes")
if ((await a.presentAlert()) != -1) {
const rawUrl = script.url.replace(/\/blob\//, "/raw/")
const content = await new Request(rawUrl).loadString()
fm.writeString(fm.joinPath(baseDir, script.name + ".js"), content)
const scriptIndex = data.findIndex((e) => e.name === script.name)
data[scriptIndex] = {
name: script.name,
url: script.url,
lastEditDate: script.newDate
}
}
}
writeData(data)
await notify(
"Update Check Complete",
"All scripts needing an update have been shown"
)
}
async function createWidget() {
const updatesArray = await checkUpdates()
const updatesNumber = updatesArray.length
const textMessage =
updatesNumber !== 0
? `Script${updatesNumber == 1 ? "" : "s"} To Update`
: "All Clear"
const widget = new ListWidget()
let date = new Date()
date.setMinutes(date.getHours() + 3)
widget.refreshAfterDate = date
if (config.runsInAccessoryWidget) {
if(config.widgetFamily === "accessoryCircular") {
widget.addSpacer()
const title = widget.addText("Updates")
title.font = Font.boldSystemFont(13)
title.minimumScaleFactor = 0.5
title.centerAlignText()
widget.addSpacer()
const text = widget.addText(updatesNumber.toString())
text.centerAlignText()
text.font = Font.boldSystemFont(25)
text.minimumScaleFactor = 0.5
text.lineLimit = 1
widget.addSpacer()
widget.presentAccessoryCircular()
} else if (config.widgetFamily === "accessoryRectangular"){
widget.addSpacer()
const title = widget.addText("Git Script Download & Update")
title.font = Font.boldSystemFont(13)
title.minimumScaleFactor = 0.5
title.centerAlignText()
widget.addSpacer()
const text = widget.addText(updatesNumber.toString() + " " + textMessage)
text.centerAlignText()
text.font = Font.boldSystemFont(30)
text.minimumScaleFactor = 0.5
text.lineLimit = 1
widget.addSpacer()
widget.presentAccessoryRectangular()
} else {
widget.addSpacer()
const text = widget.addText(`| ${updatesNumber.toString()} ${textMessage}`)
text.centerAlignText()
text.font = Font.boldSystemFont(30)
text.minimumScaleFactor = 0.5
text.lineLimit = 1
widget.addSpacer()
widget.presentAccessoryInline()
}
} else {
// made for small but works for mediim and large
const backgroundColor = Color.dynamic(Color.white(), Color.black())
const textColor = Color.dynamic(Color.black(), Color.white())
widget.backgroundColor = backgroundColor
const title = widget.addText("Git Script Download & Update")
title.centerAlignText()
title.lineLimit = 2
title.minimumScaleFactor = 0.3
title.textColor = textColor
widget.addSpacer(5)
const hr = widget.addStack()
hr.backgroundColor = Color.red()
hr.cornerRadius = 2
hr.addSpacer()
const i = hr.addImage(
Image.fromData(
Data.fromBase64String(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="
)
)
)
i.imageSize = new Size(1, 2)
hr.addSpacer()
widget.addSpacer()
const updatesNumberText = widget.addText(updatesNumber.toString())
updatesNumberText.minimumScaleFactor = 0.3
updatesNumberText.lineLimit = 1
updatesNumberText.textColor = textColor
updatesNumberText.font = Font.boldRoundedSystemFont(70)
updatesNumberText.centerAlignText()
widget.addSpacer()
const message = widget.addText(textMessage)
message.centerAlignText()
message.lineLimit = 1
message.minimumScaleFactor = 0.3
message.textColor = textColor
widget.presentSmall()
}
Script.setWidget(widget)
}