/
amq-accelerate-loading.ts
87 lines (73 loc) · 2.49 KB
/
amq-accelerate-loading.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
import { onReady } from '../lib/amq/onReady'
declare function swal(message: Record<string, unknown>): Promise<void>
declare const popoutMessages: {
displayStandardMessage(header: string, message: string): void
displayPopoutMessage(htmlBody: string, force?: boolean, onDisplay?: () => void): void
}
declare class MoeVideoPlayer {
public getNextVideoId(): string | undefined
}
declare class ExpandQuestionBox {
public showVideoPreview(url: string | undefined, ...args: unknown[]): void
}
const proxyHost = 'https://amq-proxy.starry.blue'
async function checkSession(): Promise<boolean> {
try {
const response = await fetch(`${proxyHost}/healthcheck`, {
redirect: 'error',
mode: 'cors',
credentials: 'include',
})
return response.ok
} catch (e: unknown) {
return false
}
}
function replaceUrl(url: string): string {
return `${proxyHost}/api/media?u=${encodeURIComponent(url)}`
}
onReady(async () => {
const isAuthenticated = await checkSession()
if (!isAuthenticated) {
// すぐにアラートを出すとなぜか閉じられてしまうので遅延する
setTimeout(() => {
void swal({
title: 'AMQ Accelerate Loading',
text: 'この UserScript を使用するにはユーザ認証が必要です。認証後、AMQ に再度ログインしてください。',
confirmButtonText: '認証',
showCancelButton: true,
cancelButtonText: 'キャンセル',
allowOutsideClick: false,
focusConfirm: true,
showCloseButton: false,
allowEscapeKey: false,
}).then(() => {
window.location.replace(proxyHost)
})
}, 5000)
return
} else {
popoutMessages.displayStandardMessage('AMQ Accelerate Loading', '認証成功! UserScript は正常に動作しています。')
}
const { getNextVideoId } = MoeVideoPlayer.prototype
MoeVideoPlayer.prototype.getNextVideoId = function () {
const url = getNextVideoId.apply(this)
if (!url) {
return undefined
}
return replaceUrl(url)
}
const { showVideoPreview } = ExpandQuestionBox.prototype
ExpandQuestionBox.prototype.showVideoPreview = function (url, ...args) {
if (url) {
showVideoPreview.apply(this, [replaceUrl(url), ...args])
} else {
showVideoPreview.apply(this, [url, ...args])
}
}
AMQ_addScriptData({
name: '[Closed Test] Accelerate Loading',
author: 'SlashNephy <spica@starry.blue>',
description: 'Load media faster from alternative sources.',
})
})