Reddit usernames/icons in feed

Shows Reddit post author username and icon in post headers

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Reddit usernames/icons in feed
// @namespace    http://tampermonkey.net/
// @version      1.3
// @license      MIT
// @description  Shows Reddit post author username and icon in post headers
// @match        https://www.reddit.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    async function addAuthorInfo(authorName, timeElem, iconUrl) {
        if (!authorName || !timeElem) return;

        let authorLink = document.createElement('a');
        authorLink.href = `https://www.reddit.com/user/${authorName}`;
        authorLink.target = "_blank";
        authorLink.rel = "noopener noreferrer";
        authorLink.className = "author-name whitespace-nowrap text-neutral-content visited:text-neutral-content-weak focus-visible:-outline-offset-1 cursor-pointer no-visited no-underline hover:underline font-bold";
        authorLink.style.display = "inline-flex";
        authorLink.style.alignItems = "center";

        const profileImg = document.createElement('img');
        profileImg.src = iconUrl || `https://www.redditstatic.com/avatars/avatar_default_02_24A0ED.png`;
        profileImg.alt = "User";
        profileImg.width = 24;
        profileImg.height = 24;
        profileImg.style.borderRadius = "50%";
        profileImg.style.marginRight = "6px";
        profileImg.style.objectFit = "cover";
        profileImg.style.background = "#eee";
        profileImg.style.position = "relative";
        profileImg.style.top = "8px";

        authorLink.appendChild(profileImg);
        authorLink.appendChild(document.createTextNode(authorName));

        const authorSpan = document.createElement('span');
        authorSpan.className = 'tm-author-info ml-sm text-neutral-content-weak text-12';
        authorSpan.style.display = 'inline-flex';
        authorSpan.style.alignItems = 'center';
        authorSpan.appendChild(authorLink);

        if (timeElem && timeElem.parentElement) {
            if (timeElem.nextSibling) {
                timeElem.parentElement.insertBefore(authorSpan, timeElem.nextSibling);
            } else {
                timeElem.parentElement.appendChild(authorSpan);
            }
        }
    }

    function isFeedPage() {
        const path = window.location.pathname;
        return path === '/' || path.startsWith('/r/popular') || path.startsWith('/user/') || path.startsWith('/r/all');
    }

    function enhanceAuthorInfo() {
        if (!isFeedPage()) return;

        document.querySelectorAll('shreddit-post-overflow-menu').forEach(menu => {
            const authorName = menu.getAttribute('author-name');
            const iconUrl = menu.closest('shreddit-post')?.getAttribute('icon');
            if (!authorName) return;

            let article = menu.closest('article');
            if (!article) return;

            let timeElem = article.querySelector('faceplate-timeago, time');
            if (!timeElem) return;

            if (timeElem.parentElement.querySelector('.tm-author-info')) return;

            addAuthorInfo(authorName, timeElem, iconUrl);
        });

        document.querySelectorAll('shreddit-post[view-type="compactView"]').forEach(post => {
            const authorName = post.getAttribute('author');
            const iconUrl = post.getAttribute('icon');
            if (!authorName) return;

            let timeElem = post.querySelector('faceplate-timeago');
            if (!timeElem) return;

            if (timeElem.parentElement.querySelector('.tm-author-info')) return;

            addAuthorInfo(authorName, timeElem, iconUrl);
        });
    }

    const observer = new MutationObserver(() => enhanceAuthorInfo());
    observer.observe(document.querySelector('body'), { childList: true, subtree: true });

    enhanceAuthorInfo();
})();