Flying OC Alert

Alerts you when you want to fly but an OC is about to start.

目前為 2025-04-25 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Flying OC Alert
// @namespace    http://tampermonkey.net/
// @version      0.2.2
// @description  Alerts you when you want to fly but an OC is about to start.
// @author       NichtGersti [3380912]
// @license      MIT
// @match        https://www.torn.com/page.php?sid=travel
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// ==/UserScript==

//TODO: Options

(function() {
    'use strict';

    let apiKey = "" //Minimal access or above!
    let maxWarningHours = 10
    let confirmMathQuestion = false //highest priority; makes you solve a simple addition
    let confirmPrompt = false //only if confirmMathQuestion = false; makes you write the confirmMessage
    let confirmMessage = "continue" //for confirmPrompt

    let ocUrl = "https://api.torn.com/v2/user/?selections=organizedcrime&key={apiKey}".replace("{apiKey}", apiKey)

    fetch(ocUrl).then( response => {
        if (response.ok) {
            return response.json();
        }
        throw new Error('Something went wrong');
    })
    .then( result => {

        if (result.error) {
            alert(`Error: ${result.error.code}\n${result.error.error}`)
            return
        }

        let infoOc = result.organizedCrime

        let totalSeconds = infoOc.ready_at - Math.floor(Date.now() / 1000)
        //if (totalSeconds / 3600 > 10) return

        let days = Math.floor(totalSeconds / 86400) % 24
        let hours = Math.floor(totalSeconds / 3600) % 60
        let minutes = Math.floor(totalSeconds / 60) % 60
        let seconds = totalSeconds % 60

        let timeString = ""
        if (totalSeconds > 86400) timeString += `${(days < 10) ? "0" : ""}${days}:`
        if (totalSeconds > 3600) timeString += `${(hours < 10) ? "0" : ""}${hours}:`
        if (totalSeconds > 60) timeString += `${(minutes < 10) ? "0" : ""}${minutes}:`
        timeString += `${(seconds < 10) ? "0" : ""}${seconds}`
        if (totalSeconds < 60) timeString = `${(seconds < 10) ? "0" : ""}${seconds} seconds`

        if (confirmMathQuestion) {
            let randomNumber1 = Math.ceil((Math.random() * 50))
            let randomNumber2 = Math.ceil((Math.random() * 50))
            let correctAnswer = (randomNumber1 + randomNumber2).toString()
            let message
            while (message != correctAnswer) message = prompt(`Your Organzied Crime is about to start:\n${timeString}\nSolve "${randomNumber1} + ${randomNumber2}" to continue.`)
        } else if(confirmPrompt) {
            let message
            while (message != confirmMessage) message = prompt(`Your Organzied Crime is about to start:\n${timeString}\nWrite "${confirmMessage}" to continue.`)
        } else alert(`Your Organzied Crime is about to start:\n${timeString}`)

    })
    .catch(error => console.log(error))

})();