stock-vault

stock vault for torn.com

  1. // ==UserScript==
  2. // @name stock-vault
  3. // @namespace stock-vault.zero.nao
  4. // @version 0.2
  5. // @description stock vault for torn.com
  6. // @author nao [2669774]
  7. // @match https://www.torn.com/page.php?sid=stocks*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=torn.com
  9. // @grant none
  10.  
  11. // ==/UserScript==
  12.  
  13. let stocks = {};
  14. let stockId = {};
  15.  
  16. function insert() {
  17. let current = localStorage.stockVault;
  18. let symbols = [];
  19. if ($("ul[class^='stock_']").length == 0) {
  20. setTimeout(insert, 500);
  21. return;
  22. }
  23. $("ul[class^='stock_']").each(function() {
  24. let sym = $("img", $(this)).attr("src").split("logos/")[1].split(".svg")[0];
  25. symbols.push(sym);
  26. // console.log($(this));
  27. stockId[sym] = $(this).attr("id");
  28. stocks[sym] = $("div[class^='price_']", $(this));
  29. });
  30. symbols.sort();
  31. let container = `<div>
  32. <select name="stock" id="stockid"><option value=""></option>`;
  33. for (let sy of symbols) {
  34. if (current && current == sy) {
  35. container += `<option value="${sy}" selected="selected">${sy}</option>`;
  36. } else {
  37. container += `<option value="${sy}">${sy}</option>`;
  38. }
  39. }
  40.  
  41. container += `</select>
  42. <button id="vaultall" class="torn-btn">Vault</button>
  43. <input type="try" placeholder="Amount" id="sellval">
  44. <button id="sellamt" class="torn-btn">Withdraw</button></div>
  45. <span id="responseStock"></span>`;
  46.  
  47. $("#stockmarketroot").prepend(container);
  48. $("#stockid").change(updateStock);
  49. $("#vaultall").on("click", vault);
  50. $("#sellamt").on("click", withdraw);
  51. $("#sellval").on("keyup", updateKMB);
  52. }
  53.  
  54. function updateStock() {
  55. // console.log(getPrice($("#stockid").attr("value")));
  56. localStorage.stockVault = $("#stockid").attr("value");
  57. }
  58.  
  59. function getPrice(id) {
  60. return parseFloat($(stocks[id]).text());
  61. }
  62.  
  63. function vault() {
  64. let symb = localStorage.stockVault;
  65. let money = parseInt(
  66. document.getElementById("user-money").getAttribute("data-money"),
  67. );
  68. let price = getPrice(symb);
  69. let amt = Math.floor(money / price);
  70.  
  71. $.post(
  72. `https://www.torn.com/page.php?sid=StockMarket&step=buyShares&rfcv=${getRFC()}`,
  73. {
  74. stockId: stockId[symb],
  75. amount: amt,
  76. },
  77. function(response) {
  78. // response = JSON.parse(response);
  79.  
  80. $("#responseStock").html(response.success ? "Vaulted" : "Failed");
  81. $("#responseStock").css("color", response.success ? "green" : "red");
  82. },
  83. );
  84. }
  85.  
  86. function updateKMB() {
  87. try {
  88. let val = $("#sellval").attr("value");
  89.  
  90. // Ensure val is a string and trim any whitespace
  91. if (typeof val !== "string") {
  92. throw new Error("Invalid input");
  93. }
  94. val = val.trim().toLowerCase();
  95.  
  96. if (
  97. val.endsWith(".") &&
  98. val.length > 1 &&
  99. !val.slice(0, -1).includes(".")
  100. ) {
  101. return; // Allow user to continue typing
  102. }
  103.  
  104. // Parse the value based on suffix
  105. if (val.endsWith("k")) {
  106. val = val.replace("k", "") * 1000;
  107. } else if (val.endsWith("m")) {
  108. val = val.replace("m", "") * 1000000;
  109. } else if (val.endsWith("b")) {
  110. val = val.replace("b", "") * 1000000000;
  111. } else if (!isNaN(val) && val.length > 0) {
  112. val = val; // Handle numeric values directly
  113. } else {
  114. throw new Error("Invalid input format");
  115. }
  116. // Update the attribute with the parsed value
  117. $("#sellval").attr("value", val);
  118. } catch (e) {
  119. // Handle errors by resetting the value
  120. $("#sellval").attr("value", "");
  121. console.error("Error in updateKMB:", e.message);
  122. }
  123. }
  124.  
  125. function withdraw() {
  126. let symb = localStorage.stockVault;
  127. let val = parseFloat($("#sellval").attr("value")) / 0.999;
  128. let price = getPrice(symb);
  129. let amt = Math.ceil(val / price);
  130. console.log(`Value: ${val}, Price: ${price}, Amount: ${amt}`);
  131. $.post(
  132. `https://www.torn.com/page.php?sid=StockMarket&step=sellShares&rfcv=${getRFC()}`,
  133. {
  134. stockId: stockId[symb],
  135. amount: amt,
  136. },
  137. function(response) {
  138. // response = JSON.parse(response);
  139.  
  140. $("#responseStock").html(response.success ? "Withdrawn" : "Failed");
  141. $("#responseStock").css("color", response.success ? "green" : "red");
  142. },
  143. );
  144. }
  145.  
  146. function getRFC() {
  147. var rfc = $.cookie("rfc_v");
  148. if (!rfc) {
  149. var cookies = document.cookie.split("; ");
  150. for (var i in cookies) {
  151. var cookie = cookies[i].split("=");
  152. if (cookie[0] == "rfc_v") {
  153. return cookie[1];
  154. }
  155. }
  156. }
  157. return rfc;
  158. }
  159.  
  160. insert();
  161.  
  162. const style = `
  163.  
  164. #sellval{
  165.  
  166. color: white;
  167. border: 2px solid #609b9b;
  168. border-radius: 10px;
  169. padding: 2px 25px;
  170. background: transparent;
  171. max-width: 190px;
  172. }
  173. #sellval:active {
  174. box-shadow: 2px 2px 15px #8707ff inset;
  175. }
  176.  
  177. `;
  178. const styleSheet = document.createElement("style");
  179. styleSheet.textContent = style;
  180. (document.head || document.documentElement).appendChild(styleSheet);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址