ZhihuPostCreatedAt

在知乎首页帖子标题下方展示发帖时间

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ZhihuPostCreatedAt
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @description  在知乎首页帖子标题下方展示发帖时间
// @author       You
// @match        https://www.zhihu.com/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=zhihu.com
// @grant        none
// @license MIT
// ==/UserScript==

function debounce(wait, func) {
    let timeoutId = null;
    return function debounced(...args) {
        if (timeoutId) {
            clearTimeout(timeoutId);
        }
        timeoutId = setTimeout(() => {
            func(...args);
            timeoutId = null;
        }, wait);
    };
}
function formatDate(date) {
    if (!(date instanceof Date)) {
        throw new Error("Invalid input: Please provide a Date object.");
    }

    const pad = (num) => (num < 10 ? '0' + num : num);

    const year = date.getFullYear();
    const month = pad(date.getMonth() + 1);
    const day = pad(date.getDate());
    const hours = pad(date.getHours());
    const minutes = pad(date.getMinutes());
    const seconds = pad(date.getSeconds());

    return `${year}-${month}-${day} ${hours}:${minutes}`;
}


(function() {
    'use strict';

    function main() {
        let offset = 0;
        /**
         * 获取页面卡片列表,并插入时间显示
         */
        const insertCreatedAndUpdateTimeForCards = debounce(800, () => {
            const cards = Array.from(document.getElementsByClassName("TopstoryItem"));
            for (let i = offset; i < cards.length; i += 1) {
                insertCreatedAndUpdatedTime(cards[i]);
            }
            offset = cards.length;
        });
        const cache = {};
        function insertCreatedAndUpdatedTime(card) {
            let $created = card.querySelector('meta[itemprop="dateCreated"]');
            if (!$created) {
                $created = card.querySelector('meta[itemprop="datePublished"]');
            }
            const $url = card.querySelector('meta[itemprop="url"]');
            if (!$url || !$created) {
                return;
            }
            const url = $url.getAttribute('content');
            const created = $created.getAttribute("content");
            if (cache[url]) {
                return;
            }
            const $title = card.querySelector(".ContentItem-title");
            const title = $title.innerText;
            if (!$title) {
                return;
            }
            const $created2 = document.createElement("div");
            $created2.style.cssText = "margin: 8px 0; font-size: 14px; color: var(--GBL05A)";
            $created2.textContent = "发布于 " + formatDate(new Date(created));
            $title.parentNode.insertBefore($created2, $title.nextSibling);
            cache[url] = true;
        }

        const observer = new MutationObserver((mutationsList) => {
            for (let mutation of mutationsList) {
                if (mutation.type === 'childList') {
                    //
                    insertCreatedAndUpdateTimeForCards();
                }
            }
        });
        observer.observe(document.getElementById("TopstoryContent"), {
            childList: true,
            subtree: true,
        });
    }
    main();
})();