-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdouban-ranker.user.js
More file actions
376 lines (333 loc) · 14.8 KB
/
douban-ranker.user.js
File metadata and controls
376 lines (333 loc) · 14.8 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// ==UserScript==
// @name 豆瓣榜单助手·Douban-Ranker
// @namespace https://github.com/eddiehe99/douban-ranker
// @homepageURL https://douban-ranker.eddiehe.top
// @supportURL https://github.com/eddiehe99/douban-ranker/issues
// @updateURL https://douban-ranker.eddiehe.top/douban-ranker.user.js
// @downloadURL https://douban-ranker.eddiehe.top/douban-ranker.user.js
// @license MIT
// @version 0.4.3
// @description 在豆瓣电影、播客、音乐页面展示作品在不同榜单中的排名
// @author Eddie He
// @contributor CRonaldoWei
// @icon https://img3.doubanio.com/favicon.ico
// @match https://movie.douban.com/subject/*
// @match https://www.douban.com/podcast/*
// @match https://music.douban.com/subject/*
// @include https://movie.douban.com/*
// @include https://music.douban.com/*
// @include https://book.douban.com/*
// @include https://www.douban.com/podcast/*
// @connect *
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// ==/UserScript==
(() => {
'use strict';
console.log("脚本: 豆瓣榜单助手·Douban-Ranker--开始执行--GitHub: https://github.com/eddiehe99/douban-ranker");
// 配置常量
const CONFIG = {
movieRankUrl: "https://rank4douban.eddiehe.top/data.json",
podcastRankUrl: "https://xyzrank.eddiehe.top/full.json",
musicRankUrl: "https://hma.eddiehe.top/data.json",
top250Class: "top250",
top250CssUrl: "https://img1.doubanio.com/cuphead/movie-static/charts/top250.24c18.css",
toggleButtonId: "rank_toggle",
rankLabelClass: "rank-label rank-label-other",
rankLabelCssUrl: "https://img1.doubanio.com/cuphead/movie-static/subject/rank_label.dda40.css"
};
// 缓存样式是否已加载
let isTop250StyleInjected = false;
let isRankLabelStyleInjected = false;
/**
* 注入外部 Top 250 CSS 样式表
*/
function injectTop250Stylesheet() {
if (isTop250StyleInjected) return;
const existingLink = document.querySelector(`link[href*="${CONFIG.top250CssUrl}"]`);
if (!existingLink) {
const styleLink = document.createElement('link');
styleLink.rel = 'stylesheet';
styleLink.href = CONFIG.top250CssUrl;
document.head.appendChild(styleLink);
}
isTop250StyleInjected = true;
}
/**
* 注入外部 Rank Label CSS 样式表
*/
function injectRankLabelStylesheet() {
if (isRankLabelStyleInjected) return;
const existingLink = document.querySelector(`link[href*="${CONFIG.rankLabelCssUrl}"]`);
if (!existingLink) {
const styleLink = document.createElement('link');
styleLink.rel = 'stylesheet';
styleLink.href = CONFIG.rankLabelCssUrl;
document.head.appendChild(styleLink);
}
isRankLabelStyleInjected = true;
}
/**
* 创建排名组件
* @param {Object} options
* @param {string} options.prefix 前缀(如"TOP")
* @param {number} options.position 排名位置
* @param {string} options.title 榜单标题
* @param {string} options.shortTitle 简短标题
* @param {string} options.href 榜单链接
* @returns {string} HTML字符串
*/
function createTop250RankItem({ prefix = 'No.', position, title, shortTitle, href }) {
return [
`<div class="${CONFIG.top250Class}" style="display: inline-block;">`,
`<span class="top250-no">${prefix}${position}</span>`,
`<span class="top250-link">`,
`<a href="${href}" title="${title}" target="_blank">${shortTitle}</a>`,
`</span>`,
`</div> `,
].join('');
}
/**
* 移除指定链接的 loading 占位元素
*/
function removeProcessingItem(href) {
const items = document.querySelectorAll(`.${CONFIG.top250Class} a[href="${href}"]`);
items.forEach(item => {
const parent = item.closest(`.${CONFIG.top250Class}`);
if (parent) parent.remove();
});
}
/**
* 创建排名组件
* @param {Object} options
* @param {string} options.prefix 前缀(如"TOP")
* @param {number} options.position 排名位置
* @param {string} options.title 榜单标题
* @param {string} options.shortTitle 简短标题
* @param {string} options.href 榜单链接
* @returns {string} HTML字符串
*/
function createRankLabelRankItem({ prefix = 'No.', position, title, shortTitle, href }) {
return [
`<div class="rank-label rank-label-other" style="display: inline-block;">`,
`<span class="rank-label-no">`,
`<span>${prefix}${position}</span>`,
`</span>`,
`<span class="rank-label-link">`,
`<a href="${href}" title="${title}" target="_blank">${shortTitle}</a>`,
`</span>`,
`</div> `,
].join('');
}
/**
* 初始化展示逻辑
* @param {HTMLElement} container 插入位置
* @param {Array<string>} items 排名组件数组
*/
function renderRankListWithToggle(container, items) {
// 将 HTML 字符串转换为真实的 DOM 节点
const fragment = document.createDocumentFragment();
items.forEach(html => {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = html;
tempDiv.style.display = "inline-block";
fragment.appendChild(tempDiv.firstElementChild); // 取出真正的 div 元素
const space = document.createTextNode(' ');
fragment.appendChild(space);
});
// 在 container 之前插入 fragment
container.parentNode.insertBefore(fragment, container);
// 折叠逻辑
const allItems = document.querySelectorAll(`.${CONFIG.top250Class}, .${CONFIG.rankLabelClass}`);
allItems.forEach(item => { item.style.display = "inline-block" });
if (allItems.length > 4) {
// 创建折叠按钮
const toggleBtn = document.createElement('div');
toggleBtn.id = CONFIG.toggleButtonId;
toggleBtn.innerHTML = '<a href="javascript:void(0)">展示剩余 →</a>';
toggleBtn.setAttribute('data-toggle', 'show');
toggleBtn.style.display = 'inline-block';
// 在 container 之前插入 toggleBtn
container.parentNode.insertBefore(toggleBtn, container);
Array.from(allItems).slice(4).forEach(item => { item.style.display = 'none' });
// 点击按钮切换显示/隐藏
toggleBtn.addEventListener('click', function () {
const toggleState = this.getAttribute('data-toggle');
if (toggleState === 'show') {
Array.from(allItems).slice(4).forEach(item => { item.style.display = "inline-block" });
this.setAttribute('data-toggle', 'hide');
this.innerHTML = '<a href="javascript:void(0)">隐藏剩余 ←</a>';
} else {
Array.from(allItems).slice(4).forEach(item => { item.style.display = "none" });
this.setAttribute('data-toggle', 'show');
this.innerHTML = '<a href="javascript:void(0)">展示剩余 →</a>';
}
});
}
}
/**
* 处理电影页面
*/
function handleMoviePage() {
const matchResult = window.location.href.match(/^https:\/\/movie\.douban\.com\/subject\/(\d+)\/?$/);
if (!matchResult) return;
const doubanId = matchResult[1];
const header = document.querySelector("#content > h1");
if (!header) return;
const processingStatusItem = createTop250RankItem({
position: '...',
title: 'rank4douban',
shortTitle: '处理中',
href: 'https://rank4douban.eddiehe.top/'
});
injectTop250Stylesheet();
renderRankListWithToggle(header, [processingStatusItem]);
GM_xmlhttpRequest({
method: 'GET',
url: CONFIG.movieRankUrl,
onload: function (response) {
try {
const data = JSON.parse(response.responseText);
const lists = Object.values(data);
const rankItems = lists
.filter(list => list.list[doubanId])
.map(list => ({
prefix: list.prefix || 'No.',
position: list.list[doubanId],
title: list.title,
shortTitle: list.short_title,
href: list.href
}))
.map(createTop250RankItem);
// remove processing status item
removeProcessingItem("https://rank4douban.eddiehe.top/");
if (rankItems.length > 0) {
renderRankListWithToggle(header, rankItems);
}
} catch (e) {
console.error("【豆瓣榜单助手·Douban-Ranker】电影榜单数据处理失败:", e);
alert("豆瓣榜单助手·Douban-Ranker:电影榜单数据处理时发生错误,请稍后再试!");
}
},
onerror: function (error) {
console.error("【豆瓣榜单助手·Douban-Ranker】电影榜单网络请求失败:", error);
alert("豆瓣榜单助手 · Douban-Ranker:电影榜单网络请求时发生错误,请检查您的网络连接后重试!");
}
});
}
/**
* 处理播客页面
*/
function handlePodcastPage() {
const header = document.querySelector("#content > h1");
if (!header) return;
const podcastName = header.innerText.trim();
const processingStatusItem = createTop250RankItem({
position: '...',
title: '中文播客榜',
shortTitle: '处理中',
href: 'https://xyzrank.eddiehe.top/'
});
injectTop250Stylesheet();
renderRankListWithToggle(header, [processingStatusItem]);
GM_xmlhttpRequest({
method: 'GET',
url: CONFIG.podcastRankUrl,
onload: function (response) {
try {
const data = JSON.parse(response.responseText);
const podcast = data.data?.podcasts?.find(p => p.name === podcastName);
// remove processing status item
removeProcessingItem("https://xyzrank.eddiehe.top/");
if (podcast && podcast.rank) {
const item = createTop250RankItem({
position: podcast.rank,
title: '中文播客榜',
shortTitle: '中文播客榜',
href: podcast.links[0]?.url || '#'
});
renderRankListWithToggle(header, [item]);
}
} catch (e) {
console.error("【豆瓣榜单助手·Douban-Ranker】播客榜单数据处理失败:", e);
alert("豆瓣榜单助手·Douban-Ranker:播客榜单数据处理时发生错误,请稍后再试!");
}
},
onerror: function (error) {
console.error("【豆瓣榜单助手·Douban-Ranker】播客榜单网络请求失败:", error);
alert("豆瓣榜单助手·Douban-Ranker:播客榜单网络请求时发生错误,请检查您的网络连接后重试!");
}
});
}
/**
* 处理音乐页面
*/
function handleMusicPage() {
const header = document.querySelector("h1");
if (!header) return;
const albumName = header.textContent.trim();
const processingStatusItem = createTop250RankItem({
position: '...',
title: 'HOPICO MUSIC AWARD',
shortTitle: '处理中',
href: 'https://hma.eddiehe.top/'
});
injectTop250Stylesheet();
injectRankLabelStylesheet();
renderRankListWithToggle(header, [processingStatusItem]);
GM_xmlhttpRequest({
method: 'GET',
url: CONFIG.musicRankUrl,
onload: function (response) {
try {
const data = JSON.parse(response.responseText);
const rankItems = [];
// 遍历所有届次 (HOPICO MUSIC AWARDS)
Object.entries(data).forEach(([awardName, awardData]) => {
if (!awardName.includes('HOPICO MUSIC AWARDS')) return;
const categories = awardData.categories || {};
// 检查所有奖项类别
Object.entries(categories).forEach(([categoryKey, category]) => {
const matchingAlbums = category.albums.filter(album =>
album.name == albumName
);
matchingAlbums.forEach(album => {
rankItems.push(createRankLabelRankItem({
prefix: '# ',
position: album.award,
title: category.title,
shortTitle: category.short_title,
href: awardData.href,
}));
});
});
});
// remove processing status item
removeProcessingItem("https://hma.eddiehe.top/");
if (rankItems.length > 0) {
renderRankListWithToggle(header, rankItems);
}
} catch (e) {
console.error("【豆瓣榜单助手·Douban-Ranker】音乐榜单数据处理失败:", e);
alert("豆瓣榜单助手·Douban-Ranker:音乐榜单数据处理时发生错误,请稍后再试!");
}
},
onerror: function (error) {
console.error("【豆瓣榜单助手·Douban-Ranker】音乐榜单网络请求失败:", error);
alert("豆瓣榜单助手·Douban-Ranker:音乐榜单网络请求时发生错误,请检查您的网络连接后重试!");
}
});
}
// 页面适配入口
switch (location.host) {
case 'movie.douban.com':
handleMoviePage();
break;
case 'www.douban.com':
if (location.pathname.startsWith('/podcast')) handlePodcastPage();
break;
case 'music.douban.com':
handleMusicPage();
break;
}
})();