跳到正文

科技·工程

洛谷取关提醒

Gary0约 17 分钟
原文

这是 2026年6月14日 存档的第 4 版,不是当前版本。查看当前版本

文章信息与版本

https://greasyfork.org/zh-CN/scripts/582602-%E6%B4%9B%E8%B0%B7%E5%8F%96%E5%85%B3%E6%8F%90%E9%86%92%E5%99%A8

进入洛谷,打开自己的个人主页,就可以用啦。

感谢 @ygg_pls 提供的思路!

JavaScript
// ==UserScript==
// @name			洛谷取关提醒器
// @namespace		http://tampermonkey.net/
// @version			1.4
// @description		洛谷取关提醒器,可以快速检测近期有谁取关了你。
// @author			Gary0
// @license			GNU GPLv3
// @run-at			document-end
// @match			https://www.luogu.com.cn/*
// @icon			https://cdn.luogu.com.cn/upload/usericon/3.png
// @grant			none
// ==/UserScript==

// Copyright 2026 (C) Gary0

const container_id = "fcker";
function esc_html(s)
{
	str = String(s);
	return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#039;');
}
function get_el(id) { return document.getElementById(id); }
function new_el(id) { return document.createElement(id); }
function insert_el(base, label, content, id, class_list)
{
	const el = new_el(label);
	el.innerHTML = content, el.id = id, el.classList = class_list;
	return base.appendChild(el);
}
function insert_css(content) { insert_el(get_el(container_id), "style", content, "", ""); }
function insert_js(content) { insert_el(get_el(container_id), "script", content, "", ""); }
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));

function get_uid()
{
	const m = document.documentElement.outerHTML.match(/<script id="lentille-context" type="application\/json">([\s\S]*?)<\/script>/);
	if (!m) return null;
	const json = JSON.parse(m[1]);
	if (!json?.user?.uid) return null;
	return json.user.uid;
}

let UID = get_uid();
if (UID)
{
	const container = insert_el(document.body, "div", "", container_id, "");
	container.style = "position: fixed; top: 0; left: 0; bottom: 0; right: 0; z-index: 1000; pointer-events: none;";
	insert_css(`
		.fcker-card {
			background-color: #fff;
			box-shadow: 0 1px 3px #1a1a1a1a;
			border-radius: 4px;
			padding: 10px;
			margin: 10px;
			text-align: center;
			overflow-y: auto;
		}
		.fcker-btn {
			background-color: #3498db;
			color: #fff;
			border: none;
			border-radius: 4px;
			padding: 10px;
			margin: 10px;
			text-align: center;
		}
		.fcker-btn:hover {
			cursor: pointer;
		}
		.fcker-user {
			text-align: left;
			height: 35px;
			padding: 10px;
			overflow: hidden;
			white-space: nowrap;
			border-bottom: 1px solid #e8e8e8;
		}
		.fcker-user .left {
			display: inline-block;
			border-radius: 50%;
			height: 100%;
		}
		.fcker-user .right {
			padding-left: 10px;
			display: inline-block;
			height: 100%;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
		}
		.fcker-user .slogan {
			font-size: .875em;
			color: #444;
		}

		#fcker-main {
			position: fixed;
			top: 60px;
			right: -280px;
			width: 300px;
			pointer-events: auto;
			transition: transform 0.2s ease;
		}
		#fcker-main:hover {
			transform: translateX(-260px);
		}
	`);
	const main = insert_el(container, "div", "<h3>取关提醒器</h3>", "fcker-main", "fcker-card");
	const get_btn = insert_el(main, "button", "获取新列表", "", "fcker-btn");
	get_btn.addEventListener("click", async () => {
		main.style.display = "none";
		document.body.style.overflow = "hidden";

		const get_card = insert_el(container, "div", "<h3>粉丝列表</h3>", "", "fcker-card");
		get_card.style = "margin: 0; position: fixed; top: 0; left: 0; bottom: 0; right: 0; padding: 50px; pointer-events: auto;";
		const quit_btn = insert_el(container, "button", "←", "", "fcker-btn");
		quit_btn.style = "background-color: #ddd; position: fixed; top: 0; left: 0; pointer-events: auto;";
		quit_btn.addEventListener("click", () => { location.reload(); });
		const get_card_status = insert_el(get_card, "p", "", "", "", "");
 
		async function parse_followers(html)
		{
			const m = html.match(/<script id="lentille-context" type="application\/json">([\s\S]*?)<\/script>/);
			if (!m) return null;
			const json = JSON.parse(m[1]);
			if (!json?.data?.users?.result) return { success: false, data: "页面解析失败" };
			return { success: true, data: json.data.users.result };
		}
		async function parse_followers_count(html)
		{
			const m = html.match(/<script id="lentille-context" type="application\/json">([\s\S]*?)<\/script>/);
			if (!m) return null;
			const json = JSON.parse(m[1]);
			if (!json?.data?.users?.count) return { success: false, data: "页面解析失败" };
			return { success: true, data: json.data.users.count };
		}
		async function get_followers(uid, page)
		{
			return await fetch(`https://www.luogu.com.cn/user/${uid}/follower?page=${page}`)
				.then(async (data) => {
					const text = await data.text();
					const res = await parse_followers(text);
					return res;
				})
				.catch((err) => { return { success: false, data: `获取粉丝列表失败: ${err}` }; });
		}
		async function get_followers_count(uid)
		{
			return await fetch(`https://www.luogu.com.cn/user/${uid}/follower`)
				.then(async (data) => {
					const text = await data.text();
					const res = await parse_followers_count(text);
					return res;
				})
				.catch((err) => { return { success: false, data: `获取粉丝数量失败: ${err}` }; });
		}
		async function get_all_followers(uid)
		{
			get_card_status.innerText = `loading...`;
			const cdata = await get_followers_count(uid);
			if (cdata.success)
			{
				const count = cdata.data, pages = Math.ceil(count / 20);
				let nc = 0, success = true, list = [];
				get_card_status.innerText = `loading... 0 / ${count}`;
				for (let i = 1; i <= pages; i++)
				{
					get_followers(uid, i).then((res) => {
						if (res.success && success)
						{
							nc += res.data.length, list.push(...res.data);
							get_card_status.innerText = `loading... ${nc} / ${count}`;
						}
						else if (!res.success)
						{
							success = false;
							get_card_status.innerText = res.data;
						}
					});
					await sleep(1000);
				}
				await sleep(3000);
				if (success) return list;
				else return null;
			}
			else
			{
				get_card_status.innerText = cdata.data;
				return null;
			}
		}
 
		let list = await get_all_followers(UID);
		let old_list = null;
		if (localStorage.getItem("fcker")) old_list = JSON.parse(localStorage.getItem("fcker"));
		console.log(old_list);
 
		function get_color(color)
		{
			color = color.toLowerCase();
			if (color === "gray") color = "grey";
			return color;
		}
		function display_followers(list)
		{
			for (const user of list)
			{
				let html = `
					<img src="https://cdn.luogu.com.cn/upload/usericon/${user.uid}.png" class="left" />
					<div class="right">
						<a href="/user/${user.uid}" target="_blank" disabled="false" style="font-weight: bold; font-size: 1em; color: var(--lfe-color--${get_color(user.color)}-3)">${esc_html(user.name)}</a>
						<span style="font-size: 1em; color: var(--lfe-color--${get_color(user.color)}-3)">`;
				if (user.badge) html += ` [${esc_html(user.badge)}]`;
				if (user.ccfLevel) html += ` (${user.ccfLevel})`;
				if (user.xcpcLevel) html += ` (${user.xcpcLevel})`;
				html += `</span>
						<div class="slogan">${esc_html(user.slogan)}</div>
					</div>
				`;
				insert_el(get_card, "div", html, "", "fcker-user");
			}
		}
		function save_followers()
		{
			localStorage.setItem("fcker", JSON.stringify(list));
		}
		function comp_followers()
		{
			if (!old_list)
			{
				alert("还没有旧列表,请先保存喵");
				return;
			}
			const old_map = new Map();
			for (const u of old_list) old_map.set(u.uid, u);
			const now_map = new Map();
			for (const u of list) now_map.set(u.uid, u);
			const unfollowed = [], new_followers = [];
			for (const [uid, user] of old_map.entries())
				if (!now_map.has(uid)) unfollowed.push(user);
			for (const [uid, user] of now_map.entries())
				if (!old_map.has(uid)) new_followers.push(user);
			get_card.innerHTML = "<h3>粉丝列表</h3>";
			const save_btn = insert_el(get_card, "button", "覆盖旧列表", "", "fcker-btn");
			save_btn.addEventListener("click", save_followers);
			insert_el(get_card, "h4", "取关用户", "", "");
			if (unfollowed.length === 0) insert_el(get_card, "p", "无", "", "");
			display_followers(unfollowed);
			insert_el(get_card, "h4", "新粉丝", "", "");
			if (new_followers.length === 0) insert_el(get_card, "p", "无", "", "");
			display_followers(new_followers);
		}
		function display_all_followers(list)
		{
			get_card.innerHTML = "<h3>粉丝列表</h3>";
			const comp_btn = insert_el(get_card, "button", "对比差异", "", "fcker-btn");
			const save_btn = insert_el(get_card, "button", "覆盖旧列表", "", "fcker-btn");
			comp_btn.addEventListener("click", comp_followers);
			save_btn.addEventListener("click", save_followers);
			display_followers(list);
		}
		if (list) display_all_followers(list);
	});
}