forked from the1812/Bilibili-Evolved
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
160 lines (147 loc) · 3.61 KB
/
index.ts
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
import { ComponentMetadata, ComponentEntry } from '@/components/types'
import { playerAgent } from '@/components/video/player-agent'
import { videoChange } from '@/core/observer'
import { addComponentListener } from '@/core/settings'
import { allVideoUrls } from '@/core/utils/urls'
let intervalID
let updateFunc = none
const parseTime = (t: Date, options: any): string => {
let hours = t.getHours()
let minutes = t.getMinutes().toFixed()
let seconds = t.getSeconds().toFixed()
if (Number(minutes) < 10) {
minutes = minutes.padStart(2, '0')
}
if (Number(seconds) < 10) {
seconds = seconds.padStart(2, '0')
}
if (!options?.TFFormat && hours > 12) {
hours -= 12
}
const timeStr = `${hours}:${minutes}`
return options?.showSecond ? `${timeStr}:${seconds}` : timeStr
}
enum Position {
TR = '右上角',
TL = '左上角',
}
const entry: ComponentEntry = async ({ settings: { options }, metadata }) => {
const time = document.createElement('span')
time.id = 'cur-time'
const style = {
position: 'absolute',
top: '0',
right: '0',
zIndex: '10',
fontFamily: 'Arial',
fontSize: '24px',
fontWeight: 'bold',
margin: '1rem',
}
const dyStyle = {
color: options.color,
opacity: options.opacity,
fontFamily: options.fontFamily,
fontSize: `${options.fontSize}px`,
}
// 绑定样式
Object.keys(Object.assign(style, dyStyle)).forEach(key => {
time.style[key] = style[key]
})
// 绑定动态样式
Object.keys(dyStyle).forEach(key => {
addComponentListener(`${metadata.name}.${key}`, (value: string) => {
if (key === 'fontSize') {
time.style.fontSize = `${value}px`
} else {
time.style[key] = value
}
})
})
addComponentListener(
`${metadata.name}.position`,
(value: string) => {
if (value === Position.TR) {
time.style.left = ''
time.style.right = '0'
} else if (value === Position.TL) {
time.style.right = ''
time.style.left = '0'
}
},
true,
)
videoChange(async () => {
const video = await playerAgent.query.video.wrap()
video.appendChild(time)
})
updateFunc = () => {
const t = new Date()
time.innerHTML = parseTime(t, options)
}
intervalID = setInterval(updateFunc, 1000)
}
export const component: ComponentMetadata = {
name: 'videoCurTime',
author: {
name: 'FoundTheWOUT',
link: 'https://github.com/FoundTheWOUT',
},
description: {
'zh-CN': '在视频的右上角显示当前时间',
},
displayName: '视频内显示时间',
entry,
reload() {
document.getElementById('cur-time').style.visibility = ''
intervalID = setInterval(updateFunc, 1000)
},
unload() {
document.getElementById('cur-time').style.visibility = 'hidden'
clearInterval(intervalID)
},
tags: [componentsTags.video],
options: {
opacity: {
slider: {
max: 1,
min: 0,
step: 0.01,
},
defaultValue: 0.5,
displayName: '透明度',
},
color: {
color: true,
defaultValue: '#d1d5db',
displayName: '颜色',
},
showSecond: {
defaultValue: false,
displayName: '显示秒',
},
TFFormat: {
defaultValue: true,
displayName: '24小时制',
},
position: {
defaultValue: Position.TR,
displayName: '位置',
dropdownEnum: Position,
},
fontFamily: {
defaultValue: '',
displayName: '字体',
},
fontSize: {
defaultValue: 24,
displayName: '尺寸',
slider: {
min: 10,
max: 100,
step: 1,
},
},
},
urlInclude: allVideoUrls,
}