Kbin Kibby Avatars

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
});