zhihutime

Better show the latest edit/post time before the main content.

目前為 2022-05-02 提交的版本,檢視 最新版本

// ==UserScript==
// @name         zhihutime
// @version      0.1
// @description  Better show the latest edit/post time before the main content.
// @author       [email protected]
// @match        https://www.zhihu.com/question/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=zhihu.com
// @grant        GM_addStyle
// @require      https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js
// @license      MIT
// @namespace https://gf.qytechs.cn/users/909394
// ==/UserScript==

(function() {
    'use strict';
    // render datetime warning before the main content
    const render = function() {
        Array.from(document.querySelectorAll(`.AnswerItem`)).map(answer => {
            const date_modified = answer.querySelector(`meta[itemprop='dateModified']`).getAttribute('content');
            const duration = moment.duration(moment().diff(moment(date_modified)));
            const node = document.createElement('div');
            // year
            const y = duration.get('year');
            // month
            const m = duration.get('month');
            // day
            const d = duration.get('day');
            // hour
            const h = duration.get('hour');

            node.classList.add('warning-date');

            if (y !== 0) {
                // years ago
                node.classList.add('warning-date-year');
                node.innerHTML = `⚠️ 回答于${y}年前`;
            } else if (m !== 0) {
                // months ago
                node.classList.add('warning-date-month');
                node.innerHTML = `⚠️ 回答于${m}个月前`;
            } else if (d !== 0) {
                // some days ago
                node.classList.add('warning-date-day');
                node.innerHTML = `ℹ 回答于${d}天前`;
            } else if (h !== 0) {
                // just some hours ago
                node.classList.add('warning-date-hour');
                node.innerHTML = `ℹ 回答于${h}小时前`;
            }

            const style = `
            .warning-date {
            -webkit-text-size-adjust: 100%;
            word-break: normal;
            font-size: 1.4rem;
            margin-bottom: 10px;
            display: flex;
            -webkit-box-align: center;
            align-items: center;
            padding: 10px 16px;
            background-color: #fcf0bf;
            color: rgba(0, 0, 0, 0.57);
            line-height: 1.5;
            font-weight: 600;
            border-radius: 3px;
        }

        .warning-date-year, .warning-date-month {
            background-color: #fcf0bf;
        }
        .warning-date-day, .warning-date-hour {
            background-color: #cafcca;
        }
        `;

            GM_addStyle(style);

            // insert date warning before the main content.
            const first = answer.firstChild;
            if (first.classList[0] !== 'warning-date') {
                answer.insertBefore(node, answer.firstChild);
            }

        });
    };

    render();

    // 选择需要观察变动的节点
    const targetNode = document.querySelector(`.AnswersNavWrapper [role='list']`);

    // 观察器的配置(需要观察什么变动)
    const config = { attributes: false, childList: true, subtree: false };

    // 当观察到变动时执行的回调函数
    const callback = function(mutationsList, observer) {
        // Use traditional 'for loops' for IE 11
        for(let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                // console.log('A child node has been added or removed.');
                render();
            }
        }
    };

    // 创建一个观察器实例并传入回调函数
    const observer = new MutationObserver(callback);

    // 以上述配置开始观察目标节点
    observer.observe(targetNode, config);

    // 之后,可停止观察
    // observer.disconnect();


})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址