-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget_quote-from-ZenQuotes-API.js
More file actions
78 lines (63 loc) · 2.11 KB
/
widget_quote-from-ZenQuotes-API.js
File metadata and controls
78 lines (63 loc) · 2.11 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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: gray; icon-glyph: smile-wink;
const CONFIG = {
QUOTE: {
FONT: { NAME: "IowanOldStyle-BoldItalic", SIZE: 22 },
MINIMUM_SCALE_FACTOR: 0.1,
TEXT_OPACITY: 1,
TEXT_COLOR: Color.white(),
},
AUTHOR: {
FONT: { NAME: "Avenir Next", SIZE: 14 },
MINIMUM_SCALE_FACTOR: 0.1,
TEXT_OPACITY: 0.8,
TEXT_COLOR: Color.gray(),
},
SPACER: 15,
};
const Cache = importModule("Cache");
let cache = new Cache(Script.name());
let quote = await cache.getOrFetch(fetchQuote);
let widget = await createWidget(quote);
config.runsInWidget ? Script.setWidget(widget) : widget.presentMedium();
Script.complete();
// ================
// Helper functions
// ================
// ZenQuotes API: https://zenquotes.io
async function fetchQuote() {
const response = await new Request(
"https://zenquotes.io/api/random"
).loadJSON();
return {
q: response[0].q,
a: response[0].a,
};
}
async function createWidget(quote) {
let widget = new ListWidget();
let q = widget.addText(quote.q);
q.centerAlignText();
q.font = new Font(CONFIG.QUOTE.FONT.NAME, CONFIG.QUOTE.FONT.SIZE);
q.minimumScaleFactor = CONFIG.QUOTE.MINIMUM_SCALE_FACTOR;
q.textOpacity = CONFIG.QUOTE.TEXT_OPACITY;
q.textColor = CONFIG.QUOTE.TEXT_COLOR;
if (quote.a !== "Unknown") {
widget.addSpacer(CONFIG.SPACER);
let a = widget.addText(quote.a);
a.centerAlignText();
a.font = new Font(CONFIG.AUTHOR.FONT.NAME, CONFIG.AUTHOR.FONT.SIZE);
a.minimumScaleFactor = CONFIG.AUTHOR.MINIMUM_SCALE_FACTOR;
a.textOpacity = CONFIG.AUTHOR.TEXT_OPACITY;
a.textColor = CONFIG.AUTHOR.TEXT_COLOR;
}
// 👉 Download this shortcut: https://shortcutomation.com/add-to-inbox
widget.url =
`shortcuts://run-shortcut?` +
`name=${encodeURIComponent("📥 Add to Inbox")}&` +
`input=${encodeURIComponent(
`“${quote.q.trim()}” — ${quote.a.trim()}`
)}`;
return widget;
}