Flying OC Alert

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

当前为 2025-04-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Flying OC Alert
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1.0
  5. // @description Alerts you when you want to fly but an OC is about to start.
  6. // @author NichtGersti [3380912]
  7. // @license MIT
  8. // @match https://www.torn.com/page.php?sid=travel
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=torn.com
  10. // @grant GM_registerMenuCommand
  11. // @grant GM_unregisterMenuCommand
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. let apiKey = localStorage.getItem("nichtgersti-flying-oc-alert-apikey") ?? '###PDA-APIKEY###' //Minimal access or above!
  18. let maxWarningHours = localStorage.getItem("nichtgersti-flying-oc-alert-max-warning-hours") ?? 10
  19. let alertOption = Number.parseInt(localStorage.getItem("nichtgersti-flying-oc-alert-alert-option")) ?? 0 //0 -> simple alert; 1 -> confirm prompt; 2 confirm math question
  20. let confirmMessage = localStorage.getItem("nichtgersti-flying-oc-alert-confirm-message") ?? "continue" //for confirmPrompt
  21.  
  22. let alertOptionsMenuId
  23. let confirmMessageMenuId
  24.  
  25. try {
  26. GM_registerMenuCommand('Set Api Key', setApiKey)
  27. GM_registerMenuCommand('Set Max Warning Hours', setMaxWarningHours)
  28. if (alertOption == 1) confirmMessageMenuId = GM_registerMenuCommand('Set Confirm Message', setConfirmMessage)
  29. setAlertOption(alertOption)
  30. } catch (error) {
  31. console.warn('[Flying OC Alert] Tampermonkey not detected!')
  32. }
  33.  
  34. setApiKey(true)
  35.  
  36. let ocUrl = "https://api.torn.com/v2/user/?selections=organizedcrime&key={apiKey}".replace("{apiKey}", apiKey)
  37.  
  38. fetch(ocUrl).then( response => {
  39. if (response.ok) {
  40. return response.json();
  41. }
  42. throw new Error('Something went wrong');
  43. })
  44. .then( result => {
  45.  
  46. if (result.error) {
  47. switch (result.error.code){
  48. case 2:
  49. apiKey = null;
  50. localStorage.setItem("nichtgersti-flying-oc-alert-api", null)
  51. console.error("[Flying OC Alert] Incorrect Api Key:", result)
  52. return
  53. case 9:
  54. console.warn("[Flying OC Alert] The API is temporarily disabled, please try again later")
  55. return
  56. default:
  57. console.error("[Flying OC Alert] Error:", result.error.error)
  58. return
  59. }
  60. }
  61.  
  62. let infoOc = result.organizedCrime
  63.  
  64. let totalSeconds = infoOc.ready_at - Math.floor(Date.now() / 1000)
  65. if (totalSeconds / 3600 > 10) return
  66.  
  67. let days = Math.floor(totalSeconds / 86400) % 24
  68. let hours = Math.floor(totalSeconds / 3600) % 60
  69. let minutes = Math.floor(totalSeconds / 60) % 60
  70. let seconds = totalSeconds % 60
  71.  
  72. let timeString = ""
  73. if (totalSeconds > 86400) timeString += `${(days < 10) ? "0" : ""}${days}:`
  74. if (totalSeconds > 3600) timeString += `${(hours < 10) ? "0" : ""}${hours}:`
  75. if (totalSeconds > 60) timeString += `${(minutes < 10) ? "0" : ""}${minutes}:`
  76. timeString += `${(seconds < 10) ? "0" : ""}${seconds}`
  77. if (totalSeconds < 60) timeString = `${(seconds < 10) ? "0" : ""}${seconds} seconds`
  78.  
  79. let message
  80. let randomNumber1 = Math.ceil((Math.random() * 50))
  81. let randomNumber2 = Math.ceil((Math.random() * 50))
  82. let correctAnswer = (randomNumber1 + randomNumber2).toString()
  83. switch (alertOption) {
  84. case 0:
  85. alert(`Your Organzied Crime is about to start:\n${timeString}`)
  86. break
  87. case 1:
  88. while (message != confirmMessage) message = prompt(`Your Organzied Crime is about to start:\n${timeString}\nWrite "${confirmMessage}" to continue.`)
  89. break
  90. case 2:
  91. while (message != correctAnswer) message = prompt(`Your Organzied Crime is about to start:\n${timeString}\nSolve "${randomNumber1} + ${randomNumber2}" to continue.`)
  92. break
  93. default:
  94. console.log(alertOption)
  95. throw new Error('Something went wrong');
  96. }
  97. })
  98. .catch(error => console.error("[Flying OC Alert] Error:", error))
  99.  
  100. function setApiKey(checkExisting = false) {
  101. if (!checkExisting || apiKey === null || apiKey.indexOf('PDA-APIKEY') > -1 || apiKey.length != 16){
  102. let userInput = prompt("Please enter a minimal access API key.", apiKey ?? '')
  103. if (userInput !== null && userInput.length == 16) {
  104. apiKey = userInput
  105. localStorage.setItem("nichtgersti-flying-oc-alert-apikey", apiKey)
  106. }
  107. }
  108. }
  109.  
  110. function setMaxWarningHours() {
  111. let userInput = prompt("Please enter the maximum time in hours you want to get warned at.", maxWarningHours ?? '10')
  112. if (userInput !== null) {
  113. try {
  114. maxWarningHours = Number.parseFloat(userInput)
  115. localStorage.setItem("nichtgersti-flying-oc-alert-max-warning-hours", maxWarningHours)
  116. } catch (error) { alert("Not a valid input") }
  117. }
  118. }
  119.  
  120. function setAlertOption(option = (alertOption + 1) % 3) {
  121. alertOption = option
  122. localStorage.setItem("nichtgersti-flying-oc-alert-alert-option", alertOption)
  123. let optionText = ["Simple Alert", "Confirmation Prompt", "Confirmation Addition"]
  124. try {
  125. GM_unregisterMenuCommand(alertOptionsMenuId)
  126. GM_unregisterMenuCommand(confirmMessageMenuId)
  127. } catch (error) { console.log(error) }
  128. try {
  129. alertOptionsMenuId = GM_registerMenuCommand(optionText[option], function() {setAlertOption()})
  130. if (alertOption == 1) confirmMessageMenuId = GM_registerMenuCommand('Set Confirm Message', setConfirmMessage)
  131. } catch (error) { throw error }
  132. return option
  133. }
  134.  
  135. function setConfirmMessage() {
  136. let userInput = prompt("Please enter what you want to type out to confirm that you have been alerted.", confirmMessage ?? '10')
  137. if (userInput !== null) {
  138. try {
  139. confirmMessage = userInput
  140. localStorage.setItem("nichtgersti-flying-oc-alert-confirm-message", confirmMessage)
  141. } catch (error) { alert("Not a valid input") }
  142. }
  143. }
  144.  
  145. })();