Reddit | Always expand comment box

Always expand comment box on reddit

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Advertisement:

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

Advertisement:

// ==UserScript==
// @name            Reddit | Always expand comment box
// @namespace       https://greasyfork.org/users/821661
// @match           https://www.reddit.com/*
// @match           https://sh.reddit.com/*
// @grant           none
// @noframes
// @version         1.0.0
// @author          hdyzen
// @description     Always expand comment box on reddit
// @license         GPL-3.0
// ==/UserScript==

const focusedElements = new WeakSet();
const init = () => {
    focusCommentBox();
    new MutationObserver(focusCommentBox).observe(document.body, {
        childList: true,
    });
};

window.addEventListener("load", init, { once: true });

function focusCommentBox() {
    const commentTextarea = document.querySelector("comment-body-header [noun='add_comment_button'] faceplate-textarea-input");
    if (!commentTextarea || focusedElements.has(commentTextarea)) return;
    focusedElements.add(commentTextarea);

    commentTextarea.dispatchEvent(
        new FocusEvent("focus", {
            bubbles: true,
            cancelable: true,
        }),
    );
    setTimeout(() => getDeepActiveElement()?.blur());
}

function getDeepActiveElement() {
    let activeElement = document.activeElement;
    while (activeElement?.shadowRoot) {
        activeElement = activeElement.shadowRoot.activeElement;
    }
    return activeElement;
}