Linux.do 显示楼层号

在 Linux.do 论坛帖子中显示楼层号,并处理过长的用户名显示

// ==UserScript==
// @name         Linux.do 显示楼层号
// @name:en      Linux.do Floor Number Display
// @namespace    https://greasyfork.org/zh-TW/users/1252908-eep
// @version      0.1.5
// @description  在 Linux.do 论坛帖子中显示楼层号,并处理过长的用户名显示
// @description:en  Display floor numbers in Linux.do forum posts and handle long usernames
// @author       EEP
// @license      MIT
// @match        https://linux.do/*
// @icon         https://linux.do/favicon.ico
// @grant        none
// @run-at       document-end
// @supportURL   https://github.com/[你的GitHub用户名]/[仓库名]/issues
// @homepageURL  https://github.com/[你的GitHub用户名]/[仓库名]
// @keywords     linux.do,论坛,楼层号,floor number,discourse
// @tag          linux
// @tag          linux.do
// @tag          forum
// @tag          discourse
// ==/UserScript==

(function () {
  'use strict';

  // 修改样式部分
  const style = document.createElement('style');
  style.textContent = `
        .floor-number {
            margin-left: 8px;
            color: #666;
            font-size: 12px;
        }
        .names.trigger-user-card {
            display: flex;
            align-items: center;
        }
        .first.full-name {
            max-width: 200px;
        }
        .first.full-name a {
            display: block;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        /* 添加对 post-infos 中用户名的控制 */
        .post-infos a {
            max-width: 200px;
            display: inline-block;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            vertical-align: bottom;
        }
    `;
  document.head.appendChild(style);

  // 主函数
  function addFloorNumbers() {
    const posts = document.querySelectorAll('.topic-post');

    posts.forEach((post) => {
      const article = post.querySelector('article');
      if (!article) return;

      const floorNumber = article.id.replace('post_', '');
      const namesDiv = post.querySelector('.names.trigger-user-card');

      if (namesDiv && !namesDiv.querySelector('.floor-number')) {
        const floorSpan = document.createElement('span');
        floorSpan.className = 'floor-number';
        floorSpan.textContent = `#${floorNumber}`;
        namesDiv.appendChild(floorSpan);
      }
    });
  }

  // 初始执行
  addFloorNumbers();

  // 监听动态加载的内容
  const observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      if (mutation.addedNodes.length) {
        addFloorNumbers();
      }
    });
  });

  observer.observe(document.body, {
    childList: true,
    subtree: true,
  });
})();