-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathpeanuts-widget.js
More file actions
144 lines (126 loc) · 3.74 KB
/
peanuts-widget.js
File metadata and controls
144 lines (126 loc) · 3.74 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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: tablets;
/* **********************************************
Name : peanuts-widget.js
Author : @supermamon
Version : 1.0.1
Desc : shows a random or today's Peanuts
comic strip from www.gocomics.com/peanuts/
Changelog:
-------------------------------------------------
v1.0.0 | 2020-10-28
* Initial release
-------------------------------------------------
v1.0.1 | 2022-09-20
* (fix) regex matching
* (update) merged no-background option
* (update) implemented Color.dynamic() for gradient
********************************************** */
const BACKGROUND_DARK_MODE = "system"
// options: "yes", "no", "system"
const TRANSPARENT = false
const SHOW_TITLE = true
const SHOW_DATE = true
let RANDOM = false
// default is current comic
// set the Parameter value to "random" in the
// Edit Widget screen to use a random comic
if (args.widgetParameter == 'random') {
RANDOM = true
}
let data = await loadData(RANDOM)
let widget = await createWidget(data)
if (!config.runsInWidget) {
await widget.presentMedium()
} else {
Script.setWidget(widget)
}
Script.complete()
// -----------------------------------------------
async function createWidget(data) {
const w = new ListWidget();
w.setPadding(15, 0, 15, 0)
let fromLightColor = 'b00a0fe6'
let fromDarkColor = '#010c1ee6'
let toLightColor = 'b00a0fb3'
let toDarkColor = '#010c1ee6'
if (BACKGROUND_DARK_MODE == 'no') {
fromDarkColor = fromLightColor
toDarkColor = fromDarkColor
}
if (BACKGROUND_DARK_MODE == 'yes') {
fromLightColor = fromDarkColor
toLightColor = toDarkColor
}
if (!TRANSPARENT) {
let gradient = new LinearGradient()
gradient.locations = [0, 1]
gradient.colors = [
Color.dynamic(new Color(fromLightColor), new Color(fromDarkColor)),
Color.dynamic(new Color(toLightColor), new Color(toDarkColor)),
]
w.backgroundGradient = gradient
} else {
const fm = FileManager.iCloud()
const nobgscript = fm.joinPath(fm.documentsDirectory(), 'no-background.js')
if (fm.fileExists(nobgscript)) {
const nobg = importModule('no-background')
w.backgroundImage = await nobg.getSliceForWidget('peanuts-widget')
}
}
w.addSpacer()
if (SHOW_TITLE) {
let titleTxt = w.addText('Peanuts')
titleTxt.font = Font.boldSystemFont(14)
titleTxt.centerAlignText()
titleTxt.textColor = Color.white()
w.addSpacer(2)
}
let img = await downloadImage(data.img);
let pic = w.addImage(img)
pic.centerAlignImage()
if (SHOW_DATE) {
w.addSpacer(2)
let dateTxt = w.addText(data.formattedDate)
dateTxt.centerAlignText()
dateTxt.textColor = Color.white()
}
w.addSpacer()
return w
}
// -----------------------------------------------
async function downloadImage(imgurl) {
let imgReq = new Request(imgurl)
let img = await imgReq.loadImage()
return img
}
// -----------------------------------------------
async function loadData(random) {
var comicDate = new Date()
if (random) {
var start = new Date(1950, 9, 2)
var end = new Date()
comicDate = new Date(+start + Math.random() * (end - start));
}
log(comicDate)
var df = new DateFormatter()
df.dateFormat = 'YYYY/MM/dd'
var dfDate = df.string(comicDate)
var url = `https://www.gocomics.com/peanuts/${dfDate}`
var req = new Request(url)
var src = await req.loadString()
log(src)
var m = src.match(/<picture class="gc-card__image.+?src="([^"]+)/)
var imgUrl = m[1]
return {
date: comicDate,
formattedDate: dfDate,
img: imgUrl
}
}
// -----------------------------------------------
function randomDate(start, end) {
var date = new Date(+start + Math.random() * (end - start));
return date;
}