Comment Smoother for niconico live

ニコニコ生放送のコメントをぬるぬるにします

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Comment Smoother for niconico live
// @description ニコニコ生放送のコメントをぬるぬるにします
// @namespace   https://rinsuki.net/
// @match       https://live.nicovideo.jp/watch/*
// @grant       none
// @version     1.0
// @author      rinsuki
// @license     MIT
// ==/UserScript==

(() => {
    if (!navigator.userAgent.includes("Firefox/")) return
    const origGetter = HTMLVideoElement.prototype.__lookupGetter__("currentTime")
    const origSetter = HTMLVideoElement.prototype.__lookupSetter__("currentTime")
    class Smoother {
        lastCurrentTime = 0
        lastNow = 0
        /**
         * @param {HTMLVideoElement} elm 
         */
        currentTime(elm) {
            const currentTime = origGetter.call(elm)
            if (elm.paused) {
                return currentTime
            }
            if (this.lastCurrentTime !== currentTime) {
                // 更新された
                this.lastCurrentTime = currentTime
                this.lastNow = performance.now()
                return currentTime
            }
            const now = performance.now()
            const diff = now - this.lastNow
            if (diff > 1000) {
                // 一秒ずれてるというのはおかしい…ので帰る
                return currentTime
            }
            const currentNow = this.lastCurrentTime + (diff / 1000)
            return currentNow
        }
    }
    if (origGetter != null && origSetter != null) {
        HTMLVideoElement.prototype.__defineGetter__("currentTime", function() {
            if (!("__userjs_smoother" in this)) {
                console.log("new smoother", this)
                this.__userjs_smoother = new Smoother()
            }
            return this.__userjs_smoother.currentTime(this)
        })
        HTMLVideoElement.prototype.__defineSetter__("currentTime", function() {
            origSetter.apply(this, arguments)
        })
    } else {
        console.log("[nicolive-comment-smoother]", "origGetter/origSetter is not available", origGetter, origSetter)
    }
})()