Customizable Textarea Prefix/Suffix for DGG

Adds a customizable prefix and suffix to text in the DGG chat textarea

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Customizable Textarea Prefix/Suffix for DGG
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Adds a customizable prefix and suffix to text in the DGG chat textarea
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let prefix = "WWWWaiting . o O ( ";
    let suffix = " )";

    const observer = new MutationObserver((mutations) => {
        const textarea = document.getElementById('chat-input-control');
        if (textarea) {
            console.log("Textarea found:", textarea);

            textarea.addEventListener('keydown', function(event) {
                if (event.key === 'Enter') {
                    let finalText = textarea.value;

                    if (finalText.trim().startsWith("/command")) {
                        event.preventDefault(); // Prevent the default Enter behavior
                        textarea.value = ''; // Clear the textarea

                        const newPrefix = prompt("Enter new prefix:", prefix);
                        if (newPrefix !== null) {
                            prefix = newPrefix;
                            const newSuffix = prompt("Enter new suffix:", suffix);
                            if (newSuffix !== null) {
                                suffix = newSuffix;
                                alert(`Prefix set to "${prefix}", Suffix set to "${suffix}"`);
                            }
                        }
                    } else {
                        if (finalText.trim() !== "") {
                            if (finalText.startsWith("/")) {
                                // Do nothing, just send the text
                            } else if (finalText.startsWith(">")) {
                                finalText = "> " + prefix + finalText.substring(1) + suffix;
                            } else {
                                finalText = prefix + finalText + suffix;
                            }
                            textarea.value = finalText;
                        }
                    }
                }
            });

            observer.disconnect();
        }
    });

    const chatInputFrame = document.getElementById('chat-input-frame');

    if (chatInputFrame) {
        observer.observe(chatInputFrame, { childList: true, subtree: true });
    } else {
        console.log("chat-input-frame not found. Ensure the ID is correct.");
    }
})();