-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.user.js
295 lines (270 loc) · 9.97 KB
/
script.user.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// ==UserScript==
// @name SimDevices Osu Beatmap Downloader Plugin
// @name:zh-CN SimDevices Osu 谱面下载器插件
// @include http*://osu.ppy.sh/*
// @copyright 2020, Handle
// @version 0.5.2
// @description Add extra download buttons on beatmap page for SimDevices Beatmap Downloader on osu.ppy.sh
// @description:zh-CN 在 osu! 谱面下载页面上添加额外的按钮,可以唤醒下载器自动下载并导入谱面。
// @author Handle
// @namespace https://github.com/SimDevices-Project
// @supportURL https://github.com/SimDevices-Project/beatmap-downloader-user-script/issues
// @grant none
// ==/UserScript==
;(function () {
'use strict'
const styleClassName = '_beatmap_downloader_style_'
const beapmapPageDownloadBtnClassName = '_beatmap_downloader_btn_'
const searchPageBtnClassName = `_beatmap_downloader_quick_btn_`
const searchPageBtnText = 'Launch Downloader'
const searchPageDownloadTipClassName = `_beatmap_downloader_qtip_`
const insertStyle = () => {
const styleDOM = document.createElement('style')
styleDOM.classList.add(styleClassName)
styleDOM.innerHTML = `
.${searchPageBtnClassName}:hover {
text-decoration: none!important;
}
.${searchPageDownloadTipClassName}:after{
display:block;
content:'';
border-width:8px 5px 8px 5px;
border-style:solid;
border-color:hsl(var(--base-hue),10%,10%) transparent transparent transparent;
position:absolute;
left:calc(50% - 2px);
top:100%;
}
`
if (!document.querySelector(`.${styleClassName}`)) {
document.head.append(styleDOM)
}
}
const formatURL = () => {
const URL = document.URL
const [domain, argument] = URL.substr(URL.indexOf('//') + 2).split('#')
const pathname = window.location.pathname.substr(1)
const [type, sid] = pathname.split('/')
switch (type) {
case 'beatmapsets':
if (sid && sid.length) {
const [mode, bid] = argument || []
return {
id: sid,
type: 'beatmapset',
}
} else {
return {
id: 0,
type: 'beatmapsearch',
}
}
break
default:
return { id: 0, type: type }
}
}
const insertBeatmapPageDownloadBtn = (id = 1011011, type = 's') => {
const htmlText = `
<a href="beatmap-downloader://${type}/${id}" class="btn-osu-big btn-osu-big--beatmapset-header ${beapmapPageDownloadBtnClassName}">
<span class="btn-osu-big__content">
<span class="btn-osu-big__left">
<span class="btn-osu-big__text-top">启动</span>
<span class="btn-osu-hint btn-osu-big__text-bottom">Beatmap Downloader</span>
</span>
<span class="btn-osu-big__icon">
<span class="fa-fw">
<i class="fas fa-download"></i>
</span>
</span>
</span>
</a>`
const htmlDOM = document.createRange().createContextualFragment(htmlText)
const btnContainer = document.querySelector('.beatmapset-header__buttons')
const downloaderBtnQueryWith = `.${beapmapPageDownloadBtnClassName}`
const downloaderBtnDOM = document.querySelector(downloaderBtnQueryWith)
if (!downloaderBtnDOM) {
btnContainer.insertBefore(htmlDOM, btnContainer.lastElementChild)
}
}
const getSearchPageDownloadBtn = (id = 1011011, type = 's') => {
const htmlText = `
<a href="beatmap-downloader://${type}/${id}" class="beatmapset-panel__icon ${searchPageBtnClassName}" data-orig-title="Launch Downloader" aria-describedby="qtip-downloader">
<i class="fas fa-lg fa-gamepad"></i>
</a>`
/**
* @type {HTMLAnchorElement}
*/
const htmlDOM = document.createRange().createContextualFragment(htmlText)
return htmlDOM
}
const insertSearchPageDownloadTip = () => {
const insertHTML = `
<div
id="qtip-downloader"
class="qtip qtip-default tooltip-default qtip-pos-bc ${searchPageDownloadTipClassName}"
tracking="false"
role="alert"
aria-live="polite"
aria-atomic="false"
aria-describedby="qtip-downloader-content"
aria-hidden="true"
data-qtip-id="downloader"
style="z-index: 15003;"
>
<div class="qtip-content" id="qtip-downloader-content" aria-atomic="true">
<span style="display: block; visibility: visible;">${searchPageBtnText}</span>
</div>
</div>`
/**
* @type {HTMLDivElement}
*/
const htmlDOM = document.createRange().createContextualFragment(insertHTML)
const qTipQueryWith = `.${searchPageDownloadTipClassName}`
const qTipDOM = document.querySelector(qTipQueryWith)
if (!qTipDOM) {
document.body.appendChild(htmlDOM)
}
}
/**
* 获取DOM元素绝对坐标
* @param {HTMLElement} element 要获取坐标的元素
*/
const getAbsolutePostion = (element) => {
const rect = element.getBoundingClientRect()
const X = rect.left + document.documentElement.scrollLeft
const Y = rect.top + document.documentElement.scrollTop
const width = rect.width
const height = rect.height
return {
x: X,
y: Y,
width,
height,
}
}
const setSearchPageDownloadTipPosition = ({ x = 0, y = 0, show = true } = {}) => {
const qTipQueryWith = `.${searchPageDownloadTipClassName}`
/**
* @type {HTMLDivElement}
*/
const qTipDOM = document.querySelector(qTipQueryWith)
if (!qTipDOM) {
return
}
if (!show) {
qTipDOM.style.display = 'none'
qTipDOM.style.opacity = 0
} else {
if (qTipDOM.style.display === 'block') {
} else {
qTipDOM.style.display = 'block'
let opacitySet = 0
if (qTipDOM.dataset.timer) {
clearInterval(qTipDOM.dataset.timer)
}
qTipDOM.style.opacity = opacitySet.toString(10)
const easeIn = setInterval(() => {
opacitySet += 0.2
if (opacitySet >= 1) {
opacitySet = 1
clearInterval(easeIn)
}
qTipDOM.style.opacity = opacitySet.toString(10)
}, 16.67)
qTipDOM.dataset.timer = easeIn
}
}
qTipDOM.style.left = `${x}px`
qTipDOM.style.top = `${y}px`
}
const insertSearchPageDownloadBtns = (target = document) => {
/**
* @type {NodeListOf<HTMLDivElement>}
*/
const beatmapsetPannels = target.querySelectorAll('.beatmapset-panel')
const addDownloadBtnToPannel = (beatmapsetPannel) => {
if (beatmapsetPannel.querySelector(`.${searchPageBtnClassName}`)) {
return
}
const audioURL = beatmapsetPannel.dataset.audioUrl
const anchorLinkDOM = beatmapsetPannel.querySelectorAll('a')[0]
const [domain, type, sid] = anchorLinkDOM.href.substr(anchorLinkDOM.href.indexOf('//') + 2).split('/')
if (type !== 'beatmapsets') {
return
}
const downloadBtn = getSearchPageDownloadBtn(sid, 's')
const iconBox = beatmapsetPannel.querySelector('.beatmapset-panel__icons-box')
iconBox.insertBefore(downloadBtn, iconBox.lastElementChild)
const downloadBtnToBind = iconBox.querySelector(`.${searchPageBtnClassName}`)
let tipWidth = 0
let tipHeight = 0
const showTip = () => {
const { x, y, width, height } = getAbsolutePostion(downloadBtnToBind)
setSearchPageDownloadTipPosition({ x: x + width / 2 - tipWidth / 2, y: y - tipHeight - 8, show: true })
if (!tipWidth || !tipHeight) {
const qTipQueryWith = `.${searchPageDownloadTipClassName}`
const qTipDOM = document.querySelector(qTipQueryWith)
if (!qTipDOM) {
return
}
const rect = getAbsolutePostion(qTipDOM)
tipWidth = rect.width
tipHeight = rect.height
showTip()
}
}
const hideTip = () => {
setSearchPageDownloadTipPosition({ show: false })
}
downloadBtnToBind.addEventListener('mousemove', showTip)
downloadBtnToBind.addEventListener('mouseover', showTip)
downloadBtnToBind.addEventListener('mouseenter', showTip)
downloadBtnToBind.addEventListener('mouseleave', hideTip)
downloadBtnToBind.addEventListener('mouseout', hideTip)
}
beatmapsetPannels.forEach(addDownloadBtnToPannel)
}
let timer = 0
const loader = () => {
const listenElementBeapmapPageQueryWith = '.js-react--beatmapset-page'
const listenElementBeatmapPageDOM = document.querySelector(listenElementBeapmapPageQueryWith)
const listenElementBeapmapSearchQueryWith = '.js-react--beatmaps'
const listenElementBeatmapSearchDOM = document.querySelector(listenElementBeapmapSearchQueryWith)
insertStyle()
if (listenElementBeatmapPageDOM && listenElementBeatmapPageDOM.dataset.reactTurbolinksLoaded === '1') {
// 谱面详情
const formated = formatURL()
if (formated.type === 'beatmapset') {
insertBeatmapPageDownloadBtn(formated.id, formated.type === 'beatmapset' ? 's' : 'b')
}
} else if (listenElementBeatmapSearchDOM && listenElementBeatmapSearchDOM.dataset.reactTurbolinksLoaded === '1') {
// 搜索页面
const formated = formatURL()
if (formated.type === 'beatmapsearch') {
insertSearchPageDownloadTip()
const options = {
childList: true,
}
const observer = new MutationObserver((mutationsList) => {
mutationsList.forEach((mutation) => {
switch (mutation.type) {
case 'childList':
if (mutation.addedNodes) {
mutation.addedNodes.forEach(insertSearchPageDownloadBtns)
}
break
}
})
})
observer.observe(document.querySelector('.beatmapsets__items'), options)
}
} else {
timer = requestAnimationFrame(loader)
}
}
timer = requestAnimationFrame(loader)
// 监听 turbolinks 渲染事件
// https://greasyfork.org/zh-CN/scripts/3916-osu-my-download
document.addEventListener('turbolinks:load', loader)
})()