Save The Chain!

Monitor chain timer and provide quick access to targets in Torn City. Alerts trigger only for chains of 10 hits or more.

As of 22.08.2024. See ბოლო ვერსია.

// ==UserScript==
// @name         Save The Chain!
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Monitor chain timer and provide quick access to targets in Torn City. Alerts trigger only for chains of 10 hits or more.
// @author       MummaPhucka
// @match        https://www.torn.com/*/*
// @icon         none
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Torn API Key
    const API_KEY = 'YOUR_API_KEY_HERE';

    // Target IDs
    const TARGET_IDS = ['200728', '67254', '112024', '583658'];

    // Torn API Endpoints
    const CHAIN_ENDPOINT = `https://api.torn.com/faction/?selections=chain&key=${API_KEY}`;

    // Notification threshold in seconds (e.g., 300 seconds = 5 minutes)
    const ALERT_THRESHOLD = 180;

    // Minimum chain hits required to trigger alerts
    const MIN_CHAIN_HITS = 1;

    // Send Notification
    function sendNotification(message) {
        const notification = new Notification("Torn Chain Alert", {
            body: message,
            icon: "https://www.torn.com/favicon.ico" // Optional: Torn City favicon or any other icon URL
        });

        notification.onclick = function() {
            window.focus();
        };
    }

    // Request notification permission if not already granted
    if (Notification.permission !== "granted") {
        Notification.requestPermission();
    }

    // Get Chain Data
    async function getChainData() {
        try {
            const response = await fetch(CHAIN_ENDPOINT);
            const data = await response.json();
            const chainHits = data.chain.current || 0;
            const chainTimer = data.chain.timeout || 0;
            return { chainHits, chainTimer };
        } catch (error) {
            console.error('Error fetching chain data:', error);
            return { chainHits: 0, chainTimer: 0 };
        }
    }

    // Generate Attack Links for Multiple Targets
    function generateAttackLinks() {
        return TARGET_IDS.map(id => `Attack Target: https://www.torn.com/loader.php?sid=attack&user2=${id}`).join('\n');
    }

    // Monitor Chain Timer
    async function monitorChain() {
        while (true) {
            const { chainHits, chainTimer } = await getChainData();

            if (chainHits >= MIN_CHAIN_HITS && chainTimer <= ALERT_THRESHOLD) {
                const attackLinks = generateAttackLinks();
                const message = `Chain of ${chainHits} hits is at ${chainTimer} seconds!\n\n${attackLinks}`;
                sendNotification(message);
            }

            await new Promise(resolve => setTimeout(resolve, 60000)); // Wait for 1 minute before checking again
        }
    }

    // Start monitoring when the script runs
    monitorChain();

})();