-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacstory.js
123 lines (118 loc) · 3.85 KB
/
macstory.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: newspaper;
// This script shows articles from MacStories. The script can be used either in the app, as a widget on your Home Screen or through Shortcuts. The behaviour of the script will vary slightly depending on where it's used.
let items = await loadItems()
if (config.runsInWidget) {
// Tell the widget on the Home Screen to show our ListWidget instance.
let widget = await createWidget(items)
Script.setWidget(widget)
} else if (config.runsWithSiri) {
// Present a table with a subset of the news.
let firstItems = items.slice(0, 5)
let table = createTable(firstItems)
await QuickLook.present(table)
} else {
// Present the full list of news.
let table = createTable(items)
await QuickLook.present(table)
}
// 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(items) {
let item = items[0]
let authors = item.authors.map(a => {
return a.name
}).join(", ")
let imgURL = extractImageURL(item)
let rawDate = item["date_published"]
let date = new Date(Date.parse(rawDate))
let dateFormatter = new DateFormatter()
dateFormatter.useMediumDateStyle()
dateFormatter.useShortTimeStyle()
let strDate = dateFormatter.string(date)
let gradient = new LinearGradient()
gradient.locations = [0, 1]
gradient.colors = [
new Color("#b00a0fe6"),
new Color("#b00a0fb3")
]
let widget = new ListWidget()
if (imgURL != null) {
let imgReq = new Request(imgURL)
let img = await imgReq.loadImage()
widget.backgroundImage = img
}
widget.backgroundColor = new Color("#b00a0f")
widget.backgroundGradient = gradient
// Add spacer above content to center it vertically.
widget.addSpacer()
// Show article headline.
let title = decode(item.title)
let titleElement = widget.addText(title)
titleElement.font = Font.boldSystemFont(16)
titleElement.textColor = Color.white()
titleElement.minimumScaleFactor = 0.75
// Add spacing below headline.
widget.addSpacer(8)
// Add footer woth authors and date.
let footerStack = widget.addStack()
let authorsElement = footerStack.addText(authors)
authorsElement.font = Font.mediumSystemFont(12)
authorsElement.textColor = Color.white()
authorsElement.textOpacity = 0.9
footerStack.addSpacer()
let dateElement = footerStack.addText(strDate)
dateElement.font = Font.mediumSystemFont(12)
dateElement.textColor = Color.white()
dateElement.textOpacity = 0.9
// Add spacing below content to center it vertically.
widget.addSpacer()
// Set URL to open when tapping widget.
widget.url = item.url
return widget
}
function createTable(items) {
let table = new UITable()
for (item of items) {
let row = new UITableRow()
let imageURL = extractImageURL(item)
let title = decode(item.title)
let imageCell = row.addImageAtURL(imageURL)
let titleCell = row.addText(title)
imageCell.widthWeight = 20
titleCell.widthWeight = 80
row.height = 60
row.cellSpacing = 10
row.onSelect = (idx) => {
let item = items[idx]
Safari.open(item.url)
}
row.dismissOnSelect = false
table.addRow(row)
}
return table
}
async function loadItems() {
let url = "https://macstories.net/feed/json"
let req = new Request(url)
let json = await req.loadJSON()
return json.items
}
function extractImageURL(item) {
let regex = /<img src="(.*)" alt="/
let html = item["content_html"]
let matches = html.match(regex)
if (matches && matches.length >= 2) {
return matches[1]
} else {
return null
}
}
function decode(str) {
let regex = /&#(\d+);/g
return str.replace(regex, (match, dec) => {
return String.fromCharCode(dec)
})
}