Shoplifting alert

Display a red square if cameras or guards are down in Big Al's. 1 Api call 2 seconds after you open any page, and every 60 seconds. Add your public Api key on line 62.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Shoplifting alert
// @namespace    http://torn.city.com.dot.com.com
// @version      1.1
// @description  Display a red square if cameras or guards are down in Big Al's. 1 Api call 2 seconds after you open any page, and every 60 seconds. Add your public Api key on line 62.
// @author       Adobi
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to create and display the red square with centered text and a line break
    function displayRedSquare(camera, guard) {
        if (camera === true || guard === true) {
            const redSquare = document.createElement('div');
            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);

            document.body.appendChild(redSquare);
        }
    }

    // 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=key here')
                .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 60 seconds
    setInterval(fetchAndDisplayData, 4000);
})();