Greasy Fork

Greasy Fork is available in English.

知乎上的回答/专栏的编辑时间放在第一行

有些问题的回答会具有时效性.如果答案很长,还要翻很长的页面去看编辑时间,用户体验极差.所以我写了这个脚本

当前为 2019-02-22 提交的版本,查看 最新版本

// ==UserScript==
// @name         知乎上的回答/专栏的编辑时间放在第一行
// @namespace    http://tampermonkey.net/
// @version      0.0.2
// @description  有些问题的回答会具有时效性.如果答案很长,还要翻很长的页面去看编辑时间,用户体验极差.所以我写了这个脚本
// @author       floatsyi
// @license      MIT
// @match        *://www.zhihu.com/question/*
// @match        *://zhuanlan.zhihu.com/p/*
// @grant        none
// ==/UserScript==
/*jshint esversion: 6 */
(function() {
    'use strict'
     if (window.location.href.search(/zhuanlan/i) !== -1) {
          document.querySelector('header.Post-Header').appendChild(document.querySelector('.ContentItem-time').cloneNode(true))
          return false
     }

     const _ = {}
     _.debounce = function (func, wait) {
         var lastCallTime
         var lastThis
         var lastArgs
         var timerId

         function startTimer (timerExpired, wait) {
             return setTimeout(timerExpired, wait)
         }

         function remainingWait(time) {
             const timeSinceLastCall = time - lastCallTime
             const timeWaiting = wait - timeSinceLastCall
             return timeWaiting
         }

         function shoudInvoking (time) {
             return lastCallTime !== undefined && (time - lastCallTime >= wait)
         }

         function timerExpired () {
             const time = Date.now()
             if (shoudInvoking(time)) {
                 return invokeFunc()
             }
             timerId = startTimer(timerExpired, remainingWait(time))
         }

         function invokeFunc () {
             timerId = undefined
             const args = lastArgs
             const thisArg = lastThis
             let result = func.apply(thisArg, args)
             lastArgs = lastThis = undefined
             return result
         }

         function debounced (...args) {
             let time = Date.now()
             lastThis = this
             lastArgs = args
             lastCallTime = time
             if (timerId === undefined) {
                 timerId = startTimer(timerExpired, wait)
             }
         }

         return debounced
     }

     window.onload = function () {
         const AnswerCard = document.querySelector('.Card.AnswerCard')

         const hasEle = function (el) {
             let RichContent = el.querySelector('.ContentItem .RichContent')
             let RichContent_inner = RichContent.querySelector('.RichContent-inner')
             let editTime = RichContent_inner.nextSibling

             if (editTime.nodeName === 'BUTTON') return false

             if (!!RichContent && !!RichContent_inner && !!editTime) {
                 return {
                     RichContent,
                     RichContent_inner,
                     editTime
                 }
             }

             return false
         }

         const doing = function (eles) {
             if (!eles) return false
             if (eles.RichContent.firstChild.firstChild.className === 'ContentItem-time') return false
             eles.RichContent.insertBefore(eles.editTime.cloneNode(true), eles.RichContent_inner)
             return true
         }

         doing(hasEle(AnswerCard))

         let time = 0
         const zhihuTV = document.querySelector('main')
         // 监听DOM变更
         const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
         const option = {
             'childList': true,
             'subtree': true
         }
         const doNotTranslateCode = function (mutations, observer) {
             if (time >= 20) {
                 observer.disconnect()
                 observer.takeRecords()
                 time = 0
                 setTimeout(function() {
                     !!zhihuTV && observer.observe(zhihuTV, option)
                 }, 50)
             }
             let list = document.querySelectorAll('.List .List-item')
             list.forEach(function(item){
                 doing(hasEle(item))
             })
             time++
         }
         const mo = new MutationObserver(_.debounce(doNotTranslateCode, 50))
         !!zhihuTV && mo.observe(zhihuTV, option)

     }
})()