linux.do 帖子列表显示创建时间

帖子列表显示创建时间

目前為 2024-07-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name         linux.do 帖子列表显示创建时间
// @namespace    https://linux.do
// @version      0.0.1
// @description  帖子列表显示创建时间
// @author       anghunk
// @match        *://*.linux.do/*
// @icon         https://cdn.linux.do/uploads/default/optimized/3X/9/d/9dd49731091ce8656e94433a26a3ef36062b3994_2_32x32.png
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  function formattedDate(time) {
    const timestamp = Number(time); // 将字符串转换为数字类型
    const date = new Date(timestamp);

    // 获取当前日期
    const now = new Date();
    const isToday = now.getFullYear() === date.getFullYear() &&
      now.getMonth() === date.getMonth() &&
      now.getDate() === date.getDate();

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1,并且确保两位数
    const day = String(date.getDate()).padStart(2, '0'); // 确保两位数
    const hours = String(date.getHours()).padStart(2, '0'); // 确保两位数
    const minutes = String(date.getMinutes()).padStart(2, '0'); // 确保两位数
    const seconds = String(date.getSeconds()).padStart(2, '0'); // 确保两位数

    if (isToday) {
      return `${hours}:${minutes}:${seconds}`;
    } else {
      return `${month}/${day} ${hours}:${minutes}:${seconds}`;
    }
  }

  function convertToTimestamp(dateStr) {
    // 创建一个正则表达式来匹配日期和时间部分
    var datePattern = /(\d{4}) 年 (\d{1,2}) 月 (\d{1,2}) 日 (\d{2}):(\d{2})/;
    var dateMatch = dateStr.match(datePattern);

    if (dateMatch) {
      var year = parseInt(dateMatch[1], 10);
      var month = parseInt(dateMatch[2], 10) - 1; // 月份从0开始
      var day = parseInt(dateMatch[3], 10);
      var hours = parseInt(dateMatch[4], 10);
      var minutes = parseInt(dateMatch[5], 10);

      // 创建 Date 对象
      var date = new Date(year, month, day, hours, minutes);
      return date.getTime(); // 返回时间戳
    } else {
      return null; // 日期格式无效
    }
  }

  function init() {
    $('.topic-list .age').each(function () {
      const str = $(this).attr('title');
      var match = str.match(/创建日期:([\s\S]*?)最新:/);

      if (match && match[1]) {
        var creationDate = match[1].trim();
        var timestamp = convertToTimestamp(creationDate);
      }
      if ($(this).find(".linuxtime").length < 1) {
        $('.post-activity').attr('style', 'white-space:nowrap;display:inline-block;width:100%;text-align:left;');

        if (timestamp) {
          $(this).find('.post-activity').append(`<span class="linuxtime">(${formattedDate(timestamp)})</span>`);
        }
      }
    });
  }
  init();

  document.addEventListener('click', function (event) {
    init();
  });

  setInterval(() => {
    // if ($('.post-activity .linuxtime').length == 0) {
    init();
    // }
  }, 1000);

  $(document).ready(function () {
    const targetNode = document.querySelector('.topic-list-body');

    const observer = new MutationObserver(function (mutationsList, observer) {
      for (let mutation of mutationsList) {
        if (mutation.type === 'childList') {
          const rowCount = $('.topic-list-body tr').length;
          init();
        }
      }
    });

    // 配置观察选项
    const config = {
      childList: true,
      subtree: true
    };

    observer.observe(targetNode, config);

  });

})();
长期地址
遇到问题?请前往 GitHub 提 Issues。