TagBangumiUserPro 班固米用户标记

Based on AttachHowOldtoUserinPosts, to tag users in Bangumi. Now with clickable badges to edit notes.

当前为 2025-02-04 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         TagBangumiUserPro 班固米用户标记
// @version      2.5
// @description  Based on AttachHowOldtoUserinPosts, to tag users in Bangumi. Now with clickable badges to edit notes.
// @author       age_anime
// @match        https://bgm.tv/*
// @match        https://chii.in/*
// @match        https://bangumi.tv/*
// @grant        none
// @license      MIT
// @namespace https://greasyfork.org/users/1426310
// ==/UserScript==
(function () {
    'use strict';

    // 颜色管理
    const noteColors = {
        "BGMER": "#40E0D0", // 自用
        "Wiki管理": "#BB44BB", // Wiki管理员
        "官方管理": "#000000", // 官方开发者
        "大佬": "#DD6D22", // 大佬
        "巨魔": "#CC3333", // 巨魔
        "大明星": "#DD6D22", // 大明星
        "……": "#5E6BA2", // 自己设置去
        "知名人士": "#3C3CC4" // 知名人士
        // 可自行加入其他名称和颜色
    };
    const defaultNote = "bgmer"; // 未被标记的成员的默认备注,也可以改
    // const defaultColor = "#66AA55"; // 默认颜色,护眼绿(比纯绿颜色深点)
    const defaultColor = "rgba(102, 170, 85, 1)"; // 等同于"#66AA55",最后一个参数是透明度

    // 创建并显示用户标记(badge)的函数
    function displayUserNote(userId, note) {
        const userAnchor = $("strong a.l[href='/user/" + userId + "']");
        if (userAnchor.length > 0 && userAnchor.next(".note-badge").length === 0) {

            // 获取颜色或者使用默认
            const badgeColor = noteColors[note] || defaultColor;

            // 创建标记
            const badge = $(`
                <span class="note-badge" style="
                    background-color: ${badgeColor};
                    font-size: 11px;
                    padding: 2px 5px;
                    color: #FFF;
                    border-radius: 100px;
                    line-height: 150%;
                    display: inline-block;
                    cursor: pointer;
                " title="点击编辑备注">${note}</span>`);

            // 添加点击事件,允许编辑备注
            badge.click(function () {
                const newNote = prompt("请输入新的备注:", note);
                if (newNote !== null && newNote.trim() !== "") {
                    // 更新备注并保存
                    note = newNote.trim();
                    localStorage.setItem("userAge_" + userId, note);
                    // 更新标记显示
                    badge.text(note);
                    // 更新标记颜色
                    const newBadgeColor = noteColors[note] || defaultColor;
                    badge.css("background-color", newBadgeColor);
                }
            });

            userAnchor.after(badge);
        }
    }

    // 遍历页面中用户链接,显示标记
    function markUserNotes() {
        $("strong a.l:not(.avatar)").each(function () {
            const userLink = $(this).attr("href"); // e.g., '/user/12345'
            const userId = userLink.split("/").pop();
            let note = localStorage.getItem("userAge_" + userId);
            if (note) {
                displayUserNote(userId, note);
            } else {
                // 如果不存在,为其赋予默认标签,保存并显示
                note = defaultNote;
                localStorage.setItem("userAge_" + userId, note);
                displayUserNote(userId, note);
            }
        });
    }

    // 导入用户数据
    function importUserNotes() {
        const input = document.createElement("input");
        input.type = "file";
        input.accept = "application/json";
        input.onchange = function (event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function (e) {
                    try {
                        const userNotes = JSON.parse(e.target.result);
                        Object.keys(userNotes).forEach(key => localStorage.setItem(key, userNotes[key]));
                        alert("导入成功");
                    } catch (error) {
                        alert("无效的JSON文件:" + error);
                    }
                };
                reader.readAsText(file);
            }
        };
        input.click();
    }

    // 导出用户数据
    function exportUserNotes() {
        const userNotes = {};
        Object.keys(localStorage).forEach(key => {
            if (key.startsWith("userAge_")) {
                userNotes[key] = localStorage.getItem(key);
            }
        });
        const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
        const entryCount = Object.keys(userNotes).length;
        const filename = `userNotes_${timestamp}_${entryCount}entries.json`;
        const blob = new Blob([JSON.stringify(userNotes, null, 2)], { type: "application/json" });
        const url = URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.href = url;
        a.download = filename;
        a.click();
        URL.revokeObjectURL(url);
    }

    // 添加导入/导出按钮的功能,延迟加载不影响页面首屏显示
    function addButtonFunctions() {
        const badgeUserPanel = document.querySelector("ul#badgeUserPanel");
        if (badgeUserPanel) {
            const importButton = document.createElement("a");
            importButton.href = "#";
            importButton.textContent = "导入用户数据";
            importButton.onclick = event => {
                event.preventDefault();
                importUserNotes();
            };
            const exportButton = document.createElement("a");
            exportButton.href = "#";
            exportButton.textContent = "导出用户数据";
            exportButton.onclick = event => {
                event.preventDefault();
                exportUserNotes();
            };
            const listItem = document.createElement("li");
            listItem.appendChild(importButton);
            badgeUserPanel.appendChild(listItem);
            const exportListItem = document.createElement("li");
            exportListItem.appendChild(exportButton);
            badgeUserPanel.appendChild(exportListItem);
        }
    }

    // 如果页面DOM已就绪,则立即执行用户标记显示,否则等到 window.onload
    if (document.body) {
        markUserNotes();
    } else {
        window.onload = markUserNotes;
    }

    // 延迟加载按钮编辑备注功能(这里延迟1秒,可根据需要调整)
    setTimeout(addButtonFunctions, 1000);
})();