forked from Kaiser-DMr/Scriptables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfans.js
146 lines (127 loc) · 3.42 KB
/
fans.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
// 哔哩哔哩粉丝数
// 作者:azoon
// 调用参数 bilibili@fans:446791792
class Im3xWidget {
/**
* 初始化
* @param arg 外部传递过来的参数
*/
constructor (arg) {
this.arg = arg
this.widgetSize = config.widgetFamily
}
//渲染组件
async render () {
if (this.widgetSize === 'medium') {
return await this.renderSmall()
} else if (this.widgetSize === 'large') {
return await this.renderLarge()
} else {
return await this.renderSmall()
}
}
//渲染小尺寸组件
async renderSmall () {
let data = await this.getData()
let w = new ListWidget()
let header = w.addStack()
let icon = header.addImage(await this.getImage('https://www.bilibili.com/favicon.ico'))
icon.imageSize = new Size(15, 15)
header.addSpacer(10)
let title = header.addText("哔哩哔哩粉丝")
title.textOpacity = 0.9
title.font = Font.systemFont(14)
w.addSpacer(20)
if(data.code !=0){
var flTxt = w.addText('请填写B站MID')
flTxt.textColor = new Color("#fb7299")
flTxt.font = Font.systemFont(14)
}else{
var flTxt = w.addText(this.toThousands(data.data['follower']))
flTxt.textColor = new Color("#fb7299")
flTxt.font = Font.boldRoundedSystemFont(this.getFontsize(data.data['follower']))
}
flTxt.centerAlignText()
w.addSpacer(20)
let utTxt = w.addText('更新于:'+this.nowTime())
utTxt.font = Font.systemFont(12)
utTxt.centerAlignText()
utTxt.textOpacity = 0.5
w.url = 'bilibili://'
return w
}
//渲染中尺寸组件
async renderMedium () {
let w = new ListWidget()
w.addText("暂不支持该尺寸组件")
return w
}
//渲染大尺寸组件
async renderLarge () {
let w = new ListWidget()
w.addText("暂不支持该尺寸组件")
return w
}
//加载B站数据
async getData () {
let api = 'http://api.bilibili.com/x/relation/stat?vmid='+this.arg
let req = new Request(api)
let res = await req.loadJSON()
return res
}
//加载远程图片
async getImage (url) {
let req = new Request(url)
return await req.loadImage()
}
//格式化粉丝数量,加入千分号
toThousands(num) {
return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}
//返回脚本运行时的时间,作为更新时间
nowTime(){
let date = new Date()
return date.toLocaleTimeString('chinese', { hour12: false })
}
//根据粉丝数量返回不同的字体大小
getFontsize(num){
if(num<99){
return 38
}else if(num<9999 && num>100){
return 30
}else if(num<99999 && num>10000){
return 28
}else if(num<999999 && num>100000){
return 24
}else if(num<9999999 && num>1000000){
return 22
}else{
return 20
}
}
//编辑测试使用
async test(){
if (config.runsInWidget) return
this.widgetSize = 'small'
let w1 = await this.render()
await w1.presentSmall()
this.widgetSize = 'medium'
let w2 = await this.render()
await w2.presentMedium()
this.widgetSize = 'large'
let w3 = await this.render()
await w3.presentLarge()
}
//组件单独在桌面运行时调用
async init(){
if (!config.runsInWidget) return
let widget = await this.render()
Script.setWidget(widget)
Script.complete()
}
}
module.exports = Im3xWidget
// 如果是在编辑器内编辑、运行、测试,则取消注释这行,便于调试:
//await new Im3xWidget().test()
// 如果是组件单独使用(桌面配置选择这个组件使用,则取消注释这一行:
//await new Im3xWidget(args.widgetParameter).init()