AttachHowOldtoUserinPosts

Show how old a user is in posts

目前為 2024-07-16 提交的版本,檢視 最新版本

// ==UserScript==
// @name         AttachHowOldtoUserinPosts
// @namespace    https://jirehlov.com
// @version      1.0
// @description  Show how old a user is in posts
// @author       Jirehlov
// @match        https://bgm.tv/*/topic/*
// @match        https://chii.in/*/topic/*
// @match        https://bangumi.tv/*/topic/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
	const delay = 1000;
	const ageColors = [
		{
			threshold: 2,
			color: "#FFC966"
		},
		{
			threshold: 5,
			color: "#FFA500"
		},
		{
			threshold: 10,
			color: "#F09199"
		},
		{
			threshold: Infinity,
			color: "#FF0000"
		}
	];
	let requestCount = 0;
	function calculateAge(birthDate) {
		const today = new Date();
		const dob = new Date(birthDate);
		let age = today.getFullYear() - dob.getFullYear();
		const monthDiff = today.getMonth() - dob.getMonth();
		if (monthDiff < 0 || monthDiff === 0 && today.getDate() < dob.getDate()) {
			age--;
		}
		return age;
	}
	function fetchAndStoreUserAge(userLink, delayTime) {
		setTimeout(() => {
			$.get(userLink, data => {
				const html = $(data);
				let registrationDate = html.find("ul.network_service li:first span.tip").text().replace(/加入/g, "").trim();
				if (registrationDate) {
					const userId = userLink.split("/").pop();
					localStorage.setItem("userAge_" + userId, registrationDate);
					displayUserAge(userId, registrationDate);
				}
			});
		}, delayTime);
	}
	function displayUserAge(userId, registrationDate) {
		const userAnchor = $("strong a.l[href$='/user/" + userId + "']");
		if (userAnchor.length > 0 && userAnchor.next(".age-badge").length === 0) {
			const userAge = calculateAge(registrationDate);
			if (!isNaN(userAge)) {
				let badgeColor = ageColors.find(color => userAge <= color.threshold).color;
				const badge = `
                    <span class="age-badge" style="
                        background-color: ${ badgeColor };
                        font-size: 11px;
                        padding: 2px 5px;
                        color: #FFF;
                        border-radius: 100px;
                        line-height: 150%;
                        display: inline-block;
                    ">${ userAge }年</span>`;
				userAnchor.after($(badge));
			}
		}
	}
	$("strong a.l:not(.avatar)").each(function () {
		const userLink = $(this).attr("href");
		const userId = userLink.split("/").pop();
		const storedDate = localStorage.getItem("userAge_" + userId);
		if (storedDate) {
			displayUserAge(userId, storedDate);
		} else {
			fetchAndStoreUserAge(userLink, requestCount * delay);
			requestCount++;
		}
	});
}());

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址