Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
363 lines (293 loc) · 10.4 KB
/
Copy pathcontent.js
File metadata and controls
363 lines (293 loc) · 10.4 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
// X-Vision: Judgment Day v5.0.1
// 修复:hover 绿框消失 | 自己被标绿 | /following 右侧推荐关注误标红
(() => {
const STORE_KEY = "my_following_roster";
const STYLE_ID = "uh-style-v501";
const MAX_ROSTER = 60000;
const SAVE_DEBOUNCE_MS = 2000;
const NAV_SCAN_DELAY_MS = 800;
const NAV_POLL_MS = 8000;
const DEBUG = false;
let followingSet = new Set();
let rosterReady = false;
let saveTimer = null;
let lastPath = window.location.pathname;
let myHandle = null;
// key: element, value: signature
let sigMap = new WeakMap();
const queue = new Set();
let rafScheduled = false;
function log(...args) {
if (DEBUG) console.log("[XVJD]", ...args);
}
function injectStylesOnce() {
if (document.getElementById(STYLE_ID)) return;
const styleEl = document.createElement("style");
styleEl.id = STYLE_ID;
// 用 inset box-shadow,避免 hover 时 outline 被站点样式压掉
styleEl.textContent = `
.uh-highlight-new{
box-shadow: 0 0 0 2px #00e676 inset !important;
border-radius: 12px !important;
}
.uh-alert-unfollow{
box-shadow: 0 0 0 3px #ff4444 inset !important;
background-color: rgba(255, 68, 68, 0.03) !important;
border-radius: 12px !important;
}
`;
(document.head || document.documentElement).appendChild(styleEl);
}
function loadRoster() {
return new Promise((resolve) => {
chrome.storage.local.get([STORE_KEY], (result) => {
const arr = Array.isArray(result[STORE_KEY]) ? result[STORE_KEY] : [];
followingSet = new Set(arr.map((x) => String(x).toLowerCase()));
rosterReady = true;
log("roster loaded", followingSet.size);
resolve();
});
});
}
function scheduleSave() {
if (saveTimer) clearTimeout(saveTimer);
saveTimer = setTimeout(() => {
chrome.storage.local.set({ [STORE_KEY]: Array.from(followingSet) });
saveTimer = null;
log("roster saved", followingSet.size);
}, SAVE_DEBOUNCE_MS);
}
function isFollowingPage() {
return window.location.pathname.includes("/following");
}
function parseHandleFromHref(hrefRaw) {
if (!hrefRaw) return null;
const href = String(hrefRaw);
if (!href.startsWith("/")) return null;
const clean = href.split("?")[0].split("#")[0];
const parts = clean.split("/").filter(Boolean);
if (parts.length < 1) return null;
const first = `/${parts[0].toLowerCase()}`;
const blocked = new Set([
"/home",
"/explore",
"/notifications",
"/messages",
"/search",
"/settings",
"/compose",
"/i",
"/intent"
]);
if (blocked.has(first)) return null;
return parts[0].toLowerCase();
}
// 识别当前账号 handle,用于排除自己
function detectMyHandle() {
// 侧边栏个人资料入口通常稳定
const a = document.querySelector('a[data-testid="AppTabBar_Profile_Link"][href^="/"]');
if (a) {
const h = parseHandleFromHref(a.getAttribute("href"));
if (h) return h;
}
return null;
}
// /following 主栏过滤:避免右侧推荐关注被处理
function isUserCellInFollowingTimeline(card) {
const primary = card.closest('div[data-testid="primaryColumn"]');
if (!primary) return false;
const sidebar = card.closest('div[data-testid="sidebarColumn"], aside');
if (sidebar) return false;
// /following 页面主时间线通常在一个带 aria-label 的 section 内
const sec = card.closest("section[aria-label]");
if (!sec) return true;
const label = sec.getAttribute("aria-label") || "";
// 常见:Timeline: Following / 时间线: 正在关注
if (label.includes("Following") || label.includes("正在关注")) return true;
// 其他 section 先跳过,避免误伤推荐模块
return false;
}
// 在 UserCell 内抓 handle
function getHandleFromUserCell(card) {
if (!card || !card.querySelectorAll) return null;
const links = Array.from(card.querySelectorAll('a[href^="/"]'));
for (const a of links) {
const h = parseHandleFromHref(a.getAttribute("href"));
if (!h) continue;
// 排除帖子链接 /user/status/xxx
const href = String(a.getAttribute("href") || "");
if (href.includes("/status/")) continue;
return h;
}
return null;
}
// 时间线抓作者 handle:优先用时间戳链接 /user/status/id,hover 不会变
function getHandleFromArticle(article) {
if (!article) return null;
const timeEl = article.querySelector('a[href*="/status/"] time');
if (timeEl) {
const a = timeEl.closest("a");
if (a) {
const href = String(a.getAttribute("href") || "");
const clean = href.split("?")[0].split("#")[0];
const parts = clean.split("/").filter(Boolean);
// 期待格式:user / status / id
if (parts.length >= 3 && parts[1] === "status") {
const h = parts[0].toLowerCase();
if (h && h !== "i") return h;
}
}
}
// 兜底:找 User-Name 区块里的主页链接,限制在当前 article
const blocks = Array.from(article.querySelectorAll('[data-testid="User-Name"]'))
.filter((el) => el.closest("article") === article);
for (const block of blocks) {
const a = block.querySelector('a[href^="/"]');
const h = a ? parseHandleFromHref(a.getAttribute("href")) : null;
if (h) return h;
}
return null;
}
function enqueue(el) {
if (!el || el.nodeType !== 1) return;
queue.add(el);
if (rafScheduled) return;
rafScheduled = true;
requestAnimationFrame(flushQueue);
}
function flushQueue() {
rafScheduled = false;
if (!rosterReady) {
queue.clear();
return;
}
const following = isFollowingPage();
for (const el of queue) {
if (following) {
if (el.matches && el.matches('[data-testid="UserCell"]')) {
applyFollowingCard(el);
} else if (el.querySelectorAll) {
el.querySelectorAll('[data-testid="UserCell"]').forEach(applyFollowingCard);
}
} else {
if (el.matches && el.matches("article")) {
applyTimelineArticle(el);
} else {
const art = el.closest && el.closest("article");
if (art) applyTimelineArticle(art);
if (el.querySelectorAll) el.querySelectorAll("article").forEach(applyTimelineArticle);
}
}
}
queue.clear();
}
// /following 页面逻辑
function applyFollowingCard(card) {
if (!card) return;
if (!isUserCellInFollowingTimeline(card)) return;
const handle = getHandleFromUserCell(card);
if (!handle) {
card.classList.remove("uh-alert-unfollow");
return;
}
if (myHandle && handle === myHandle) {
card.classList.remove("uh-alert-unfollow");
return;
}
// 学习 roster,仅对真正的 following 列表做学习
if (!followingSet.has(handle) && followingSet.size < MAX_ROSTER) {
followingSet.add(handle);
scheduleSave();
}
// 未回关判断仍用局部文案匹配
const text = card.innerText || "";
const followerFlag =
text.includes("关注了你") ||
text.includes("Follows you") ||
text.includes("互相关注");
const sig = `f|${handle}|${followerFlag ? "ok" : "no"}`;
const prev = sigMap.get(card);
if (prev === sig) return;
if (followerFlag) card.classList.remove("uh-alert-unfollow");
else card.classList.add("uh-alert-unfollow");
sigMap.set(card, sig);
}
// 时间线与评论区:只给生人加绿框
function applyTimelineArticle(article) {
if (!article) return;
const handle = getHandleFromArticle(article);
if (!handle) return;
if (myHandle && handle === myHandle) {
// 清理残留
article.classList.remove("uh-highlight-new");
sigMap.set(article, `t|${handle}|self`);
return;
}
const hasRoster = followingSet.size > 0;
const isNew = hasRoster && !followingSet.has(handle);
const sig = `t|${handle}|${isNew ? "new" : "old"}`;
const prev = sigMap.get(article);
if (prev === sig) return;
article.classList.remove("uh-highlight-new");
if (isNew) article.classList.add("uh-highlight-new");
sigMap.set(article, sig);
}
function forceScanAll() {
sigMap = new WeakMap();
// 尝试刷新 myHandle
myHandle = myHandle || detectMyHandle();
log("myHandle", myHandle);
if (isFollowingPage()) {
document.querySelectorAll('[data-testid="UserCell"]').forEach((el) => enqueue(el));
} else {
document.querySelectorAll("article").forEach((el) => enqueue(el));
}
}
const observer = new MutationObserver((mutations) => {
if (!rosterReady) return;
for (const m of mutations) {
for (const n of m.addedNodes) {
if (n.nodeType !== 1) continue;
enqueue(n);
}
}
});
function checkUrlChange() {
const current = window.location.pathname;
if (current === lastPath) return;
lastPath = current;
setTimeout(forceScanAll, NAV_SCAN_DELAY_MS);
}
function hookHistory() {
if (window.__xvjdHistoryHooked) return;
window.__xvjdHistoryHooked = true;
const fire = () => window.dispatchEvent(new Event("xvjd:navigate"));
const wrap = (method) => {
const orig = history[method];
history[method] = function () {
const ret = orig.apply(this, arguments);
fire();
return ret;
};
};
wrap("pushState");
wrap("replaceState");
window.addEventListener("popstate", fire);
window.addEventListener("xvjd:navigate", checkUrlChange);
}
async function init() {
injectStylesOnce();
await loadRoster();
// 初始化时尽早拿到自己的 handle
myHandle = detectMyHandle();
// 有些页面侧栏渲染慢,补一次延迟检测
setTimeout(() => {
myHandle = myHandle || detectMyHandle();
}, 1500);
observer.observe(document.body, { childList: true, subtree: true });
hookHistory();
setTimeout(forceScanAll, NAV_SCAN_DELAY_MS);
setInterval(checkUrlChange, NAV_POLL_MS);
}
init();
})();