Uhmegle AutoSkip with Persistent Country List and Disconnect Detection (Randomized Intervals)

Auto-skip chats based on country (persisted) and if "You have disconnected" appears, simulate an ESC key press after a randomized delay.

目前為 2025-04-04 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Uhmegle AutoSkip with Persistent Country List and Disconnect Detection (Randomized Intervals)
// @namespace    http://tampermonkey.net/
// @version      1.3.1
// @description  Auto-skip chats based on country (persisted) and if "You have disconnected" appears, simulate an ESC key press after a randomized delay.
// @match        *://uhmegle.com/*
// @grant        none
// @license GNU GPLv3
// ==/UserScript==

(function() {
    'use strict';

    // -------------------------------
    // Persistent Country List Settings
    // -------------------------------

    const storageKey = 'uhmegleAutoSkipCountries';

    // Retrieve stored auto-skip country settings or initialize as empty.
    let autoSkipCountries = JSON.parse(localStorage.getItem(storageKey)) || {};

    // Save changes to localStorage.
    function saveAutoSkipCountries() {
        localStorage.setItem(storageKey, JSON.stringify(autoSkipCountries));
    }

    // Create and style a panel for the country list.
    const panel = document.createElement('div');
    panel.style.position = 'fixed';
    panel.style.top = '10px';
    panel.style.right = '10px';
    panel.style.backgroundColor = 'white';
    panel.style.border = '1px solid #ccc';
    panel.style.padding = '10px';
    panel.style.zIndex = 10000;
    panel.style.maxHeight = '300px';
    panel.style.overflowY = 'auto';
    panel.innerHTML = '<h4>AutoSkip Countries</h4><div id="countryList"></div>';
    document.body.appendChild(panel);

    // Update the panel with the list of countries and their auto-skip checkbox.
    function updateCountryListUI() {
        const listDiv = document.getElementById('countryList');
        listDiv.innerHTML = '';
        for (const country in autoSkipCountries) {
            const label = document.createElement('label');
            label.style.display = 'block';
            label.style.marginBottom = '5px';
            const checkbox = document.createElement('input');
            checkbox.type = 'checkbox';
            checkbox.checked = autoSkipCountries[country];
            checkbox.addEventListener('change', function() {
                autoSkipCountries[country] = this.checked;
                saveAutoSkipCountries();
            });
            label.appendChild(checkbox);
            label.appendChild(document.createTextNode(' ' + country));
            listDiv.appendChild(label);
        }
    }

    updateCountryListUI();

    // -------------------------------
    // ESC Key Simulation Functions
    // -------------------------------

    // Function to simulate one ESC key press.
    function simulateEsc() {
        const escEvent = new KeyboardEvent('keydown', {
            key: 'Escape',
            code: 'Escape',
            keyCode: 27,
            which: 27,
            bubbles: true,
            cancelable: true
        });
        document.dispatchEvent(escEvent);
    }

    // Function to simulate two ESC key presses.
    function simulateSkip() {
        console.log('Simulating ESC key presses for auto-skip...');
        simulateEsc();
        // Random delay between 400ms and 600ms.
        const randomDelay = 400 + Math.random() * 200;
        setTimeout(simulateEsc, randomDelay);
    }

    // -------------------------------
    // Monitor for Country Changes
    // -------------------------------

    let lastCountry = '';

    function checkCountry() {
        const countryElem = document.getElementById('countryName');
        if (countryElem) {
            const currentCountry = countryElem.textContent.trim();
            if (currentCountry && currentCountry !== lastCountry) {
                lastCountry = currentCountry;
                // Add new country if not already present.
                if (!(currentCountry in autoSkipCountries)) {
                    autoSkipCountries[currentCountry] = false; // default auto-skip off
                    saveAutoSkipCountries();
                    updateCountryListUI();
                }
                // If auto-skip is enabled for this country, simulate the skip.
                if (autoSkipCountries[currentCountry]) {
                    simulateSkip();
                }
            }
        }
    }

    // Use a recursive function to randomize interval between country checks.
    function scheduleCountryCheck() {
        checkCountry();
        const nextDelay = 800 + Math.random() * 400; // 800ms to 1200ms
        setTimeout(scheduleCountryCheck, nextDelay);
    }
    scheduleCountryCheck();

    // Also observe DOM changes in case the country element is loaded dynamically.
    const countryObserver = new MutationObserver(checkCountry);
    countryObserver.observe(document.body, { childList: true, subtree: true });

    // -------------------------------
    // Monitor for "You have disconnected" Text
    // -------------------------------

    let disconnectTriggered = false;
    function checkForDisconnect() {
        // Check if the phrase "You have disconnected" appears anywhere in the visible text.
        const pageText = document.body.innerText || "";
        const found = pageText.includes("You have disconnected");
        if (found && !disconnectTriggered) {
            disconnectTriggered = true;
            console.log('"You have disconnected" detected. Simulating ESC key press after a random delay...');
            // Random delay between 400ms and 600ms before simulating an ESC key press.
            const randomDelay = 400 + Math.random() * 200;
            setTimeout(simulateEsc, randomDelay);
        } else if (!found && disconnectTriggered) {
            disconnectTriggered = false;
        }
    }

    // Recursive check for disconnect with random interval.
    function scheduleDisconnectCheck() {
        checkForDisconnect();
        const nextDelay = 800 + Math.random() * 400;
        setTimeout(scheduleDisconnectCheck, nextDelay);
    }
    scheduleDisconnectCheck();

    // -------------------------------
    // End of Script
    // -------------------------------
})();