-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouban-neodb-userscript.js
113 lines (97 loc) · 3.55 KB
/
douban-neodb-userscript.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
// ==UserScript==
// @name 豆瓣 NeoDB 助手
// @namespace https://cdjax.com/
// @version 0.4
// @description 在豆瓣页面显示 NeoDB 对应条目
// @author You
// @match https://movie.douban.com/subject/*
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
// 添加一个标记,防止重复添加
const NEODB_ADDED_FLAG = 'neodb-added';
function addStyles() {
const styles = `
.neodb-section {
margin-bottom: 30px;
}
.neodb-section h2 {
color: #072;
font-size: 15px;
margin-bottom: 12px;
}
.neodb-section .neodb-link {
display: block;
background: #f5f5f5;
padding: 10px;
border-radius: 4px;
color: #37a;
text-decoration: none;
}
.neodb-section .neodb-link:hover {
background: #e8f4f9;
}
`;
const styleSheet = document.createElement("style");
styleSheet.textContent = styles;
document.head.appendChild(styleSheet);
}
function insertAfterElement(newElement, targetElement) {
if (targetElement.nextSibling) {
targetElement.parentNode.insertBefore(newElement, targetElement.nextSibling);
} else {
targetElement.parentNode.appendChild(newElement);
}
}
function addNeoDBInfo() {
// 检查是否已经添加过
if (document.querySelector(`.${NEODB_ADDED_FLAG}`)) {
return;
}
// 获取豆瓣条目标题
const titleElement = document.querySelector('h1 span[property="v:itemreviewed"]');
if (!titleElement) {
console.log("未找到标题元素");
return;
}
const title = titleElement.textContent.trim();
// 获取年份
const yearElement = document.querySelector('span.year');
const year = yearElement ? yearElement.textContent.replace(/[()]/g, '').trim() : '';
// 创建 NeoDB 区块
const neodbSection = document.createElement('div');
neodbSection.className = `neodb-section ${NEODB_ADDED_FLAG}`;
// 添加标题
const header = document.createElement('h2');
header.textContent = 'NeoDB';
neodbSection.appendChild(header);
// 构建搜索链接
const searchTerm = year ? `${title} ${year}` : title;
const searchUrl = `https://neodb.social/search/?q=${encodeURIComponent(searchTerm)}`;
// 添加链接
const link = document.createElement('a');
link.href = searchUrl;
link.className = 'neodb-link';
link.textContent = '在 NeoDB 上查看';
link.target = '_blank';
neodbSection.appendChild(link);
// 插入到豆瓣右侧栏的适当位置
const interestSection = document.querySelector('.related-info') ||
document.querySelector('.aside') ||
document.querySelector('#interest_sectl');
if (interestSection) {
insertAfterElement(neodbSection, interestSection);
console.log("成功添加 NeoDB 区块");
}
}
// 添加样式
addStyles();
// 当 DOM 加载完成后执行一次
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', addNeoDBInfo);
} else {
addNeoDBInfo();
}
})();