Stumblechat Stalker

Intercept WebSocket messages on StumbleChat and display them in tabs, sorted by sender's handle, with minimize and resize functionality

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Stumblechat Stalker
// @namespace    http://tampermonkey.net/
// @version      1.06
// @description  Intercept WebSocket messages on StumbleChat and display them in tabs, sorted by sender's handle, with minimize and resize functionality
// @author       MeKLiN
// @match        https://stumblechat.com/room/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=stumblechat.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let overlayVisible = true; // Flag to track overlay visibility
    let userMap = {}; // Maps handles to user info (username, nick)
    let selfHandle = null; // Store the handle of the current user

    // Function to update userMap based on "joined", "join", or "quit" events
    function updateUserMap(message) {
        if (message.stumble === "join" || message.stumble === "joined") {
            // Handle the user joining
            const { handle, username, nick, userlist } = message;

            // Check if the userlist exists and update the map with all users
            if (userlist) {
                userlist.forEach(user => {
                    if (user.handle && user.username) {
                        userMap[user.handle] = { username: user.username, nick: user.nick };
                    }
                });
            }

            // Update user map with the individual user joining
            if (handle && username) {
                userMap[handle] = { username, nick };
            }

            // Check if the current message is for the current user
            if (handle && !selfHandle) {
                selfHandle = handle; // Store the user's handle if it's the first "joined"
            }
        } else if (message.stumble === "quit") {
            // Handle the user quitting
            const { handle } = message;
            if (handle) {
                delete userMap[handle];
            }
        }
    }

    // Function to display the user list in the joinTab
    function displayUserList() {
        const joinTab = document.getElementById('joinTab');
        joinTab.innerHTML = ''; // Clear previous content

        Object.keys(userMap).forEach(handle => {
            const user = userMap[handle];
            const userButton = document.createElement('button');
            userButton.innerText = `${user.username} (${handle} / ${user.nick})`;
            joinTab.appendChild(userButton);
        });
    }

    const originalWebSocket = window.WebSocket;
    window.WebSocket = function(url, protocols) {
        const ws = new originalWebSocket(url, protocols);
        ws.addEventListener('message', event => {
            try {
                const message = JSON.parse(event.data);

                // Handle the "join", "joined", and "quit" events
                if (["join", "joined", "quit"].includes(message.stumble)) {
                    updateUserMap(message);
                    displayUserList();
                }
            } catch (error) {
                console.error("Error processing WebSocket message:", error);
            }
        });
        return ws;
    };

        // Create the overlay container
        const overlayDiv = document.createElement("div");
        overlayDiv.id = "overlayContainer";
        Object.assign(overlayDiv.style, {
            position: "fixed",
            top: "0",
            left: "0",
            zIndex: "9999",
            backgroundColor: "#222",
            color: "#fff",
            padding: "10px",
            borderRadius: "8px",
            maxWidth: "400px",
            maxHeight: "80vh",
            overflowY: "auto",
            display: "flex",
            flexDirection: "column",
            resize: "both",
            visibility: "visible"
        });

        // Create the toggle button (separate from the overlay)
        const toggleButton = document.createElement("button");
        toggleButton.innerHTML = "+";
        Object.assign(toggleButton.style, {
            position: "fixed",
            top: "0px",
            left: "0px",
            zIndex: "10001", // Ensure it's on top of everything
            marginBottom: "10px",
            padding: "8px",
            cursor: "pointer",
        });
        toggleButton.onclick = () => {
            // Toggle the visibility of the overlay
            overlayDiv.style.display = overlayVisible ? "none" : "flex";
            overlayVisible = !overlayVisible;
            toggleButton.innerHTML = overlayVisible ? "-" : "+";
        };
        document.body.appendChild(toggleButton); // Add button to the body

        // Add the overlay content (tabs)
        const tabNames = ["join"];
        tabNames.forEach(tab => {
            const tabButton = document.createElement("button");
            tabButton.innerHTML = tab.charAt(0).toUpperCase() + tab.slice(1);
            Object.assign(tabButton.style, { backgroundColor: "#444", border: "none", padding: "10px", margin: "2px", cursor: "pointer" });
            overlayDiv.appendChild(tabButton);
        });

        // Add the tab content
        const tabContentDivs = ["join"];
        tabContentDivs.forEach(tab => {
            const tabDiv = document.createElement("div");
            tabDiv.id = `${tab}Tab`;
            Object.assign(tabDiv.style, { display: "none", padding: "10px", border: "1px solid #555", borderRadius: "5px", marginTop: "5px", maxHeight: "60vh", overflowY: "auto" });
            overlayDiv.appendChild(tabDiv);
        });

        document.body.appendChild(overlayDiv);

        // Default to showing the "join" tab
        function showTab(tab) {
            tabContentDivs.forEach(tabName => {
                document.getElementById(`${tabName}Tab`).style.display = tabName === tab ? "block" : "none";
            });
        }

        showTab("join");
    })();