Replika Chatbox History

Remembers and scroll through the last 10 entered in the chatbox using the up and down arrow keys

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Replika Chatbox History
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  Remembers and scroll through the last 10 entered in the chatbox using the up and down arrow keys
// @author       Nixsy
// @match        *://*/*
// @grant        none
// @license GPL 3
// ==/UserScript==

(function () {
    'use strict';

    const test = false;
    if (test) {
        console.log("Script started");
    }

    const maxEntries = 10;
    let chatHistory = [];
    let currentEntryIndex = 0; 

    function handleKeyPress(event) {
        if (event.key == 'ArrowUp' || event.key == 'ArrowDown' || event.key == 'Enter') {
            console.log('key event :' + event.key);

            var chatbox = document.querySelector('#send-message-textarea');

            if (event.key == 'ArrowUp') {
                // Scroll up through history
                if (currentEntryIndex > 0) {
                    currentEntryIndex--;
                    chatbox.value = chatHistory[currentEntryIndex];
					chatbox.click()
					var e = new KeyboardEvent('keydown',{'keyCode':32,'which':32});
                    if (test) {
                        console.log('arrow up and current entry: ' + chatHistory[currentEntryIndex]);
                    }
                }
            } else if (event.key == 'ArrowDown') {
                // Scroll down through history
                if (currentEntryIndex < chatHistory.length - 1) {
                    currentEntryIndex++;
                    chatbox.value = chatHistory[currentEntryIndex];
					chatbox.click()
					var e = new KeyboardEvent('keydown',{'keyCode':32,'which':32});
                    if (test) {
                        console.log('arrow down and current entry: ' + chatHistory[currentEntryIndex]);
                    }
                } else {
                    // If at the latest message, clear the chatbox
                    currentEntryIndex = chatHistory.length;
                    chatbox.value = '';
                }
            } else if (event.key == 'Enter') {
                if (chatbox.value) {
                    console.log("chatbox isnt null")
                } else {
                    var chatbox = document.querySelector('#send-message-textarea');
					console.log('tried to refil chatbox content with: ' + chatbox.value)
                }
                if (test) {
                    console.log('enter pressed and this added to object :' + chatbox.value);
                }
                // Remember the current entry when Enter is pressed

                const currentEntry = chatbox.value;
                if (currentEntry !== '') {
                    if (chatHistory.length == maxEntries) {
                        chatHistory.shift(); // Remove the oldest entry if the history is full
                    }
                    chatHistory.push(currentEntry);
                    currentEntryIndex = chatHistory.length;
                    if (test) {
                        console.log('current entry : ' + currentEntry + ' current length:' + chatHistory.length);
                    }
                }
            }
        }
    }

    setTimeout(function () {
        const chatbox2 = document.querySelector('#send-message-textarea');
        if (chatbox2) {
            chatbox2.addEventListener('keydown', handleKeyPress);
            if (test) {
                console.log('Chatbox key event added');
            }

        } else {
            if (test) {
                console.log('Chatbox not found');
            }
        }
    }, 1000);
})();