fix(pting-ip-location): support fengchao.chat IP location
This commit is contained in:
1 file changed
+95
-44
@@ -1,10 +1,10 @@
|
||||
// ==UserScript==
|
||||
// @name 蜂巢 - 帖子/回复显示 IP 属地
|
||||
// @namespace https://pting.club/
|
||||
// @version 1.0.1
|
||||
// @namespace https://fengchao.chat/
|
||||
// @version 1.0.2
|
||||
// @description 在帖子、回复及头像悬浮资料卡中显示用户资料页已公开的 IP 属地。
|
||||
// @author Stardream
|
||||
// @match https://pting.club/*
|
||||
// @match https://fengchao.chat/*
|
||||
// @grant none
|
||||
// @run-at document-idle
|
||||
// @license MIT
|
||||
@@ -12,7 +12,8 @@
|
||||
|
||||
(() => {
|
||||
'use strict';
|
||||
const CACHE_KEY = 'pting-ip-location-cache-v1';
|
||||
|
||||
const CACHE_KEY = 'pting-ip-location-cache-v2';
|
||||
const CACHE_TTL = 7 * 24 * 60 * 60 * 1000;
|
||||
const cache = new Map();
|
||||
const pending = new Map();
|
||||
@@ -22,22 +23,30 @@
|
||||
|
||||
try {
|
||||
const stored = JSON.parse(localStorage.getItem(CACHE_KEY) || '{}');
|
||||
|
||||
for (const [username, item] of Object.entries(stored)) {
|
||||
if (item && item.location && Date.now() - item.updatedAt < CACHE_TTL) cache.set(username, item);
|
||||
if (item && item.location && Date.now() - item.updatedAt < CACHE_TTL) {
|
||||
cache.set(username, item);
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
|
||||
function saveCache() {
|
||||
try { localStorage.setItem(CACHE_KEY, JSON.stringify(Object.fromEntries(cache))); } catch {}
|
||||
try {
|
||||
localStorage.setItem(CACHE_KEY, JSON.stringify(Object.fromEntries(cache)));
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function getUsername(link) {
|
||||
try {
|
||||
const url = new URL(link.href, location.href);
|
||||
if (url.origin !== location.origin) return null;
|
||||
|
||||
const match = url.pathname.match(/^\/users\/([^/?#]+)\/?$/);
|
||||
return match ? decodeURIComponent(match[1]) : null;
|
||||
} catch { return null; }
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function getProfileButtonUsername(button) {
|
||||
@@ -54,61 +63,72 @@
|
||||
function isPrimaryProfileLink(link) {
|
||||
const card = link.closest('[data-slot="popover-content"]');
|
||||
if (!card) return true;
|
||||
|
||||
const firstUserLink = [...card.querySelectorAll('a[href]')].find(getUsername);
|
||||
return link === firstUserLink;
|
||||
}
|
||||
|
||||
function isUserSearch() {
|
||||
return location.pathname === '/search' && new URLSearchParams(location.search).get('type') === 'users';
|
||||
return location.pathname === '/search' &&
|
||||
new URLSearchParams(location.search).get('type') === 'users';
|
||||
}
|
||||
|
||||
function getSearchUsernameElement(link, username) {
|
||||
if (!isUserSearch()) return null;
|
||||
|
||||
return [...link.querySelectorAll('p')].find(
|
||||
element => element.textContent.trim() === '@' + username
|
||||
) || null;
|
||||
}
|
||||
|
||||
function extractLocation(html) {
|
||||
// IP 属地位于个人页“加入”文字之后的同一资料行。
|
||||
const doc = new DOMParser().parseFromString(html, 'text/html');
|
||||
const joined = [...doc.querySelectorAll('body *')].find(
|
||||
el => el.children.length === 0 && (el.textContent || '').trim().endsWith('加入')
|
||||
);
|
||||
if (!joined) return null;
|
||||
|
||||
// “加入”和城市不一定处于同一个最小元素内;向上寻找资料行,
|
||||
// 再取“加入”之后最近的纯中文叶子节点(例如“北京”)。
|
||||
for (let scope = joined.parentElement; scope; scope = scope.parentElement) {
|
||||
const leaves = [...scope.querySelectorAll('*')]
|
||||
.filter(el => el.children.length === 0);
|
||||
const index = leaves.indexOf(joined);
|
||||
if (index < 0) continue;
|
||||
const locationName = leaves
|
||||
.slice(index + 1, index + 5)
|
||||
.map(el => (el.textContent || '').trim())
|
||||
.find(text => /^[\u4e00-\u9fff]{2,12}$/.test(text));
|
||||
if (locationName) return locationName;
|
||||
}
|
||||
return null;
|
||||
// 只在“加入日期”所在的资料信息行中匹配,避免误读简介或帖子内容。
|
||||
const joined = [...doc.querySelectorAll('body *')].find(
|
||||
el => el.children.length === 0 &&
|
||||
(el.textContent || '').trim().endsWith('加入')
|
||||
);
|
||||
|
||||
if (!joined?.parentElement) return null;
|
||||
|
||||
const locationRow = [...joined.parentElement.children].find(
|
||||
el => /^IP\s*属地\s*[::]\s*.+$/.test((el.textContent || '').trim())
|
||||
);
|
||||
|
||||
const match = locationRow?.textContent.trim()
|
||||
.match(/^IP\s*属地\s*[::]\s*(.+)$/);
|
||||
|
||||
return match?.[1].trim() || null;
|
||||
}
|
||||
|
||||
function fetchLocation(username) {
|
||||
const cached = cache.get(username);
|
||||
if (cached) return Promise.resolve(cached.location);
|
||||
if (pending.has(username)) return pending.get(username);
|
||||
const task = fetch('/users/' + encodeURIComponent(username), { credentials: 'same-origin' })
|
||||
|
||||
const task = fetch('/users/' + encodeURIComponent(username), {
|
||||
credentials: 'same-origin'
|
||||
})
|
||||
.then(response => response.ok ? response.text() : '')
|
||||
.then(extractLocation)
|
||||
.catch(() => null)
|
||||
.then(locationName => {
|
||||
pending.delete(username);
|
||||
|
||||
if (locationName) {
|
||||
cache.set(username, { location: locationName, updatedAt: Date.now() });
|
||||
cache.set(username, {
|
||||
location: locationName,
|
||||
updatedAt: Date.now()
|
||||
});
|
||||
saveCache();
|
||||
} else unavailable.add(username);
|
||||
} else {
|
||||
unavailable.add(username);
|
||||
}
|
||||
|
||||
return locationName;
|
||||
});
|
||||
|
||||
pending.set(username, task);
|
||||
return task;
|
||||
}
|
||||
@@ -118,14 +138,17 @@
|
||||
if (knownBadge?.isConnected) return;
|
||||
|
||||
const usernameElement = getSearchUsernameElement(target, username);
|
||||
// 用户搜索结果的卡片链接是网格直接子项,只能把徽标放入 @username 行。
|
||||
|
||||
// 用户搜索结果的卡片本身是网格项目,只允许插到 @username 行内。
|
||||
if (isUserSearch() && !usernameElement) return;
|
||||
|
||||
if (usernameElement) {
|
||||
const existingBadge = [...usernameElement.children].find(
|
||||
element => element.classList.contains('tm-pting-ip-location') &&
|
||||
element =>
|
||||
element.classList.contains('tm-pting-ip-location') &&
|
||||
element.dataset.username === username
|
||||
);
|
||||
|
||||
if (existingBadge) {
|
||||
badgeByTarget.set(target, existingBadge);
|
||||
return;
|
||||
@@ -133,23 +156,37 @@
|
||||
}
|
||||
|
||||
const next = target.nextElementSibling;
|
||||
if (!usernameElement && next && next.classList.contains('tm-pting-ip-location') &&
|
||||
next.dataset.username === username) {
|
||||
|
||||
if (
|
||||
!usernameElement &&
|
||||
next &&
|
||||
next.classList.contains('tm-pting-ip-location') &&
|
||||
next.dataset.username === username
|
||||
) {
|
||||
badgeByTarget.set(target, next);
|
||||
return;
|
||||
}
|
||||
|
||||
const badge = document.createElement('span');
|
||||
badge.className = 'tm-pting-ip-location';
|
||||
badge.dataset.username = username;
|
||||
badge.textContent = 'IP · …';
|
||||
badge.title = 'IP 属地';
|
||||
|
||||
badgeByTarget.set(target, badge);
|
||||
if (usernameElement) usernameElement.append(badge);
|
||||
else target.insertAdjacentElement('afterend', badge);
|
||||
|
||||
if (usernameElement) {
|
||||
usernameElement.append(badge);
|
||||
} else {
|
||||
target.insertAdjacentElement('afterend', badge);
|
||||
}
|
||||
|
||||
fetchLocation(username).then(locationName => {
|
||||
if (!badge.isConnected) return;
|
||||
if (locationName) badge.textContent = 'IP · ' + locationName;
|
||||
else {
|
||||
|
||||
if (locationName) {
|
||||
badge.textContent = 'IP · ' + locationName;
|
||||
} else {
|
||||
badgeByTarget.delete(target);
|
||||
badge.remove();
|
||||
}
|
||||
@@ -157,38 +194,50 @@
|
||||
}
|
||||
|
||||
function scan() {
|
||||
// 个人页原生显示属地,因此不重复插入。
|
||||
// 个人资料页已经原生显示 IP 属地。
|
||||
if (location.pathname.startsWith('/users/')) return;
|
||||
|
||||
if (isUserSearch()) {
|
||||
// 清理旧版错误插入到网格同级的徽标,恢复搜索结果的两栏布局。
|
||||
// 清理旧版错误插入到用户搜索网格同级的徽标。
|
||||
for (const badge of document.querySelectorAll('.tm-pting-ip-location')) {
|
||||
const parent = badge.parentElement;
|
||||
if (parent?.classList.contains('grid') && parent.querySelector(':scope > a[href^="/users/"]')) {
|
||||
|
||||
if (
|
||||
parent?.classList.contains('grid') &&
|
||||
parent.querySelector(':scope > a[href^="/users/"]')
|
||||
) {
|
||||
badge.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const link of document.querySelectorAll('a[href]')) {
|
||||
const username = getUsername(link);
|
||||
|
||||
if (username && isPrimaryProfileLink(link) && !unavailable.has(username)) {
|
||||
appendBadge(link, username);
|
||||
}
|
||||
}
|
||||
|
||||
// “作者复言”等条目只提供资料卡按钮,没有用户链接。
|
||||
// “作者复言”等条目可能只有资料卡按钮、没有用户链接。
|
||||
for (const button of document.querySelectorAll('[data-user-profile-preview-trigger]')) {
|
||||
if (!button.textContent.trim() || hasNearbyUserLink(button)) continue;
|
||||
|
||||
const username = getProfileButtonUsername(button);
|
||||
if (username && !unavailable.has(username)) appendBadge(button, username);
|
||||
|
||||
if (username && !unavailable.has(username)) {
|
||||
appendBadge(button, username);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleScan() {
|
||||
clearTimeout(scanTimer);
|
||||
scanTimer = setTimeout(() => scan(), 120);
|
||||
scanTimer = setTimeout(scan, 120);
|
||||
}
|
||||
|
||||
const style = document.createElement('style');
|
||||
|
||||
style.textContent = [
|
||||
'.tm-pting-ip-location {',
|
||||
'display:inline-flex;align-items:center;vertical-align:middle;',
|
||||
@@ -199,8 +248,10 @@
|
||||
'}',
|
||||
'.tm-pting-ip-location:hover { opacity:1; }'
|
||||
].join('');
|
||||
|
||||
document.documentElement.append(style);
|
||||
scan();
|
||||
|
||||
new MutationObserver(scheduleScan).observe(document.documentElement, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
|
||||
Reference in new issue
Block a user