-
Notifications
You must be signed in to change notification settings - Fork 0
/
todayHistory.js
99 lines (90 loc) · 2.68 KB
/
todayHistory.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: teal; icon-glyph: newspaper;
const _info = {
name: 'todayHistory',
version: '1.1',
updated_at: '2023-12-06 13:00:00',
author: 'ycrao',
description: 'history events on today',
repo_file_url: 'https://github.com/ycrao/scripts-for-scriptable/blob/main/app/todayHistory.js',
raw_file_url: 'https://raw.githubusercontent.com/ycrao/scripts-for-scriptable/main/app/todayHistory.js'
}
class Billboard {
constructor(title, items) {
this.title = title
this.items = items
}
randomColor() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
}
render() {
const widget = new ListWidget()
widget.backgroundColor = new Color('#1e1e1e', 1)
widget.setPadding(10, 10, 10, 10)
const title = widget.addText(this.title)
title.font = Font.boldSystemFont(18)
title.textColor = new Color("#f7e01e", 1)
title.centerAlignText()
widget.addSpacer(20)
const len = this.items.length
console.log(len)
let maxInt = 5
let start = 0
if (len > maxInt) {
const diff = len - maxInt
start = Math.floor(Math.random()*diff)
}
let loop = 0
while (loop < maxInt) {
let idx = loop + start
// using stack
const stack = widget.addStack()
const t = stack.addText(this.items[idx].title)
t.font = Font.lightSystemFont(12)
t.textColor = new Color(this.randomColor(), 1)
t.leftAlignText()
t.lineLimit = 1
stack.centerAlignContent()
if (this.items[idx].url) {
stack.url = `${this.items[idx].url}`
}
widget.addSpacer(10)
loop ++
}
if (config.runsInApp) {
widget.presentLarge()
}
Script.setWidget(widget)
Script.complete()
}
}
async function getItems() {
const wv = new WebView()
await wv.loadURL("https://tool.lu/todayonhistory/")
jsStr = `
const newsItems = []
const ulEl = document.querySelector('#tohlis')
const liEls = ulEl.querySelectorAll('li')
liEls.forEach((li) => {
let title = li.innerText
title = title.replace('\\n', '')
console.log(title)
let url = "https://m.baidu.com/s?word=" + encodeURIComponent(title)
newsItems.push({
title: title,
url: url
})
})
completion(newsItems)
`
const newsItems = await wv.evaluateJavaScript(jsStr, true)
// wv.present(true)
return newsItems
}
const items = await getItems()
bb = new Billboard("历史上的今天", items)
bb.render()