-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimeResolutionLister.user.js
More file actions
327 lines (297 loc) · 10.9 KB
/
animeResolutionLister.user.js
File metadata and controls
327 lines (297 loc) · 10.9 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// ==UserScript==
// @name 動畫瘋解析度顯示器
// @description 不用開始播放就可以知道該部番劇能播放的解析度!註:必須要有動畫瘋會員才能顯示完整列表
// @namespace https://github.com/sunafterrainwm/animad-userscript
// @supportURL https://github.com/sunafterrainwm/animad-userscript/issues
// @downloadURL https://github.com/sunafterrainwm/animad-userscript/raw/master/animeResolutionLister.user.js
// @updateURL https://github.com/sunafterrainwm/animad-userscript/raw/master/animeResolutionLister.user.js
// @version 2024-10-12.01
// @author sunafterrainwm
// @licence (C) 2024 sunafterrainwm; BSD 3-Clause; https://opensource.org/license/bsd-3-clause
// @match https://ani.gamer.com.tw/animeVideo.php?*
// @icon https://ani.gamer.com.tw/apple-touch-icon-72.jpg
// @require https://unpkg.com/xhook@1.6.2/dist/xhook.min.js
// @require https://cdn.jsdelivr.net/npm/m3u8-parser@4.2.0/dist/m3u8-parser.min.js
// @grant none
// @run-at document-end
// ==/UserScript==
/* global $, xhook, m3u8Parser */
(function () {
'use strict';
const ResolutionListerLoadFromPage = true; // 盡可能從頁面中攔截所需參數而非調用Api
const thisUrl = new URL(window.location.href);
let videoSn = thisUrl.searchParams.get('sn');
let deviceid;
const hookContainers = {};
const hook = (name) => {
hookContainers[name] ??= (() => {
const hooks = [];
let tmp = null;
const obj = {
hooks,
tmp,
add(func) {
if (tmp) {
func(tmp);
}
hooks.push(func);
},
once(func) {
if (tmp) {
func(tmp);
return;
}
const _func = (data) => {
obj.remove(_func);
func(data);
};
hooks.push(_func);
},
remove(func) {
const index = hooks.indexOf(func);
if (index === -1) {
return false;
}
hooks.splice(index, 1);
return true;
},
fire(data) {
tmp = data;
for (const func of hooks) {
func(data);
}
},
clear() {
tmp = null;
}
}
return obj;
})();
return hookContainers[name];
};
async function setTimeoutAsync(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
let animeDataProvided = window.animefun;
async function loadSimpleBangumiResolution() {
if (animeDataProvided && ResolutionListerLoadFromPage) {
return animeDataProvided.quality;
}
const apiResult = await fetch('https://api.gamer.com.tw/mobile_app/anime/v4/video.php?' + new URLSearchParams({
sn: videoSn
}))
.then((res) => res.json());
if (apiResult.error) {
return false;
}
return apiResult.quality;
}
function loadDeviceidFromCache() {
deviceid = window.localStorage.getItem('ANIME_deviceid');
}
let cacheDeviceid;
async function loadDeviceid() {
if (ResolutionListerLoadFromPage) {
return true;
}
if (cacheDeviceid) {
return cacheDeviceid;
}
const apiResult = await fetch('/ajax/getdeviceid.php?' + new URLSearchParams({
id: deviceid
}))
.then((res) => res.json());
if (apiResult.deviceid) {
cacheDeviceid = deviceid = apiResult.deviceid;
window.localStorage.setItem('ANIME_deviceid', deviceid);
return true;
} else {
console.error(apiResult);
return false;
}
}
let cacheTokenData;
async function checkUserIsVip() {
if (!window.User.Login.isLogin()) {
return false;
}
if (ResolutionListerLoadFromPage && !cacheTokenData) {
await Promise.race([
new Promise((resolve) => {
hook('onAjaxGetToken').once((data) => {
cacheTokenData = data;
resolve();
});
}),
setTimeoutAsync(3000)
]);
}
if (cacheTokenData) {
return cacheTokenData.error ? false : cacheTokenData.vip;
}
const apiResult = await fetch('/ajax/token.php?' + new URLSearchParams({
adID: '0',
sn: videoSn,
device: deviceid,
hash: Math.random().toString(16).slice(2, 14).padStart(12, '0')
}))
.then((res) => res.json());
if (apiResult.error) {
console.error(apiResult);
return false;
}
cacheTokenData = apiResult;
return apiResult.vip;
}
async function getM3U8Url() {
if (ResolutionListerLoadFromPage) {
let prevM3U8Url;
hook('onAjaxGetM3U8Url').once((m3u8Url) => {
prevM3U8Url = m3u8Url;
});
if (prevM3U8Url) {
return prevM3U8Url;
}
}
const apiResult = await fetch('/ajax/m3u8.php?' + new URLSearchParams({
sn: videoSn,
device: deviceid
}))
.then((res) => res.json());
if (apiResult.error) {
throw new Error(apiResult.error.message ?? JSON.stringify(apiResult));
}
return apiResult.src;
}
async function loadAndParseM3u8(src) {
let prevM3U8List = null;
// xhook 攔截不到 videojs 的 m3u8 請求
// hook('onAjaxGetM3U8List').once((m3u8List) => {
// prevM3U8List = m3u8List;
// });
const m3u8List = prevM3U8List ?? await fetch(src)
.then((res) => res.text());
return parseM3u8(m3u8List);
}
const resolution1609 = {
360: 640, // 640x360
540: 960, // 960x540
720: 1280, // 1280x720
1080: 1920, // 1920x1080
};
function parseM3u8(m3u8List) {
const parser = new m3u8Parser.Parser();
parser.push(m3u8List);
parser.end();
const resultList = [];
for (const pl of parser.manifest.playlists) {
const resolution = pl.attributes.RESOLUTION;
if (!resolution) {
continue;
}
if (resolution1609[resolution.height] === resolution.width) {
resultList.push(`${resolution.height}p`);
} else {
resultList.push(`${resolution.height}p (${resolution.width}x${resolution.height})`);
}
}
return resultList;
}
if (ResolutionListerLoadFromPage) {
xhook.after(async function (request, response) {
if (!response.ok) {
return;
}
let url = new URL(request.url, window.location.origin);
if (url.hostname === 'ani.gamer.com.tw') {
if (url.pathname === '/ajax/getdeviceid.php') {
setTimeout(loadDeviceidFromCache, 1);
} else if (url.pathname === '/ajax/token.php') {
if (response.status === 200) {
const clonedResponse = response.clone();
const json = await clonedResponse.json();
hook('onAjaxGetToken').fire(json);
}
} else if (url.pathname === '/ajax/m3u8.php') {
if (response.status === 200) {
const clonedResponse = response.clone();
const json = await clonedResponse.json();
hook('onAjaxGetM3U8Url').fire(json.src);
}
}
} else if (url.hostname === 'api.gamer.com.tw') {
if (
url.pathname === '/anime/v1/video.php'
&& url.searchParams.get('videoSn') !== videoSn
) {
const clonedResponse = response.clone();
const json = await clonedResponse.json();
if (json.data.video) {
animeDataProvided = json.data.video;
videoSn = animeDataProvided.videoSn;
init();
}
}
// xhook 攔截不到 videojs 的 m3u8 請求
// } else if (url.hostname === 'bahamut.akamaized.net' && url.pathname.endsWith('.m3u8')) {
// const clonedResponse = response.clone();
// const m3u8List = await clonedResponse.text();
// hook('onAjaxGetM3U8List').fire(m3u8List);
}
});
}
let $container;
async function init() {
if (!$('#video-container').length) {
return;
}
$container?.remove();
$container = null;
hook('onAjaxGetM3U8Url').clear();
$('.anime-option').css('width', '100%');
let simpleResolution = await loadSimpleBangumiResolution();
if (simpleResolution === 'FHD') {
simpleResolution = '1080p';
} else if (simpleResolution === '') {
simpleResolution = '🗑未知';
} else if (simpleResolution === false) {
return;
}
const $resolutions = $('<span>');
function displayResolutions(resultList) {
$resolutions.empty().text('提供以下解析度:' + resultList.join('、'));
}
loadDeviceidFromCache();
await loadDeviceid();
const isVip = await checkUserIsVip();
$container = $('<span>')
.text('|')
.append(
$resolutions
.append(
$('<span>').text('上限解析度:' + simpleResolution)
)
);
$('.anime_info_detail').append($container);
if (isVip) {
$resolutions.append(
$('<span>').text('|'),
$('<a>')
.attr({
href: ''
})
.css({
color: 'black'
})
.text('點此查看可觀看的所有解析度')
.on('click', (e) => {
e.preventDefault();
getM3U8Url().then(loadAndParseM3u8).then(displayResolutions).catch((error) => alert('獲取解析度失敗:' + String(error)));
})
);
}
}
init();
})();