安装之后,先编辑一下脚本中的 UID 变量,改成你的洛谷 uid,保存。
之后进入洛谷,打开自己的粉丝页面,再刷新就可以用啦。
// ==UserScript==
// @name 洛谷取关提醒器
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 洛谷取关提醒器,可以快速检测近期有谁取关了你。
// @author Gary0
// @license MIT
// @run-at document-end
// @match https://www.luogu.com.cn/*/follower*
// @icon https://cdn.luogu.com.cn/upload/usericon/3.png
// @grant none
// ==/UserScript==
let UID = 3; // 替换为你的洛谷 UID
const container_id = "fcker";
function esc_html(s)
{
str = String(s);
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
}
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));
if (window.location.href.includes(UID.toString() + "/follower"))
{
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;
}
`);
const main = insert_el(document.body, "div", "<h3>取关提醒器</h3>", "fcker-main", "fcker-card");
main.style = "position: fixed; top: 60px; right: 0; width: 300px; pointer-events: auto;";
const get_btn = insert_el(main, "button", "获取新列表", "", "fcker-btn");
get_btn.addEventListener("click", async () => {
main.style.display = "none";
const get_card = insert_el(container, "div", "<h3>粉丝列表</h3>", "", "fcker-card", "");
get_card.style = "position: fixed; top: 60px; left: 60px; bottom: 60px; right: 60px; pointer-events: auto;";
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);
}
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()
{
const save_btn = insert_el(get_card, "button", "覆盖旧列表", "", "fcker-btn");
save_btn.addEventListener("click", save_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>";
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);
});
}