sf-jury-duty-widget.js
· 2.0 KiB · JavaScript
原始文件
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-glyph: theater-masks;
// Your reporting group number:
const GROUP_NUMBER = 69
const PAGE =
'https://www.sfsuperiorcourt.org/divisions/jury-services/jury-reporting'
const REPORTING = 'REPORTING'
const STANDBY = 'STANDBY'
const SAFE = 'safe'
const WANTED = 'wanted'
const UNCLEAR = 'unclear'
const RE_HTML = /<\/?[\w\s]*>|<.+[\W]>/g
const RE_RESULTS = new RegExp(
`GROUPS.*?:\s?\nGroups.*${GROUP_NUMBER}.*?\n`,
'g'
)
const COLOR_RED = new Color('#FF9580')
const COLOR_GREEN = new Color('#8AFF80')
const fetchPage = async (URL) => {
const req = new Request(URL)
const str = await req.loadString()
return str.replace(RE_HTML, '')
}
const getResults = async () => {
const data = await fetchPage(PAGE)
const results = data.match(RE_RESULTS)
return results ? results.join('') : ''
}
const getType = (results) => {
if (results.indexOf(STANDBY) !== -1) {
return SAFE
}
if (results.indexOf(REPORTING) !== -1) {
return WANTED
}
return UNCLEAR
}
const makeWidget = async () => {
const results = await getResults()
const type = getType(results)
const widget = new ListWidget()
const gradient = new LinearGradient()
gradient.colors = [new Color('#000'), new Color('#000')]
gradient.locations = [0, 1]
widget.backgroundGradient = gradient
const textStack = widget.addStack()
textStack.layoutVertically()
textStack.centerAlignContent()
const textPrefix = textStack.addText('You are')
textPrefix.font = Font.semiboldRoundedSystemFont(16)
textPrefix.textColor = new Color('#F8F8F2')
textPrefix.leftAlignText()
const textTitle = textStack.addText(type)
textTitle.font = Font.boldRoundedSystemFont(32)
textTitle.textColor = type === WANTED ? COLOR_RED : COLOR_GREEN
textTitle.leftAlignText()
widget.url = PAGE
return widget
}
const widget = await makeWidget()
if (config.runsInWidget) {
Script.setWidget(widget)
Script.complete()
} else {
widget.presentSmall()
}
1 | // Variables used by Scriptable. |
2 | // These must be at the very top of the file. Do not edit. |
3 | // icon-color: deep-blue; icon-glyph: theater-masks; |
4 | |
5 | // Your reporting group number: |
6 | const GROUP_NUMBER = 69 |
7 | |
8 | const PAGE = |
9 | 'https://www.sfsuperiorcourt.org/divisions/jury-services/jury-reporting' |
10 | const REPORTING = 'REPORTING' |
11 | const STANDBY = 'STANDBY' |
12 | const SAFE = 'safe' |
13 | const WANTED = 'wanted' |
14 | const UNCLEAR = 'unclear' |
15 | |
16 | const RE_HTML = /<\/?[\w\s]*>|<.+[\W]>/g |
17 | const RE_RESULTS = new RegExp( |
18 | `GROUPS.*?:\s?\nGroups.*${GROUP_NUMBER}.*?\n`, |
19 | 'g' |
20 | ) |
21 | |
22 | const COLOR_RED = new Color('#FF9580') |
23 | const COLOR_GREEN = new Color('#8AFF80') |
24 | |
25 | const fetchPage = async (URL) => { |
26 | const req = new Request(URL) |
27 | const str = await req.loadString() |
28 | return str.replace(RE_HTML, '') |
29 | } |
30 | |
31 | const getResults = async () => { |
32 | const data = await fetchPage(PAGE) |
33 | const results = data.match(RE_RESULTS) |
34 | return results ? results.join('') : '' |
35 | } |
36 | |
37 | const getType = (results) => { |
38 | if (results.indexOf(STANDBY) !== -1) { |
39 | return SAFE |
40 | } |
41 | if (results.indexOf(REPORTING) !== -1) { |
42 | return WANTED |
43 | } |
44 | return UNCLEAR |
45 | } |
46 | |
47 | const makeWidget = async () => { |
48 | const results = await getResults() |
49 | const type = getType(results) |
50 | |
51 | const widget = new ListWidget() |
52 | const gradient = new LinearGradient() |
53 | gradient.colors = [new Color('#000'), new Color('#000')] |
54 | gradient.locations = [0, 1] |
55 | widget.backgroundGradient = gradient |
56 | |
57 | const textStack = widget.addStack() |
58 | textStack.layoutVertically() |
59 | textStack.centerAlignContent() |
60 | |
61 | const textPrefix = textStack.addText('You are') |
62 | textPrefix.font = Font.semiboldRoundedSystemFont(16) |
63 | textPrefix.textColor = new Color('#F8F8F2') |
64 | textPrefix.leftAlignText() |
65 | |
66 | const textTitle = textStack.addText(type) |
67 | textTitle.font = Font.boldRoundedSystemFont(32) |
68 | textTitle.textColor = type === WANTED ? COLOR_RED : COLOR_GREEN |
69 | textTitle.leftAlignText() |
70 | |
71 | widget.url = PAGE |
72 | |
73 | return widget |
74 | } |
75 | |
76 | const widget = await makeWidget() |
77 | if (config.runsInWidget) { |
78 | Script.setWidget(widget) |
79 | Script.complete() |
80 | } else { |
81 | widget.presentSmall() |
82 | } |
83 |