Greasy Fork is available in English.

ニコニコ動画(Re:仮)コメントフィルター(仮)

Filter out specific comments from JSON response

// ==UserScript==
// @name         ニコニコ動画(Re:仮)コメントフィルター(仮)
// @namespace    http://tampermonkey.net/
// @version      0.11
// @description  Filter out specific comments from JSON response
// @author       Velgail
// @match        https://www.nicovideo.jp/watch_tmp/*
// @grant        none
// @license      The Unlicense
// ==/UserScript==

(function() {
    'use strict';

    // List of NG comments
    const ngComments = [
        "んん~まかァァウッッ!!!!🤏😎",
        "にょ、にょまれ~~✋🐮🤚💦",
        // ここに他のNGコメントを追加
    ];

    // Intercept the fetch function
    const originalFetch = window.fetch;
    window.fetch = function(url, options) {
        if (url.startsWith('https://nvapi.nicovideo.jp/v1/tmp/comments/')) {
            return originalFetch(url, options).then(response => {
                return response.json().then(data => {
                    // Filter the comments here
                    if (data.data && data.data.comments) {
                        data.data.comments = data.data.comments.filter(comment => !ngComments.includes(comment.message));
                    }
                    // Create a new Response object with the modified data
                    const modifiedResponse = new Response(JSON.stringify(data), {
                        status: response.status,
                        statusText: response.statusText,
                        headers: response.headers
                    });
                    return modifiedResponse;
                });
            });
        }
        return originalFetch(url, options);
    };

    // Intercept the XMLHttpRequest
    const originalOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url) {
        this._url = url;
        return originalOpen.apply(this, arguments);
    };

    const originalSend = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send = function() {
        this.addEventListener('readystatechange', function() {
            if (this.readyState === 4 && this._url.startsWith('https://nvapi.nicovideo.jp/v1/tmp/comments/')) {
                try {
                    const jsonResponse = JSON.parse(this.responseText);
                    // Filter the comments here
                    if (jsonResponse.data && jsonResponse.data.comments) {
                        jsonResponse.data.comments = jsonResponse.data.comments.filter(comment => !ngComments.includes(comment.message));
                    }
                    // Override the responseText property
                    Object.defineProperty(this, 'responseText', { value: JSON.stringify(jsonResponse) });
                } catch (e) {
                    console.error('Failed to modify JSON response', e);
                }
            }
        });
        return originalSend.apply(this, arguments);
    };
})();