Comment Smoother for niconico live

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

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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)
    }
})()