Shoplifting alert

Display a red square if cameras or guards are down in Big Al's for the purpose of gaining special ammo.

当前为 2023-09-08 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Shoplifting alert
// @namespace    http://torn.city.com.dot.com.com
// @version      1.2
// @description  Display a red square if cameras or guards are down in Big Al's for the purpose of gaining special ammo.
// @author       Adobi
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @license      MIT
// ==/UserScript==
//
// USAGE
// Add public API key on LINE 24 in this script.
// Click red square to get rid of it.
// Define how long you want it gone on LINE 25 in this script.
//

(function() {
    'use strict';

    // USER DEFINED VARIABLES

    const apiKey = 'API key here inside the single quotes'; // Your public access API key.
    let timeOut = 1800 // How many seconds the script should sleep if you click the red square.

    // END OF USER DEFINED VARIABLES. Don't change shit below this line.


    timeOut = timeOut * 1000

    // Function to create and display the red square with text
    function displayRedSquare(camera, guard) {
        // Check if both camera and guard values are false
        if (camera === false && guard === false) {
            // If both are false, remove the red square div (if it exists)
            const existingRedSquare = document.getElementById('redSquare');
            if (existingRedSquare) {
                existingRedSquare.remove();
            }
            return; // Exit the function, as there's no need to create the red square
        }

        // Create or update the red square div
        const redSquare = document.getElementById('redSquare') || document.createElement('div');
        redSquare.id = 'redSquare';
        redSquare.style.position = 'fixed';
        redSquare.style.left = '0';
        redSquare.style.top = '0';
        redSquare.style.width = '100px';
        redSquare.style.height = '100px';
        redSquare.style.backgroundColor = 'red';
        redSquare.style.zIndex = '9999';
        redSquare.style.display = 'flex'; // Use flexbox for centering

        // 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 "camera" and "guard" values
        const cameraTextNode = document.createTextNode(`Camera: ${camera}`);
        const lineBreak = document.createElement('br');
        const guardTextNode = document.createTextNode(`Guard: ${guard}`);

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

        // Append the text container to the red square
        redSquare.appendChild(textContainer);

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

        // Add a click event listener to the red square
        redSquare.addEventListener('click', () => {
            // Set camera and guard values to false
            GM_setValue('cameraValue', false);
            GM_setValue('guardValue', false);

            // Set the timestamp to the current timestamp plus some time defined by user on line 19ish
            GM_setValue('lastCallTime', Date.now() + timeOut);

            // Remove the red square div
            redSquare.remove();
        });
    }

    // Function to fetch API data and display the red square
    function fetchAndDisplayData() {
        const currentTime = Date.now();
        const lastCallTime = GM_getValue('lastCallTime', 0);

        // Check if at least 60 seconds have passed since the last API call
        if (currentTime - lastCallTime >= 60000) {
            GM_setValue('lastCallTime', currentTime); // Store the current timestamp

            fetch(`https://api.torn.com/torn/?selections=shoplifting&key=${apiKey}`)
                .then(response => response.json())
                .then(data => {
                    // Get the values for "camera" and "guard" from the API data
                    const camera = data.shoplifting.big_als[0].disabled;
                    const guard = data.shoplifting.big_als[1].disabled;

                    // Store the values of "camera" and "guard"
                    GM_setValue('cameraValue', camera);
                    GM_setValue('guardValue', guard);

                    // Call the displayRedSquare function with camera and guard values
                    displayRedSquare(camera, guard);
                })
                .catch(error => console.error('API Error:', error));
        }
    }

    // Initial run after 2 seconds with saved values of camera and guard, or fetch new data if values don't exist
    setTimeout(() => {
        const currentTime = Date.now();
        const lastCallTime = GM_getValue('lastCallTime', 0);
        // Check if the last saved timestamp is more than 60 seconds ago or if cameraValue and guardValue don't exist
        if (
            currentTime - lastCallTime >= 61000 ||
            GM_getValue('cameraValue') === undefined ||
            GM_getValue('guardValue') === undefined
        ) {
            // If any of the conditions are met, fetch new data
            fetchAndDisplayData();
        } else {
            // Otherwise, use the old saved values for camera and guard
            const savedCamera = GM_getValue('cameraValue', false); // Default to false if not saved
            const savedGuard = GM_getValue('guardValue', false); // Default to false if not saved
            displayRedSquare(savedCamera, savedGuard);
        }
    }, 2000);

    // Repeat every 5 seconds
    setInterval(fetchAndDisplayData, 5000);
})();