Emerald Auto Next

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Emerald Auto Next
// @namespace       https://greasyfork.org/
// @version         1.2
// @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;

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

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

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

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

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

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

window.onload = () => {
    observeChanges();
};