twitter text comments only

Twitter script to curate and display only text comments that are relevant and meaningful, filtering out unrelated memes and nonsensical content.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name         twitter text comments only
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Twitter script to curate and display only text comments that are relevant and meaningful, filtering out unrelated memes and nonsensical content.
// @author       You
// @match https://twitter.com/*/status/*

// ==/UserScript==

(function () {
    'use strict';
    function injectCss() {
        // Create a <style> element
        var styleElement = document.createElement('style');
        styleElement.id = "text-comments-only";
        // Define your CSS rules
        var cssCode = `
            [data-testid="cellInnerDiv"]:nth-child(1) {
            display: unset !important;
            }
        `;
        // Add your CSS rules to the <style> element
        styleElement.textContent = cssCode;
        document.head.appendChild(styleElement);
    }


    // Select the node that will be observed for mutations
    const targetNode = document.body;

    // Options for the observer (which mutations to observe)
    const config = { attributes: true, childList: true, subtree: true };

    // Create an observer instance linked to the callback
    const observer = new MutationObserver(callback);

    // Start observing the target node for configured mutations
    observer.observe(targetNode, config);

    // Callback function to execute when mutations are observed
    function callback(mutationList) {
        for (const mutation of mutationList) {
            const isNewNodeAdded = mutation.addedNodes && mutation.addedNodes?.length > 0;
            const isStyleInjected = document.getElementById("text-comments-only");

            if (isNewNodeAdded && /^https?:\/\/(www\.)?twitter\.com\/.*\/status\/\d{19}$/.test(location.href)) {
                const node = mutation.addedNodes[0];
                if (node.classList?.length > 0) {
                    const tweetPosts = document.querySelectorAll(`.${node.classList[0]} [data-testid="cellInnerDiv"]:not([textonly])`)
                    if (tweetPosts.length > 0) {
                        tweetPosts.forEach(el => {
                            const isValidVideo = el.querySelectorAll('[aria-label="Embedded video"]').length > 0;
                            const isValidImg = el.querySelectorAll('[alt="Image"]').length > 0;
                            const isValidGif = el.querySelectorAll('[aria-label="Play this GIF"]').length > 0;

                            if (isValidVideo) {
                                el.style.display = "none";
                                el.setAttribute("textonly", "true");
                                return;
                            }
                            if (isValidImg) {
                                el.style.display = "none";
                                el.setAttribute("textonly", "true");
                                return;
                            }
                            if (isValidGif) {
                                el.style.display = "none";
                                el.setAttribute("textonly", "true");
                                return;
                            }
                        })
                    }
                }

                if (!isStyleInjected) {
                    // Append the <style> element to the <head> of the document
                    injectCss();
                }
                
            }
            
        }
    };
})();