NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name TORN: Bazaar Safety
// @version 2.2.0
// @author DeKleineKobini
// @description Favorite items and put them at the top
// @namespace dekleinekobini.bazaarsafety
// @require https://openuserjs.org/src/libs/DeKleineKobini/DKK_Torn_Utilities.js
// @updateURL https://openuserjs.org/meta/DeKleineKobini/TORN_Bazaar_Safety.meta.js
// @match https://www.torn.com/bazaar.php*
// @license MIT
// @connect api.torn.com
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
setDebug(false);
/* --------------------
CODE - EDIT ON OWN RISK
-------------------- */
requireAPI();
setPrefixEasy("BS");
var apiItems = {};
getCache("tornapi_items").then(response => {
if (response) {
debug("Loaded items from the cache!");
apiItems = response;
} else {
sendAPIRequest("torn", null, "items").then(function(response) {
debug("Loaded items from the api!");
if (response.items) apiItems = response.items;
setCache("tornapi_items", response.items, getMillisUntilNewDay());
});
}
});
$(window).bind('hashchange', function() {
if (window.location.href === "https://www.torn.com/bazaar.php#/p=add") {
debug("Switched to add items page.");
setupAddItem();
} else if (window.location.href === "https://www.torn.com/bazaar.php#/p=manage") {
debug("Switched to manage items page.");
setupManageItem();
}
});
setupAddItem();
setupManageItem();
function setupAddItem() {
debug("Setup the observer for 'add items'.");
observeMutations(document, ".items-footer", true, function(){
observeMutations($(".items-footer")[1], "input[value='CONFIRM']", false, checkSafetyOnAddItem, { childList: true }, removeSafetyFromAddItem);
}, { childList: true, subtree: true });
}
function setupManageItem() {
debug("Setup the observer for 'manage items'.");
observeMutations(document, ".bazaar-page-wrap", true, function(){
observeMutations($(".bazaar-page-wrap")[0], ".bazaar-list-items", false, function() {
var safeties = [];
$(".input-money").on('input', function() {
let input = $(this);
let row = $(this).parent().parent().parent().parent();
let id = row.find(".rrp input").attr("value");
let price = currencyToNumber(input.val());
let marketPrice = apiItems[id].market_value;
var enableSafety = false, disableSafety = false;
if (price < marketPrice) {
if (!safeties.includes(id)) {
row.css("background-color", "lightcoral")
enableSafety = true;
safeties.push(id);
}
} else if (safeties.includes(id)) {
row.css("background-color", "initial")
disableSafety = true;
safeties = safeties.filter(function(ele){
return ele != id;
});
}
let button = $(".save input");
let buttonParent = button.parent();
if (enableSafety && safeties.length == 1) {
log("Enabling safety.")
button.attr("disabled", true);
buttonParent.css("border", "1 px soldid #999999");
buttonParent.css("background-color", "#cccccc");
buttonParent.css("color", "#666666");
}
if (disableSafety && safeties.length == 0) {
log("Disabling safety.")
button.removeAttr("disabled");
buttonParent.css("border", "initial");
buttonParent.css("background-color", "initial");
buttonParent.css("color", "initial");
}
});
});
}, { childList: true, subtree: true });
}
function checkSafetyOnAddItem() {
debug("Checking safety.")
var enableSafety = false;
$(".category-wrap > .market-confirm > li").each(function() {
let row = $(this);
let id = row.find("img").attr("src").split("/")[3];
let price = currencyToNumber(row.find("input").val());
let marketPrice = apiItems[id].market_value;
debug(id + " / " + price + " / " + marketPrice);
if (price < marketPrice) {
row.css("background-color", "lightcoral")
enableSafety = true;
}
});
if (enableSafety) {
log("Enabling safety.")
let button = $("input[value='CONFIRM']");
let buttonParent = button.parent();
button.attr("disabled", true);
buttonParent.css("border", "1 px soldid #999999");
buttonParent.css("background-color", "#cccccc");
buttonParent.css("color", "#666666");
}
}
function removeSafetyFromAddItem() {
log("Disabling safety.")
$(".category-wrap > .market-confirm > li").each(function() {
let row = $(this);
row.css("background-color", "")
});
}
function currencyToNumber(currency) {
if(contains(currency, "$")) currency = currency.substring(1);
currency = replaceAll(currency, ",", "");
return parseInt(currency);
}