forked from im3x/Scriptables
-
Notifications
You must be signed in to change notification settings - Fork 1
/
latest.js
218 lines (188 loc) · 5.96 KB
/
latest.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// ONE·一个图文
// iOS14 桌面组件插件 for Scriptable
// author@古人云
// https://github.com/im3x/Scriptables
//
class Im3xWidget {
// 初始化,接收参数
constructor (arg, loader) {
this.loader = loader
this.arg = parseInt(arg)
if (!Number.isInteger(this.arg)) this.arg = 0
}
// 渲染组件
async render () {
let data = await this.getData()
let widget = await (config.widgetFamily === 'large') ? this.renderLarge(data) : this.renderSmall(data)
return widget
}
async renderLarge (one) {
let w = new ListWidget()
if (!one) return await this.renderErr(w)
w.url = this.loader ? this.getURIScheme('open-url', one['url']) : one["url"]
// 时间
const dates = one["date"].split(" / ")
let date1 = w.addText(dates[2])
date1.font = Font.lightSystemFont(60)
date1.centerAlignText()
date1.textColor = Color.white()
let line = w.addText("————".repeat(2))
line.textOpacity = 0.5
line.centerAlignText()
line.textColor = Color.white()
let date2 = w.addText(dates[0] + " / " + dates[1])
date2.font = Font.lightMonospacedSystemFont(30)
date2.centerAlignText()
date2.textColor = Color.white()
date2.textOpacity = 0.7
// 换行
w.addSpacer(20)
// 内容
let body = w.addText(one["content"])
body.font = Font.lightSystemFont(22)
body.textColor = Color.white()
w.addSpacer(50)
let author = w.addText("—— " + one["text_authors"])
author.rightAlignText()
author.font = Font.lightSystemFont(14)
author.textColor = Color.white()
author.textOpacity = 0.8
// 加载背景图片
let bg = await this.getImage(one["img_url"])
w.backgroundImage = await this.shadowImage(bg)
// 记得最后返回组件
return w
}
async renderSmall (one) {
console.log('create.small.widget')
let w = new ListWidget()
if (!one) return await this.renderErr(w)
w.url = this.loader ? this.getURIScheme('open-url', one['url']) : one["url"]
w = await this.renderHeader(w, 'http://image.wufazhuce.com/apple-touch-icon.png', '「ONE · 一个」')
console.log('render.header.done')
let body = w.addText(one['content'])
body.textColor = Color.white()
body.font = Font.lightSystemFont(config.widgetFamily === 'small' ? 14 : 16)
w.addSpacer(10)
let footer = w.addText('—— ' + one['text_authors'])
footer.rightAlignText()
footer.textColor = Color.white()
footer.textOpacity = 0.8
footer.font = Font.lightSystemFont(12)
// 加载背景图片
let bg = await this.getImage(one["img_url"])
w.backgroundImage = await this.shadowImage(bg)
console.log('create.small.widget.done')
return w
}
async renderErr (widget) {
let err = widget.addText("💔 加载失败,稍后重试..")
err.textColor = Color.red()
err.centerAlignText()
return widget
}
/**
* 渲染标题
* @param widget 组件对象
* @param icon 图标url地址
* @param title 标题
*/
async renderHeader (widget, icon, title) {
let header = widget.addStack()
header.centerAlignContent()
let _icon = header.addImage(await this.getImage(icon))
_icon.imageSize = new Size(14, 14)
_icon.cornerRadius = 4
header.addSpacer(10)
let _title = header.addText(title)
_title.textColor = Color.white()
_title.textOpacity = 0.7
_title.font = Font.boldSystemFont(12)
widget.addSpacer(15)
return widget
}
async getData () {
const API = "http://m.wufazhuce.com/one";
const req1 = new Request(API)
// await req1.load()
const body1 = await req1.loadString()
const token = body1.split("One.token = '")[1].split("'")[0]
const API2 = "http://m.wufazhuce.com/one/ajaxlist/0?_token=" + token
const req2 = new Request(API2)
const res2 = await req2.loadJSON()
const data = res2["data"]
console.log('arg====')
return data ? data[this.arg] : false
}
async getImage (url) {
let r = new Request(url)
return await r.loadImage()
}
async shadowImage (img) {
let ctx = new DrawContext()
// 获取图片的尺寸
ctx.size = img.size
ctx.drawImageInRect(img, new Rect(0, 0, img.size['width'], img.size['height']))
ctx.setFillColor(new Color("#000000", 0.7))
ctx.fillRect(new Rect(0, 0, img.size['width'], img.size['height']))
let res = await ctx.getImage()
return res
}
/**
* 用户传递的组件自定义点击操作
*/
async runActions () {
let { act, data } = this.parseQuery()
if (!act) return
if (act === 'open-url') {
Safari.openInApp(data, false)
}
}
// 获取跳转自身 urlscheme
// w.url = this.getURIScheme("copy", "data-to-copy")
getURIScheme (act, data) {
let _raw = typeof data === 'object' ? JSON.stringify(data) : data
let _data = Data.fromString(_raw)
let _b64 = _data.toBase64String()
return `scriptable:///run?scriptName=${encodeURIComponent(Script.name())}&act=${act}&data=${_b64}&__widget__=${encodeURIComponent(args['widgetParameter'])}`
}
// 解析 urlscheme 参数
// { act: "copy", data: "copy" }
parseQuery () {
const { act, data } = args['queryParameters']
if (!act) return { act }
let _data = Data.fromBase64String(data)
let _raw = _data.toRawString()
let result = _raw
try {
result = JSON.parse(_raw)
} catch (e) {}
return {
act,
data: result
}
}
// 用于测试
async test () {
if (config.runsInWidget) return
let widget = await this.render()
widget.presentSmall()
}
// 单独运行
async init () {
if (!config.runsInWidget) return await this.runActions()
try {
this.arg = parseInt(args.widgetParameter)
if (!Number.isInteger(this.arg)) this.arg = 0
} catch (e) {}
let widget = await this.render()
Script.setWidget(widget)
Script.complete()
}
}
module.exports = Im3xWidget
// test
// await new Im3xWidget().test()
// init
// await new Im3xWidget(0, true).init()