-
Notifications
You must be signed in to change notification settings - Fork 244
/
「源码」蚂蚁庄园.js
120 lines (110 loc) · 3.61 KB
/
「源码」蚂蚁庄园.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: orange; icon-glyph: comments;
//
// iOS 桌面组件脚本 @「小件件」
// 开发说明:请从 Widget 类开始编写,注释请勿修改
// https://x.im3x.cn
//
// 添加require,是为了vscode中可以正确引入包,以获得自动补全等功能
if (typeof require === 'undefined') require = importModule
const { Base } = require("./「小件件」开发环境")
// @组件代码开始
class Widget extends Base {
url = 'https://www.youxi369.com/news/2254_4.html'
/**
* 传递给组件的参数,可以是桌面 Parameter 数据,也可以是外部如 URLScheme 等传递的数据
* @param {string} arg 自定义参数
*/
constructor (arg) {
super(arg)
this.name = '蚂蚁庄园'
this.desc = '今天的题会做么?'
}
/**
* 渲染函数,函数名固定
* 可以根据 this.widgetFamily 来判断小组件尺寸,以返回不同大小的内容
*/
async render () {
const data = await this.getData()
switch (this.widgetFamily) {
case 'large':
return await this.renderLarge(data)
case 'medium':
return await this.renderMedium(data)
default:
return await this.renderMedium(data)
}
}
/**
* 渲染中尺寸组件
*/
async renderMedium (data, num = 4, title = false) {
let w = new ListWidget()
// await this.renderHeader(w, data['logo'], data['title'])
data.slice(0, num * 2).map((d, idx) => {
if (!title && idx % 2 === 0) return;
const cell = w.addStack()
cell.centerAlignContent()
const cell_text = cell.addText(d)
cell_text.font = Font.lightSystemFont(16)
cell.addSpacer()
w.addSpacer(10)
})
w.url = this.actionUrl(this.url)
w.addSpacer()
return w
}
/**
* 渲染大尺寸组件
*/
async renderLarge (data) {
return await this.renderMedium(data, 5, true)
}
/**
* 获取数据函数,函数名可不固定
*/
async getData () {
const html = await this.fetchAPI(this.url, false);
const tmp = html.split(`<p style="display:none">mryt</p>`)[1].split(`<div class="more-strategy"><i></i></div>`)[0];
const arr = tmp.split(`</span></p>`).slice(0, 20);
const result = [];
for (const answer of arr) {
// console.log(`====answer==`, answer);
if (!answer) continue;
const text = answer.replace(/<p>/, '').replace(/\ /gi, ' ').replace(/<span style="[^"]+">/gi, '').replace(/小鸡宝宝考考你[,,]?/, '').trim();
const [ title, an ] = text.split('答案:');
if (!title) continue;
const [ day, t] = title.split('月')[1].split(':');
result.push(t);
result.push(`答案:${an} (${day})`);
}
return result;
}
// http.get
async fetchAPI (api, json = true) {
let data = null
try {
let req = new Request(api)
req.headers = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/85.0.4183.102'
}
data = await (json ? req.loadJSON() : req.loadString())
} catch (e) {}
// 判断数据是否为空(加载失败)
if (!data) {
return null
}
return data
}
/**
* 自定义注册点击事件,用 actionUrl 生成一个触发链接,点击后会执行下方对应的 action
* @param {string} url 打开的链接
*/
async actionOpenUrl (url) {
Safari.openInApp(url, false)
}
}
// @组件代码结束
const { Testing } = require("./「小件件」开发环境")
await Testing(Widget)