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