Emerald Auto Next

Auto-start new chat on disconnect and keep chat input focused.

目前為 2024-12-11 提交的版本,檢視 最新版本

// ==UserScript==
// @name            Emerald Auto Next
// @namespace       https://greasyfork.org/
// @version         1.3
// @description     Auto-start new chat on disconnect and keep chat input focused.

// @author          Zach
// @license         Apache-2.0
// @icon            https://emeraldchat.com/logo7.svg

// @match           https://emeraldchat.com/app

// @grant           none

// @compatible      Firefox
// @compatible      Violentmonkey

// ==/UserScript==

'use strict';

function randomTimeout(min, max) {
    const minMs = min * 1000;
    const maxMs = max * 1000;
    return Math.floor(Math.random() * (maxMs - minMs + 1)) + minMs;
}

function simulateClick(element) {
    const mouseDown = new MouseEvent('mousedown', { bubbles: true, cancelable: true });
    const mouseUp = new MouseEvent('mouseup', { bubbles: true, cancelable: true });
    const clickEvent = new MouseEvent('click', { bubbles: true, cancelable: true });

    element.dispatchEvent(mouseDown);
    element.dispatchEvent(mouseUp);
    element.dispatchEvent(clickEvent);
}

function giveKarma() {
    const [good, bad] = document.querySelectorAll("a.ui-button-match-mega");
    if (!good || !bad) return;

    const messages = document.querySelectorAll("#messages > .room-component-message-container").length;
    if (messages > 3) {
        simulateClick(good);
    }
    else {
        simulateClick(bad);
    }
}

function nextChat() {
    const startButton = document.querySelector(".ui-button-match");
    if (!startButton) return;

    setTimeout(() => {
        if (startButton.textContent === "Start") {
            simulateClick(startButton);
        }
    }, randomTimeout());
}

function focusChat() {
    const chatInput = document.getElementById("room-input");
    if (!chatInput) return;

    document.body.addEventListener("keydown", (event) => {
        if (!document.querySelector("#ui-hatch > *, #ui-hatch-2 > *, #interests") && event.key !== "`") {
            chatInput.focus();
        }
    }, { once: true });
}

function observeChanges() {
    const container = document.getElementById("container");
    if (!container) return;

    let shouldExecute = true;

    const observer = new MutationObserver(() => {
        giveKarma();
        if (shouldExecute) nextChat();
        focusChat();
    });

    observer.observe(container, { childList: true, subtree: true });

    document.addEventListener("keydown", (event) => {
        if (event.key === "`") {
            event.preventDefault();
            shouldExecute = !shouldExecute;
        }
    });
}


observeChanges();