AttachHowOldtoUserinPosts

Show how old a user is in posts

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

// ==UserScript==
// @name         AttachHowOldtoUserinPosts
// @namespace    https://jirehlov.com
// @version      1.4
// @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/*
// @match        https://bgm.tv/character/*
// @match        https://chii.in/character/*
// @match        https://bangumi.tv/character/*
// @match        https://bgm.tv/person/*
// @match        https://chii.in/person/*
// @match        https://bangumi.tv/person/*
// @match        https://bgm.tv/index/*
// @match        https://chii.in/index/*
// @match        https://bangumi.tv/index/*
// @match        https://bgm.tv/blog/*
// @match        https://chii.in/blog/*
// @match        https://bangumi.tv/blog/*
// @match        https://bgm.tv/ep/*
// @match        https://chii.in/ep/*
// @match        https://bangumi.tv/ep/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
	const delay = 2000;
	const ageColors = [
		{
			threshold: 2,
			color: "#FFC966"
		},
		{
			threshold: 5,
			color: "#FFA500"
		},
		{
			threshold: 10,
			color: "#F09199"
		},
		{
			threshold: Infinity,
			color: "#FF0000"
		}
	];
	let requestCount = 0;
	let usersToFetch = [];
	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(() => {
			$.ajax({
				url: userLink,
				dataType: "html",
				success: function (data) {
					const parser = new DOMParser();
					const doc = parser.parseFromString(data, "text/html");
					let registrationDate = $(doc).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 {
			usersToFetch.push(userLink);
		}
	});
	usersToFetch = [...new Set(usersToFetch)];
	console.log("Users to fetch:", usersToFetch);
	usersToFetch.forEach((userLink, index) => {
		fetchAndStoreUserAge(userLink, index * delay);
	});
}());

QingJ © 2025

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