niconico slot count changer

ニコニコ動画の最大同時コメント表示数を変更できるUserScriptです。デフォルトでは標準と同じ40になっているので、GM_valueのSlotCountを変更してください。※注意: 増やせば増やすほどメモリを食います(実際にそれだけのコメントがなくても)

目前為 2020-11-07 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        niconico slot count changer
// @namespace   rinsuki.net
// @match       https://www.nicovideo.jp/watch/*
// @version     1.0.1
// @grant       GM_getValue
// @grant       GM_setValue
// @author      rinsuki
// @description ニコニコ動画の最大同時コメント表示数を変更できるUserScriptです。デフォルトでは標準と同じ40になっているので、GM_valueのSlotCountを変更してください。※注意: 増やせば増やすほどメモリを食います(実際にそれだけのコメントがなくても)
// ==/UserScript==

(() => {
    const loadedSlotCount = GM_getValue("SlotCount")
    const EXPECT_SLOT_COUNT = Math.min(100000, typeof loadedSlotCount === "number" && Number.isSafeInteger(loadedSlotCount) && loadedSlotCount > 0 ? loadedSlotCount : 40)
    GM_setValue("SlotCount", EXPECT_SLOT_COUNT)

    function getReactInternalInstance(dom) {
        for (const key in dom) {
            if (key.startsWith("__reactInternalInstance$")) return dom[key]
        }
    }
    const observer = new unsafeWindow.MutationObserver(mutations => {
            for (const mutation of mutations) {
                if (mutation.target.id === "CommentRenderer") {
                    const commentRenderer = mutation.target
                    const commentRendererInstance = getReactInternalInstance(commentRenderer).return.stateNode.renderer
                    /** @type {Function} */
                    const orig = commentRendererInstance.__proto__.registerLayerProcessorEvents
                    commentRendererInstance.__proto__.registerLayerProcessorEvents = function(layer) {
                        /** @type {{_stagingList: unknown[], reservedList: unknown[]}} */
                        const slotRepository = layer.slotRepository
                        const slotClass = (slotRepository._stagingList[0] ?? slotRepository.reservedList[0]).constructor
                        while (true) {
                            const currentSlot = slotRepository._stagingList.length + slotRepository.reservedList.length
                            if (currentSlot > EXPECT_SLOT_COUNT) {
                                if (slotRepository.reservedList.length) {
                                    slotRepository.reservedList.pop()
                                } else {
                                    slotRepository._stagingList.pop()
                                }
                            } else if (currentSlot < EXPECT_SLOT_COUNT) {
                                const slot = new slotClass
                                slotRepository.reservedList.push(slot)
                            } else {
                                break
                            }
                        }
                        orig.call(this, layer)
                    }
                    observer.disconnect()
                    break
                }
            }
    })
    observer.observe(document.getElementById("js-app"), {
        childList: true,
        subtree: true,
    })
})()