Based on AttachHowOldtoUserinPosts, to tag users in Bangumi. Now with clickable badges to edit notes.
当前为
// ==UserScript==
// @name TagBangumiUserPro
// @version 2.2
// @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"; // 默认颜色,护眼绿(比纯绿颜色深点)
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 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);
}
// 添加按钮
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);
}
// 展示用户标签,并添加点击编辑功能
$("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);
}
});
})();