-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
127 lines (105 loc) · 2.87 KB
/
index.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
import { VERSION } from './version.js'
import DDAtHome from 'ddatnodejs'
const INTERVAL = 630
const wsLimit = 0
let logLimit = 1024
const info = (...message) => console.info('DD@Browser:', ...message)
const log = (...message) => {
if (logLimit <= 0) {
return
}
console.log('DD@Browser:', ...message)
logLimit--
if (logLimit === 0) {
info('log 太多, 不再显示')
}
}
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
const set = (key, value) => GM.setValue(key, value)
const get = (key, d) => GM.getValue(key, d)
const channel = new BroadcastChannel('DDSync')
const f = url => {
const options = {}
if (url.includes('bilibili.com')) { // PeroPero no credentials
options.credentials = 'include'
}
return fetch(url, options)
}
const getMID = async () => {
const { data: { isLogin, mid } } = await f('https://api.bilibili.com/x/web-interface/nav').then(r => r.json())
if (isLogin) {
return mid
}
return 0
}
let on = false
const midP = getMID()
const runtime = () => {
const uas = navigator.userAgent.split(' ')
const ua = uas.filter(a => a.includes('/')).map(a => a.split('/')).reduce((o, [k, v]) => {
o[k] = v
return o
}, {})
if (ua.Chromium) {
return `Chromium/${ua.Chromium}`
}
if (ua.Chrome) {
return `Chrome/${ua.Chrome}`
}
if (ua.Safari) {
return `Safari/${ua.Version}`
}
return uas[uas.length - 1]
}
const makeURL = async () => {
const url = new URL('wss://cluster.vtbs.moe')
url.searchParams.set('runtime', runtime())
url.searchParams.set('version', VERSION)
url.searchParams.set('platform', navigator.platform)
const uuid = localStorage.DDUUID || await get('uuid', String(Math.random()))
await set('uuid', uuid)
log('uuid', uuid)
url.searchParams.set('uuid', uuid)
const name = localStorage.DDName
if (name) {
url.searchParams.set('name', name)
}
return url
}
const getBUVID = () => {
const buvid3 = document.cookie.split('; ').find(c => c.startsWith('buvid3='))
if (buvid3) {
return buvid3.split('=')[1]
}
}
const open = async () => {
log('open')
const home = new DDAtHome(await makeURL(), { INTERVAL, wsLimit, WebSocket, customFetch: f, getBUVID, uid: await midP, liveInterval: INTERVAL * 2 })
home.on('log', log)
}
let timeout
channel.onmessage = async ({ data }) => {
if (data === 'wait') {
log('wait')
clearTimeout(timeout)
}
if (data === 'start' && on) {
channel.postMessage('wait')
}
}
const hi = async () => {
log('hi')
await wait(1000 * Math.random())
while (!on) {
timeout = setTimeout(() => {
on = true
channel.postMessage('wait')
open()
}, 1000 * 3)
channel.postMessage('start')
await wait(1000 * 10)
}
}
hi()
info(`你可以通过 localStorage.DDUUID = 'uuid' 来设置 UUID, 以便记录你的数据`)
info(`你可以通过 localStorage.DDName = '你的名字' 来设置展示的名字`)