/
bilibili-evolved.js
300 lines (296 loc) · 9.3 KB
/
bilibili-evolved.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
296
297
298
299
300
// ==UserScript==
// @name Bilibili Evolved (Preview)
// @version 1.10.1
// @description Bilibili Evolved 的预览版, 可以抢先体验新功能.
// @author Grant Howard, Coulomb-G
// @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G)
// @license MIT
// @match *://*.bilibili.com/*
// @run-at document-start
// @updateURL https://cdn.jsdelivr.net/gh/the1812/Bilibili-Evolved@preview/bilibili-evolved.preview.user.js
// @downloadURL https://cdn.jsdelivr.net/gh/the1812/Bilibili-Evolved@preview/bilibili-evolved.preview.user.js
// @supportURL https://github.com/the1812/Bilibili-Evolved/issues
// @homepage https://github.com/the1812/Bilibili-Evolved
// @grant unsafeWindow
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_setClipboard
// @grant GM_info
// @grant GM_xmlhttpRequest
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.setClipboard
// @grant GM.info
// @grant GM.xmlHttpRequest
// @connect raw.githubusercontent.com
// @connect cdn.jsdelivr.net
// @connect *
// @require https://code.jquery.com/jquery-3.4.0.min.js
// @require https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js
// @require https://cdn.bootcss.com/jszip/3.1.5/jszip.min.js
// @require https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js
// @require https://unpkg.com/vuex@3.1.2/dist/vuex.js
// @icon https://cdn.jsdelivr.net/gh/the1812/Bilibili-Evolved@preview/images/logo-small.png
// @icon64 https://cdn.jsdelivr.net/gh/the1812/Bilibili-Evolved@preview/images/logo.png
// ==/UserScript==
Vue.config.productionTip = false
Vue.config.devtools = false
// if (unsafeWindow.Vue === undefined) {
// unsafeWindow.Vue = Vue
// }
// GM4 polyfill start
if (typeof GM == 'undefined') {
this.GM = {}
}
Object.entries({
'log': console.log.bind(console),
'info': GM_info,
}).forEach(([newKey, old]) => {
if (old && (typeof GM[newKey] == 'undefined')) {
GM[newKey] = old
}
})
Object.entries({
'GM_getValue': 'getValue',
'GM_setClipboard': 'setClipboard',
'GM_setValue': 'setValue',
'GM_xmlhttpRequest': 'xmlHttpRequest',
}).forEach(([oldKey, newKey]) => {
let old = this[oldKey]
if (old && (typeof GM[newKey] == 'undefined')) {
GM[newKey] = function (...args) {
return new Promise((resolve, reject) => {
try {
resolve(old.apply(this, args))
} catch (e) {
reject(e)
}
})
}
}
})
// GM4 polyfill end
import { logError, raiseEvent, loadLazyPanel, contentLoaded, fixed, isOffline, getUID, scriptVersion, getCsrf, formatCount, escapeFilename } from './utils'
import { settings, loadSettings, settingsChangeHandlers } from './settings'
import { Ajax, setupAjaxHook } from './ajax'
import { loadResources } from './resource-loader'
import { Toast } from './toast-holder'
import { DoubleClickEvent } from './double-click-event'
import { Observer } from './observer'
import { SpinQuery } from './spin-query'
import { ColorProcessor } from './color-processor'
// [Offline build placeholder]
import { ResourceType } from './resource-type'
import { Resource } from './resource'
import { resourceManifest } from './resource-manifest'
import { StyleManager } from './style-manager'
import { ResourceManager } from './resource-manager'
import { getScriptBlocker } from './script-blocker'
import { installStyle, uninstallStyle } from './custom-styles'
import { store } from './store'
;(async () => {
const redundantFrames = [
'https://message.bilibili.com/pages/nav/index_new_sync',
'https://message.bilibili.com/pages/nav/index_new_pc_sync',
'https://t.bilibili.com/h5/dynamic/specification',
]
if (await GM.getValue('customNavbar') === true
&& redundantFrames.includes(document.URL)) {
if (await GM.getValue('useDarkStyle') === true) {
document.documentElement.style.setProperty('--theme-color', await GM.getValue('customStyleColor'))
if (typeof offlineData === 'undefined') {
const cache = await GM.getValue('cache', {})
if ('darkStyle' in cache) {
const style = document.createElement('style')
style.innerHTML = cache.darkStyle
style.id = 'dark-style'
document.head.insertAdjacentElement('afterbegin', style)
}
} else {
const style = document.createElement('style')
style.innerHTML = Object.entries(offlineData).find(([key]) => {
return key.includes('/dark.min.css')
})[1]
style.id = 'dark-style'
document.head.insertAdjacentElement('afterbegin', style)
}
}
console.log(`Skipped <iframe> loading for ${document.URL}`)
return
}
try {
const events = {}
for (const name of ['init', 'styleLoaded', 'scriptLoaded']) {
events[name] = {
completed: false,
subscribers: [],
complete () {
this.completed = true
this.subscribers.forEach(it => it())
}
}
}
if (unsafeWindow.bilibiliEvolved === undefined) {
unsafeWindow.bilibiliEvolved = { addons: [] }
}
Object.assign(unsafeWindow.bilibiliEvolved, {
subscribe (type, callback) {
const event = events[type]
if (callback) {
if (event && !event.completed) {
event.subscribers.push(callback)
} else {
callback()
}
} else {
return new Promise((resolve) => this.subscribe(type, () => resolve()))
}
}
})
contentLoaded(() => {
document.body.classList.add('round-corner')
})
loadResources()
await loadSettings()
getScriptBlocker().then(scriptBlocker => {
scriptBlocker.start()
})
if (settings.ajaxHook) {
setupAjaxHook()
}
const resources = new ResourceManager()
events.init.complete()
resources.styleManager.prefetchStyles()
// if (settings.customNavbar) {
// contentLoaded(() => {
// document.body.classList.add('custom-navbar-loading')
// if (settings.useDarkStyle) {
// document.body.classList.add('dark')
// }
// })
// }
events.styleLoaded.complete()
Object.assign(unsafeWindow.bilibiliEvolved, {
SpinQuery,
Toast,
Observer,
DoubleClickEvent,
ColorProcessor,
StyleManager,
ResourceManager,
Resource,
ResourceType,
Ajax,
resourceManifest,
loadSettings,
logError,
raiseEvent,
loadLazyPanel,
contentLoaded,
fixed,
settings,
settingsChangeHandlers,
addSettingsListener,
removeSettingsListener,
isEmbeddedPlayer,
isIframe,
getI18nKey,
dq,
dqa,
UserAgent,
EmptyImageUrl,
ascendingSort,
descendingSort,
formatFileSize,
formatDuration,
getDpiSourceSet,
getScriptBlocker,
isOffline,
getUID,
scriptVersion,
getCsrf,
formatCount,
escapeFilename,
installStyle,
uninstallStyle,
store,
resources,
theWorld: waitTime => {
if (waitTime > 0) {
setTimeout(() => { debugger }, waitTime)
} else {
debugger
}
},
newHomePage: () => {
document.cookie = 'INTVER=1; domain=.bilibili.com; path=/; expires=Fri, 21 Aug 2020 02:13:04 GMT'
},
monkeyInfo: GM.info,
monkeyApis: {
getValue: GM.getValue,
setValue: GM.setValue,
setClipboard: GM.setClipboard,
xhr: GM.xmlHttpRequest,
addValueChangeListener: () => console.warn('此功能已弃用.')
}
})
const applyScripts = () => resources.fetch()
.then(() => {
events.scriptLoaded.complete()
const addons = new Proxy(unsafeWindow.bilibiliEvolved.addons || [], {
apply: function (target, thisArg, argumentsList) {
return thisArg[target].apply(this, argumentsList)
},
set: function (target, property, value) {
if (target[property] === undefined) {
resources.applyWidget(value)
}
target[property] = value
return true
}
})
addons.forEach(it => resources.applyWidget(it))
Object.assign(unsafeWindow.bilibiliEvolved, { addons })
})
.catch(error => logError(error))
const loadingMode = settings.scriptLoadingMode
switch (loadingMode) {
case '延后':
fullyLoaded(applyScripts)
break
case '同时':
contentLoaded(applyScripts)
break
case '自动':
case '延后(自动)':
{
const quickLoads = [
'//live.bilibili.com',
]
if (quickLoads.some(it => document.URL.includes(it))) {
contentLoaded(applyScripts)
} else {
fullyLoaded(applyScripts)
}
break
}
case '同时(自动)':
{
const delayLoads = [
'//www.bilibili.com/video/av',
'//www.bilibili.com/bangumi/play',
]
if (delayLoads.some(it => document.URL.includes(it))) {
fullyLoaded(applyScripts)
} else {
contentLoaded(applyScripts)
}
break
}
default:
break
}
} catch (error) {
logError(error)
}
})()