Kbin Kibby Avatars

Displays a Kibby icon as the default for people with no avatars set.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Kbin Kibby Avatars
// @namespace    http://tampermonkey.net/
// @version      0.3.1
// @description  Displays a Kibby icon as the default for people with no avatars set.
// @author       minnieo
// @match        https://kbin.social/*
// @icon         https://minnie.untone.uk/kibpfps/kib1.png
// @grant        none
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

const pref = 'https://minnie.untone.uk/kibpfps/kib'
const max = 25;

const commentSection = document.querySelector('section.comments.entry-comments.comments-tree');
const noAvatarElements = Array.from(commentSection.querySelectorAll('div.no-avatar'));

const filledUsers = {}

noAvatarElements.forEach((defaultAvatar) => {
    let assignedAvatar;
    const username = defaultAvatar.parentElement.href.split('/')[4]
    if (filledUsers[username]) {
        assignedAvatar = filledUsers[username];
    } else {
        const randomIndex = Math.floor(Math.random() * max) + 1;
        assignedAvatar = randomIndex
        filledUsers[username] = assignedAvatar;

    }
    const kibbyAvatar = document.createElement('img');
    kibbyAvatar.alt = 'Default avatar';
    kibbyAvatar.src = pref + assignedAvatar + '.png';
    kibbyAvatar.style.cssText = `
        max-width: 40px;
        max-height: 40px;
    `;
    defaultAvatar.parentNode.replaceChild(kibbyAvatar, defaultAvatar);
});