Manual Message Numbering

Numbers each message in chat and allows clearing the counter

当前为 2024-10-01 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Manual Message Numbering
// @namespace    -
// @version      1.4
// @description  Numbers each message in chat and allows clearing the counter
// @author       Clawberry
// @match        https://character.ai/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let isCleared = false; // Tracks whether numbers are cleared or not

    // Function to number messages
    const numberMessage = (message, index) => {
        let numberSpan = message.querySelector('.message-number');
        if (!numberSpan) {
            numberSpan = document.createElement('span');
            numberSpan.classList.add('message-number');
            numberSpan.style.cssText = 'font-weight: bold; margin-right: 5px; color: #595959;';
            message.prepend(numberSpan);
        }
        numberSpan.textContent = `${index + 1}. `;
    };

    // Function to number all messages
    const numberAllMessages = () => {
        const messages = Array.from(document.querySelectorAll('.m-0:not(.swiper .m-0)'));
        messages.forEach(numberMessage);
    };

    // Function to clear message numbers
    const clearAllMessages = () => {
        const messages = Array.from(document.querySelectorAll('.message-number'));
        messages.forEach(span => {
            span.textContent = '';
        });
    };

    // Create the button and add event listeners
    const createButton = () => {
        const button = document.createElement('button');
        button.textContent = 'Update Message Counter';
        button.style.cssText = `
            position: fixed;
            bottom: 5px;
            right: 10px;
            z-index: 9999;
            padding: 5px 5px;
            background-color: #000;
            color: #FFF;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 0.7em;
        `;

        // Toggle between updating and clearing message numbers on button click
        button.addEventListener('click', () => {
            if (isCleared) {
                numberAllMessages(); // Add numbers back
                button.textContent = 'Clear Message Counter'; // Update button text
            } else {
                clearAllMessages(); // Clear numbers
                button.textContent = 'Update Message Counter'; // Update button text
            }
            isCleared = !isCleared; // Toggle state
        });

        document.body.appendChild(button);
    };

    // Observe the chat for new messages
    const observeChat = () => {
        const observer = new MutationObserver(numberAllMessages);
        const chatContainer = document.querySelector('.chat-container');
        if (chatContainer) {
            observer.observe(chatContainer, { childList: true, subtree: true });
        }
    };

    createButton();
    setTimeout(() => {
        numberAllMessages();
        observeChat();
    }, 1000);
})();