Hey guys i wrote my first Script for torn. Its for the Stock Market and shows which Stock is close to the minumum prize. Feel free to use it if you like. I just wanted to share my first success. Cheers! Also be Kind please :)
// ==UserScript==
// @name Torn - Stock Auto Tracker
// @namespace http://tampermonkey.net/
// @version 1.5
// @description Adds a stock tracker button next to the City button (or in the container if City is missing) and highlights buying opportunities
// @author Deglor
// @match https://www.torn.com/page.php?sid=stocks*
// @grant none
// ==/UserScript==
(function() {
'use strict';
let stockElements = [];
let currentStockIndex = 0;
let processing = false;
function addTrackerButton() {
const checkExist = setInterval(() => {
const cityButtonContainer = document.querySelector('.linksContainer___LiOTN');
if (cityButtonContainer) {
clearInterval(checkExist);
if (document.querySelector('#stockTrackerButton')) return;
let trackerButton = document.createElement('a');
trackerButton.innerText = "Stock Tracker";
trackerButton.href = "#";
trackerButton.className = "stock-tracker-button";
trackerButton.id = "stockTrackerButton";
trackerButton.style.marginRight = "10px";
trackerButton.style.color = "inherit";
trackerButton.style.textDecoration = "none";
const cityButton = cityButtonContainer.querySelector('a[href="/city.php"]');
if (cityButton) {
cityButtonContainer.insertBefore(trackerButton, cityButton);
} else {
cityButtonContainer.appendChild(trackerButton);
}
trackerButton.addEventListener('click', (e) => {
e.preventDefault();
startStockTracking();
});
}
}, 500);
}
function startStockTracking() {
stockElements = Array.from(document.querySelectorAll("ul.stock___ElSDB"));
if (stockElements.length === 0) return;
currentStockIndex = 0;
processing = true;
processNextStock();
}
function processNextStock() {
if (currentStockIndex >= stockElements.length) {
processing = false;
return;
}
let stockElement = stockElements[currentStockIndex];
let dropdownButton = stockElement.querySelector("[data-name='priceTab']");
if (dropdownButton) {
dropdownButton.click();
waitForDropdown(stockElement, () => {
let dropdown = document.querySelector(".stockDropdown___Y2X_v");
if (dropdown) {
waitForPrices(dropdown, () => extractStockData(dropdown, stockElement));
} else {
currentStockIndex++;
processNextStock();
}
});
} else {
currentStockIndex++;
processNextStock();
}
}
function extractStockData(dropdown, stockElement) {
let priceElements = dropdown.querySelectorAll(".value___S4Cjf");
let lowPriceElement = dropdown.querySelector(".value___S4Cjf.low___r3cFG");
let highPriceElement = dropdown.querySelector(".value___S4Cjf.up___ZyXiA");
if (priceElements.length >= 2 && lowPriceElement && highPriceElement) {
let currentPrice = parseFloat(priceElements[0].innerText.replace('$', '').replace(',', ''));
let minPrice = parseFloat(lowPriceElement.innerText.replace('$', '').replace(',', ''));
let maxPrice = parseFloat(highPriceElement.innerText.replace('$', '').replace(',', ''));
let percentage = ((currentPrice - minPrice) / (maxPrice - minPrice)) * 100;
let shouldBuy = percentage <= 15;
let stockNameElement = stockElement.querySelector("[data-name='nameTab']");
if (stockNameElement) {
stockNameElement.style.setProperty("background-color", shouldBuy ? 'rgba(0, 255, 0, 0.3)' : 'rgba(255, 0, 0, 0.3)', "important");
waitForHighlight(stockNameElement);
}
} else {
currentStockIndex++;
processNextStock();
}
}
function waitForDropdown(stockElement, callback, attempts = 0) {
if (attempts > 20) {
callback();
return;
}
let dropdown = document.querySelector(".stockDropdown___Y2X_v");
if (dropdown) {
(callback, 100);
} else {
(() => waitForDropdown(stockElement, callback, attempts + 1), 50);
}
}
function waitForPrices(dropdown, callback, attempts = 0) {
if (attempts > 30) {
currentStockIndex++;
processNextStock();
return;
}
let priceElements = dropdown.querySelectorAll(".value___S4Cjf");
let lowPriceElement = dropdown.querySelector(".value___S4Cjf.low___r3cFG");
let highPriceElement = dropdown.querySelector(".value___S4Cjf.up___ZyXiA");
if (priceElements.length >= 2 && lowPriceElement && highPriceElement) {
(callback, 100);
} else {
(() => waitForPrices(dropdown, callback, attempts + 1), 50);
}
}
function waitForHighlight(stockElement, attempts = 0) {
if (attempts > 30) {
currentStockIndex++;
processNextStock();
return;
}
let bgColor = window.getComputedStyle(stockElement).backgroundColor;
if (bgColor !== 'rgba(0, 0, 0, 0)' && bgColor !== 'transparent') {
currentStockIndex++;
processNextStock();
} else {
(() => waitForHighlight(stockElement, attempts + 1), 50);
}
}
addTrackerButton();
})();