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