This repository was archived by the owner on Mar 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodemao-userdb.user.js
More file actions
334 lines (281 loc) · 13.1 KB
/
codemao-userdb.user.js
File metadata and controls
334 lines (281 loc) · 13.1 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
// ==UserScript==
// @name CodemaoUserDB 社区集成插件(编程猫昵称搜索)
// @namespace https://userdb.hachimlab.top/
// @version 1.0
// @description 社区没有按昵称找人的功能?来使用这个插件!
// @author HachimLab
// @match https://shequ.codemao.cn/discover*
// @match https://shequ.codemao.cn/user/*
// @grant GM_xmlhttpRequest
// @connect udbapi.hachimlab.top
// @icon https://hachimlab.top/favicon.ico
// ==/UserScript==
(function() {
'use strict';
// ==============================
// 首次使用提示
// ==============================
const firstUseKey = "codemao_userdb_first_use_notice";
if (!localStorage.getItem(firstUseKey)) {
alert(
`欢迎使用 CodemaoUserDB 社区集成插件!这个提示只会在第一次使用时弹出~
本插件将会无感记录/更新用户ID信息,仅在打开个人页面时进行记录
部分用户信息未记录,关于技术细节,请打开GitHub仓库查看
给我们一个Star:https://github.com/hachimlab/UserDB
============
By HachimLab(哈基米实验室) | https://hachimlab.top/`
);
localStorage.setItem(firstUseKey, "true");
}
// ==============================
// 发现页功能(用户搜索)
// ==============================
if (location.href.includes("/discover")) {
function removePopup() {
const popup = document.getElementById('notices');
if (popup) {
popup.remove();
}
}
function addSearchBox() {
const container = document.querySelector('.r-discover--search-box');
if (!container) return;
if (container.querySelector('.custom-user-search')) return;
const input = document.createElement('input');
input.type = 'text';
input.placeholder = '搜索用户';
input.className = 'custom-user-search';
input.style.border = '1px solid #e5e5e5';
input.style.borderRadius = '4px';
input.style.padding = '6px 12px';
input.style.lineHeight = '18px';
input.style.color = '#333';
input.style.width = '220px';
input.style.marginLeft = '8px';
container.appendChild(input);
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
const keyword = input.value.trim();
if (keyword) {
searchUsers(keyword);
}
}
});
}
function addExternalLink() {
const switchBox = document.querySelector('.r-discover--switch-box');
if (!switchBox) return;
if (switchBox.querySelector('.custom-userdb-link')) return;
const listItem = document.createElement('li');
listItem.className = 'custom-userdb-link';
listItem.style.marginLeft = '20px';
listItem.style.padding = '0 8px';
listItem.style.cursor = 'pointer';
const link = document.createElement('a');
link.href = 'https://userdb.hachimlab.top/';
link.target = '_blank';
link.textContent = 'Codemao UserDB';
link.style.color = '#fec433';
link.style.fontWeight = '500';
link.style.fontSize = '16px';
link.style.textDecoration = 'none';
link.style.transition = 'color 0.2s ease';
link.addEventListener('mouseenter', () => {
link.style.color = '#ffbb10';
});
link.addEventListener('mouseleave', () => {
link.style.color = '#fec433';
});
listItem.appendChild(link);
switchBox.appendChild(listItem);
}
async function searchUsers(keyword) {
const worksSection = document.querySelector('.r-discover--works-section');
if (!worksSection) return;
const currentUrl = window.location.href;
sessionStorage.setItem('lastWorkSearchUrl', currentUrl);
worksSection.innerHTML = '<div style="text-align: center; padding: 50px;">搜索中...</div>';
try {
const response = await fetch(`https://udbapi.hachimlab.top/search?nickname=${encodeURIComponent(keyword)}`);
if (response.status === 404) {
showEmptyResult(keyword, worksSection);
return;
}
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (data.status === 'ok' && data.items && data.items.length > 0) {
displayUserResults(data, keyword, worksSection);
} else {
showEmptyResult(keyword, worksSection);
}
} catch (error) {
console.error('搜索用户出错:', error);
worksSection.innerHTML = `<div style="text-align: center; padding: 50px; color: red;">搜索失败: ${error.message}<br>请检查网络连接或稍后重试</div>`;
}
}
function showEmptyResult(keyword, worksSection) {
worksSection.innerHTML = `
<div style="margin-bottom: 20px; display: flex; justify-content: space-between; align-items: center; padding: 0 10px;">
<div style="font-size: 16px; color: #333;">
为你找到"${escapeHtml(keyword)}"相关的用户共 0 个
</div>
<button id="backToWorkSearch" style="
padding: 6px 12px;
background: #fec433;
border: none;
border-radius: 4px;
color: #fff;
cursor: pointer;
font-size: 14px;
transition: background-color 0.2s ease;
">返回作品搜索</button>
</div>
`;
const backButton = document.getElementById('backToWorkSearch');
if (backButton) {
backButton.addEventListener('mouseenter', () => {
backButton.style.backgroundColor = '#ffbb10';
});
backButton.addEventListener('mouseleave', () => {
backButton.style.backgroundColor = '#fec433';
});
backButton.addEventListener('click', () => {
const lastUrl = sessionStorage.getItem('lastWorkSearchUrl');
if (lastUrl) {
window.location.href = lastUrl;
} else {
window.location.href = 'https://shequ.codemao.cn/discover';
}
});
}
}
function displayUserResults(data, keyword, worksSection) {
const items = data.items;
const total = data.user || items.length;
let html = `
<div style="margin-bottom: 20px; display: flex; justify-content: space-between; align-items: center; padding: 0 10px;">
<div style="font-size: 16px; color: #333;">
为你找到"${escapeHtml(keyword)}"相关的用户共 ${total} 个
</div>
<button id="backToWorkSearch" style="
padding: 6px 12px;
background: #fec433;
border: none;
border-radius: 4px;
color: #fff;
cursor: pointer;
font-size: 14px;
transition: background-color 0.2s ease;
">返回作品搜索</button>
</div>
<div style="display: flex; flex-wrap: wrap; gap: 20px; margin-bottom: 40px;">
`;
items.forEach(user => {
const userId = user.id;
html += `
<div class="event_target data_report r-discover--work-item" style="cursor: pointer; width: 220px; margin: 0;" onclick="window.open('https://shequ.codemao.cn/user/${userId}', '_blank')">
<div class="r-discover-c-workcard--work_item_wrap">
<div class="r-discover-c-workcard--work_item">
<div class="r-discover-c-workcard--work_img_wrap">
<span class="r-discover-c-workcard--work_img" style="background-image: url('${escapeHtml(user.avatar || 'https://static.codemao.cn/avatar/default/v1_user_008.png')}');"></span>
</div>
<div class="r-discover-c-workcard--work_detail" style="padding-bottom: 15px;">
<h5 class="r-discover-c-workcard--work_name">${escapeHtml(user.nickname)}</h5>
<div style="color: #666; font-size: 14px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; min-height: 20px;">
${escapeHtml(user.description || '这个人很懒,什么都没写~')}
</div>
<p class="r-discover-c-workcard--author" style="margin-top: 10px; padding-top: 0; margin-bottom: 0;">
<a target="_blank" href="https://shequ.codemao.cn/user/${userId}" style="color: #999; font-size: 13px; text-decoration: none; transition: color 0.2s ease;">用户ID: ${userId}</a>
</p>
</div>
</div>
</div>
</div>
`;
});
html += `</div>`;
worksSection.innerHTML = html;
const backButton = document.getElementById('backToWorkSearch');
if (backButton) {
backButton.addEventListener('mouseenter', () => {
backButton.style.backgroundColor = '#ffbb10';
});
backButton.addEventListener('mouseleave', () => {
backButton.style.backgroundColor = '#fec433';
});
backButton.addEventListener('click', () => {
const lastUrl = sessionStorage.getItem('lastWorkSearchUrl');
if (lastUrl) {
window.location.href = lastUrl;
} else {
window.location.href = 'https://shequ.codemao.cn/discover';
}
});
}
}
function escapeHtml(unsafe) {
if (!unsafe) return '';
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function init() {
removePopup();
addSearchBox();
addExternalLink();
}
window.addEventListener('load', function() {
setTimeout(init, 500);
});
}
// ==============================
// 用户页 UID 自动同步
// ==============================
if (location.href.includes("/user/")) {
const THROTTLE_TIME = 10 * 60 * 1000;
const match = window.location.pathname.match(/\/user\/(\d+)/);
if (!match) return;
const uid = match[1];
const now = Date.now();
const storageKey = `codemao_uid_${uid}`;
const lastTime = localStorage.getItem(storageKey);
if (lastTime && (now - parseInt(lastTime, 10)) < THROTTLE_TIME) {
return;
}
const originalTitle = document.title;
function tempTitle(text) {
document.title = text;
setTimeout(() => {
document.title = originalTitle;
}, 1000);
}
const updateUrl = `https://udbapi.hachimlab.top/user/update?id=${uid}`;
const addUrl = `https://udbapi.hachimlab.top/user/add?id=${uid}`;
GM_xmlhttpRequest({
method: "GET",
url: updateUrl,
onload: function(response) {
if (response.status === 404) {
GM_xmlhttpRequest({
method: "GET",
url: addUrl,
onload: function(addResponse) {
if (addResponse.status === 200) {
localStorage.setItem(storageKey, now.toString());
tempTitle("已记录此用户信息");
}
}
});
} else {
localStorage.setItem(storageKey, now.toString());
tempTitle("已更新此用户信息");
}
}
});
}
})();