-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlockscreen-rss-widget_rotation.js
More file actions
67 lines (51 loc) · 1.8 KB
/
lockscreen-rss-widget_rotation.js
File metadata and controls
67 lines (51 loc) · 1.8 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
// Scriptable Lockscreen RSS widget (with rotation feature)
// Version: v1.1.1
// Author: @leon47331
const url = "RSS-FEED-URL";
const srcName = "SITE-NAME";
const rotationValue = 5;
async function getRSSFeed() {
let req = new Request(url);
let feed = await req.loadString();
let items = [];
let regex = /<item>[\s\S]*?<title>(?:<!\[CDATA\[)?(.*?)(?:\]\]>)?<\/title>[\s\S]*?<link>(.*?)<\/link>[\s\S]*?<\/item>/g;
let match;
while ((match = regex.exec(feed)) !== null) {
let title = match[1];
let link = match[2];
title = title.replace(/"/g, "\"");
title = title.replace(/„/g, "„").replace(/“/g, "“");
title = title.replace(/–/g, "–");
items.push({ title, link });
}
return items.slice(0, rotationValue);
}
async function getRandomEntry(items) {
if (items.length === 0) return null;
let randomIndex = Math.floor(Math.random() * items.length);
return items[randomIndex];
}
let widget = new ListWidget();
async function createWidget() {
let rssItems = await getRSSFeed();
let latestItem = await getRandomEntry(rssItems);
if (latestItem !== null) {
let title = latestItem.title;
let link = latestItem.link;
let stack = widget.addStack();
stack.layoutVertically();
let titleText = widget.addText(srcName);
titleText.font = Font.boldSystemFont(13);
titleText.textColor = new Color("#adacac");
widget.addSpacer(1);
let newsText = widget.addText(title);
newsText.font = Font.semiboldMonospacedSystemFont(12);
widget.url = link;
} else {
widget.addText("No new articles available");
widget.url = "scriptable://" // FIXME: Placeholder
}
Script.setWidget(widget);
Script.complete();
}
await createWidget();