Torn City Chain Watch Alert

Alert when chain timer drops below user-defined threshold and flash the screen red continuously.

// ==UserScript==
// @name         Torn City Chain Watch Alert
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Alert when chain timer drops below user-defined threshold and flash the screen red continuously.
// @author       Fu11y
// @match        https://www.torn.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let previousStateBelowThreshold = false;
    let alertedForCurrentThreshold = false;
    let alertThresholdInSeconds = parseInt(localStorage.getItem('alertThreshold')) || 240; // Default to 4 minutes

    function createDropdown() {
        const dropdown = document.createElement('select');
        dropdown.id = 'chainTimerDropdown';
        [120, 150, 180, 210, 240, 270].forEach(seconds => {
            const option = document.createElement('option');
            option.value = seconds;
            option.textContent = `${seconds / 60} minutes`;
            dropdown.appendChild(option);
        });
        dropdown.value = alertThresholdInSeconds;
        dropdown.addEventListener('change', (e) => {
            alertThresholdInSeconds = parseInt(e.target.value);
            localStorage.setItem('alertThreshold', alertThresholdInSeconds);
            alertedForCurrentThreshold = false; // Reset the alert state when threshold changes
        });

        // Styles to position the dropdown in the top right corner
        dropdown.style.position = 'fixed';
        dropdown.style.top = '10px';
        dropdown.style.right = '10px';
        dropdown.style.zIndex = '10000'; // Ensure it's above other page content

        document.body.appendChild(dropdown);
    }

    function checkChainTimer() {
        const timerElement = document.querySelector('.bar-timeleft___B9RGV');

        if (timerElement) {
            const timerText = timerElement.textContent.trim();
            const timeParts = timerText.split(':');
            const minutes = parseInt(timeParts[0], 10);
            const seconds = parseInt(timeParts[1], 10);
            const totalTimeInSeconds = (minutes * 60) + seconds;

            if (totalTimeInSeconds < alertThresholdInSeconds) {
                if (!alertedForCurrentThreshold) {
                    alert(`Chain timer is below ${alertThresholdInSeconds / 60} minutes!`);
                    alertedForCurrentThreshold = true;
                }
                flashScreenRed();
                previousStateBelowThreshold = true;
            } else if (totalTimeInSeconds >= alertThresholdInSeconds) {
                previousStateBelowThreshold = false;
                alertedForCurrentThreshold = false; // Reset the alert state when timer goes back above threshold
            }
        }
    }

    function flashScreenRed() {
        const flashDiv = document.createElement('div');
        flashDiv.style.position = 'fixed';
        flashDiv.style.top = '0';
        flashDiv.style.left = '0';
        flashDiv.style.width = '100vw';
        flashDiv.style.height = '100vh';
        flashDiv.style.backgroundColor = 'red';
        flashDiv.style.opacity = '0.5';
        flashDiv.style.zIndex = '9999';

        document.body.appendChild(flashDiv);

        setTimeout(() => {
            flashDiv.remove();
        }, 1000); // Flash for 1 second
    }

    createDropdown();
    setInterval(checkChainTimer, 2000);
})();