Ghost Trade Buttons v2

Adds buttons to remove at million $ intervals to the trade page to make it easier to manage money in ghost trades.

  1. // ==UserScript==
  2. // @name Ghost Trade Buttons v2
  3. // @namespace Titanic_
  4. // @version 2.2
  5. // @description Adds buttons to remove at million $ intervals to the trade page to make it easier to manage money in ghost trades.
  6. // @license MIT
  7. // @author Titanic_ [2968477]
  8. // @match https://www.torn.com/trade.php*
  9. // @grant none
  10. // ==/UserScript==
  11. (function () {
  12. 'use strict';
  13. // Feel free to add to or change these
  14. const BUTTONS = [
  15. { label: "-100k", amount: 100000 },
  16. { label: "-500k", amount: 500000 },
  17. { label: "-1m", amount: 1000000 },
  18. { label: "-5m", amount: 5000000 },
  19. { label: "-10m", amount: 10000000 }
  20. ];
  21. function addElements() {
  22. const div = document.querySelector("div.input-money-group");
  23. if (!div) return;
  24. const spacerRef = document.querySelector("span.btn-wrap.silver");
  25. if (!spacerRef) return;
  26. const spacer = spacerRef.previousElementSibling?.cloneNode();
  27. const parent = document.createElement("div");
  28. if (spacer) parent.prepend(spacer);
  29. BUTTONS.forEach(btn => addButton(parent, btn.label, btn.amount));
  30. addCustomButton(parent);
  31. addPasteButton(parent);
  32. div.parentNode.insertBefore(parent, div.nextSibling);
  33. }
  34. function addButton(parent, label, amount) {
  35. let btn = createButton(label, () => adjustMoney(-amount));
  36. parent.appendChild(btn);
  37. }
  38. function addPasteButton(parent) {
  39. let btn = createButton("Paste", async () => {
  40. try {
  41. let text = await navigator.clipboard.readText();
  42. let value = parseInt(text.replace(/[, $]/g, ""));
  43. if (isNaN(value)) alert("Clipboard does not contain a valid number.");
  44. else adjustMoney(value, true);
  45. } catch (err) {
  46. alert("Clipboard access denied. Please paste manually.");
  47. }
  48. });
  49. parent.appendChild(btn);
  50. }
  51. function addCustomButton(parent) {
  52. let btn = createButton("Custom", async () => {
  53. let input = prompt("Enter amount to subtract:");
  54. let value = parseInt(input.replace(/[, $]/g, ''));
  55. if (!isNaN(value)) adjustMoney(-value);
  56. else alert("Invalid number entered.");
  57. });
  58. parent.appendChild(btn);
  59. }
  60. function createButton(label, onClick) {
  61. let btn = document.createElement("input");
  62. btn.value = label;
  63. btn.type = "button";
  64. btn.classList.add("torn-btn");
  65. btn.addEventListener("click", onClick);
  66. return btn;
  67. }
  68. function adjustMoney(amount, set = false) {
  69. let [inputVisible, inputHidden] = document.querySelectorAll(".user-id.input-money");
  70. if (!inputVisible || !inputHidden) return;
  71. let newValue = set ? amount : (parseInt(inputHidden.value) || 0) + amount;
  72. if (newValue < 0) newValue = 0;
  73. inputVisible.value = newValue;
  74. inputVisible.dispatchEvent(new Event("input", { bubbles: true }));
  75. }
  76. function observe() {
  77. if (!window.location.href.includes("trade.php#step=addmoney")) return;
  78. clearInterval(window.GhostTradeInterval);
  79. window.GhostTradeInterval = setInterval(() => {
  80. if (document.querySelector('.user-id.input-money') &&
  81. document.querySelectorAll("ul.inputs > li > div").length < 3) {
  82. clearInterval(window.GhostTradeInterval);
  83. addElements();
  84. }
  85. }, 100);
  86. }
  87. window.addEventListener("hashchange", observe);
  88. observe();
  89. })();