Busting reminder

Guess how many busts you can do without getting jailed

目前為 2023-09-18 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Busting reminder
// @namespace    http://torn.city.com.dot.com.com
// @version      0.2.1
// @description  Guess how many busts you can do without getting jailed
// @author       Adobi
// @match        https://www.torn.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function findLongestSequence(numbersArray) {
        const period = 24 * 60 * 60 * 3
        let longestSequence = 0;
        let currentSequence = 1;
        let currentMin = numbersArray[0];
        let currentMax = numbersArray[0];

        for (let i = 1; i < numbersArray.length; i++) {
            const num = numbersArray[i];

            if (Math.abs(num - currentMin) <= period && Math.abs(num - currentMax) <= period) {
                currentSequence++;
                currentMin = Math.min(currentMin, num);
                currentMax = Math.max(currentMax, num);
            } else {
                longestSequence = Math.max(longestSequence, currentSequence);
                currentSequence = 1;
                currentMin = num;
                currentMax = num;
            }
        }

        return Math.max(longestSequence, currentSequence);
    }


    // Function to create and display yellow square with the option to enter API key
    function displayYellowSquare() {
        const yellowSquare = document.createElement('div');
        yellowSquare.id = 'yellowSquare';
        yellowSquare.style.position = 'fixed';
        yellowSquare.style.left = '0';
        yellowSquare.style.top = '0';
        yellowSquare.style.color = 'black';
        yellowSquare.style.top = '100px';
        yellowSquare.style.width = '100px';
        yellowSquare.style.height = '100px';
        yellowSquare.style.backgroundColor = 'yellow';
        yellowSquare.style.zIndex = '99999999';
        yellowSquare.style.display = 'flex'; // Use flexbox for centering
        yellowSquare.style.cursor = 'pointer'; // Change cursor to pointer

        // Create a container div for centered text
        const textContainer = document.createElement('div');
        textContainer.style.margin = 'auto'; // Center horizontally
        textContainer.style.textAlign = 'center'; // Center text
        textContainer.style.display = 'flex';
        textContainer.style.flexDirection = 'column'; // Center vertically

        // Create text nodes for the yellow square with a line break
        const yellowTextNode0 = document.createTextNode('Busting reminder');
        const lineBreak = document.createElement('br');
        const yellowTextNode1 = document.createTextNode('Click here to supply full access API key');


        // Append the text nodes and line break to the text container
        textContainer.appendChild(yellowTextNode0);
        textContainer.appendChild(lineBreak);
        textContainer.appendChild(yellowTextNode1);

        // Append the text container to the yellow square
        yellowSquare.appendChild(textContainer);

        // Append the yellow square to the body (if it's not already added)
        if (!document.getElementById('yellowSquare')) {
            document.body.appendChild(yellowSquare);
        }

        // Add a click event listener to the yellow square
        yellowSquare.addEventListener('click', () => {
            const apiKey = prompt('Enter your full access API key:');
            if (apiKey) {
                // Store the API key with GM_setValue
                GM_setValue('busting_api_key', apiKey);
                yellowSquare.remove(); // Remove the yellow square after entering the API key
                fetchAndDisplayData(); // Run an API call with the new key
            }
        });
    }

    // Check if the API key is already stored using GM_getValue
    const apiKey = GM_getValue('busting_api_key', '');

    // If the API key is not stored, display the yellow square
    if (!apiKey) {
        displayYellowSquare();
    }



    // Function to fetch API data
    function fetchAndDisplayData() {
        const currentTime = Date.now()/1000;
        const apiKey = GM_getValue('busting_api_key', '');
        // If the API key is not stored, display the yellow square
        if (!apiKey) {
            displayYellowSquare();
        }
        const url = `https://api.torn.com/user/?selections=log&log=5360&key=${apiKey}`
        console.log(url)
        const timestamps = [];
        let mostBustsIn72h = 0;
        let last24h = 0;
        let last72h = 0;
        fetch(url)
            .then(response => response.json())
            .then(data => {
            if (data.error) {
                if (data.error.error === "Incorrect key" || data.error.error === "Access level of this key is not high enough") {
                    GM_setValue('shoplifting_api_key', "");
                    displayYellowSquare();
                } else { return; }
            } else {
                // Count busts in log and stuff
                for (const entry in data['log']) {
                    timestamps.push(data['log'][entry]['timestamp']);
                    if (data['log'][entry]['timestamp'] > currentTime - 24 * 60 * 60 * 3) {
                        last72h++;
                        if (data['log'][entry]['timestamp'] > currentTime - 24 * 60 * 60) {
                            last24h++;
                        }
                    }
                }
                mostBustsIn72h = findLongestSequence(timestamps);
                const expectedMaxDailyBusts = mostBustsIn72h / 3;
                const currentAvailableBusts = mostBustsIn72h - last72h;
                console.log(mostBustsIn72h)
                console.log(last72h)


                let jailLink = document.querySelector('div#nav-jail');
                let jailLinkText = jailLink.querySelector('.linkName___FoKha');

                ////////// Check if there's an existing text element and remove it
                //let myCounter = document.querySelector('.my-unique-bust-counter')
                let existingTextElement = jailLinkText.parentNode.querySelector('.bust-guesser');
                if (existingTextElement) {
                    existingTextElement.remove();
                }
                //////////
                const newSpan = document.createElement('p');
                newSpan.className = 'linkName___FoKha bust-guesser';
                newSpan.style.color = 'red';
                if (currentAvailableBusts <= 0) { newSpan.style.color = 'green'; }
                newSpan.style.float = 'right';
                newSpan.style.paddingRight = '5px';
                newSpan.textContent = currentAvailableBusts.toFixed(0);
                jailLinkText.parentNode.appendChild(newSpan);
            }
        });
    }

    fetchAndDisplayData()
    setInterval(fetchAndDisplayData, 10 * 60 * 1000);
    //displayYellowSquare() //debugging
    //displayRedSquare(true, true) //debugging
})();