/
random.js
103 lines (99 loc) · 3.95 KB
/
random.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-glyph: book;
// This script shows a random Scriptable API in a widget. The script is meant to be used with a widget configured on the Home Screen.
// You can run the script in the app to preview the widget or you can go to the Home Screen, add a new Scriptable widget and configure the widget to run this script.
// You can also try creating a shortcut that runs this script. Running the shortcut will show widget.
let api = await randomAPI()
let widget = await createWidget(api)
if (config.runsInWidget) {
// The script runs inside a widget, so we pass our instance of ListWidget to be shown inside the widget on the Home Screen.
Script.setWidget(widget)
} else {
// The script runs inside the app, so we preview the widget.
widget.presentMedium()
}
// Calling Script.complete() signals to Scriptable that the script have finished running.
// This can speed up the execution, in particular when running the script from Shortcuts or using Siri.
Script.complete()
async function createWidget(api) {
let appIcon = await loadAppIcon()
let title = "Random Scriptable API"
let widget = new ListWidget()
// Add background gradient
let gradient = new LinearGradient()
gradient.locations = [0, 1]
gradient.colors = [
new Color("141414"),
new Color("13233F")
]
widget.backgroundGradient = gradient
// Show app icon and title
let titleStack = widget.addStack()
let appIconElement = titleStack.addImage(appIcon)
appIconElement.imageSize = new Size(15, 15)
appIconElement.cornerRadius = 4
titleStack.addSpacer(4)
let titleElement = titleStack.addText(title)
titleElement.textColor = Color.white()
titleElement.textOpacity = 0.7
titleElement.font = Font.mediumSystemFont(13)
widget.addSpacer(12)
// Show API
let nameElement = widget.addText(api.name)
nameElement.textColor = Color.white()
nameElement.font = Font.boldSystemFont(18)
widget.addSpacer(2)
let descriptionElement = widget.addText(api.description)
descriptionElement.minimumScaleFactor = 0.5
descriptionElement.textColor = Color.white()
descriptionElement.font = Font.systemFont(18)
// UI presented in Siri ans Shortcuta is non-interactive, so we only show the footer when not running the script from Siri.
if (!config.runsWithSiri) {
widget.addSpacer(8)
// Add button to open documentation
let linkSymbol = SFSymbol.named("arrow.up.forward")
let footerStack = widget.addStack()
let linkStack = footerStack.addStack()
linkStack.centerAlignContent()
linkStack.url = api.url
let linkElement = linkStack.addText("Read more")
linkElement.font = Font.mediumSystemFont(13)
linkElement.textColor = Color.blue()
linkStack.addSpacer(3)
let linkSymbolElement = linkStack.addImage(linkSymbol.image)
linkSymbolElement.imageSize = new Size(11, 11)
linkSymbolElement.tintColor = Color.blue()
footerStack.addSpacer()
// Add link to documentation
let docsSymbol = SFSymbol.named("book")
let docsElement = footerStack.addImage(docsSymbol.image)
docsElement.imageSize = new Size(20, 20)
docsElement.tintColor = Color.white()
docsElement.imageOpacity = 0.5
docsElement.url = "https://docs.scriptable.app"
}
return widget
}
async function randomAPI() {
let docs = await loadDocs()
let apiNames = Object.keys(docs)
let num = Math.round(Math.random() * apiNames.length)
let apiName = apiNames[num]
let api = docs[apiName]
return {
name: apiName,
description: api["!doc"],
url: api["!url"]
}
}
async function loadDocs() {
let url = "https://docs.scriptable.app/scriptable.json"
let req = new Request(url)
return await req.loadJSON()
}
async function loadAppIcon() {
let url = "https://is5-ssl.mzstatic.com/image/thumb/Purple124/v4/21/1e/13/211e13de-2e74-4221-f7db-d6d2c53b4323/AppIcon-1x_U007emarketing-0-7-0-85-220.png/540x540sr.jpg"
let req = new Request(url)
return req.loadImage()
}