counterfeit_blocker

Block counterfeit in bazzar, for example, vic sold at 830k.

  1. // ==UserScript==
  2. // @name counterfeit_blocker
  3. // @namespace nodelore.torn.easy-market
  4. // @version 1.0
  5. // @description Block counterfeit in bazzar, for example, vic sold at 830k.
  6. // @author nodelore[2786679]
  7. // @match https://www.torn.com/bazaar.php*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=torn.com
  9. // @grant unsafeWindow
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. "use strict";
  15.  
  16. //================ Easy-market Configuration =======================
  17. const API = ""; // Insert your API here (PUBLIC level is fine)
  18. // Extend your block items here, just add item name
  19. const blockList = [
  20. "Vicodin",
  21. "Box of Grenades",
  22. "Mistletoe",
  23. "Medium Suitcase"
  24. ];
  25.  
  26. // You should also add threshold for the newly added item
  27. const blockThreshold = {
  28. "Vicodin": 2000,
  29. "Box of Grenades": 1500000,
  30. "Mistletoe": 1000000,
  31. "Medium Suitcase": 5000000,
  32. };
  33.  
  34. // If the item price is over BLOCK_RATE * THRESHOLD, it would be blocked. You could change the default rate here.
  35. const BLOCK_RATE = 2.0
  36. //==================================================================
  37.  
  38. if (window.COUNTERFEIT_BLOCKER) return;
  39. window.COUNTERFEIT_BLOCKER = true;
  40.  
  41. const updateBlockThreshold = async ()=>{
  42. if(!API) return;
  43. const url = `https://api.torn.com/torn/?selections=items&key=${API}`;
  44. const resp = await fetch(url);
  45. if('error' in resp){
  46. console.error("Fail to fetch item from APIs");
  47. return;
  48. }
  49. const data = await resp.json();
  50. if(data && data.items){
  51. for(let item_id in data.items){
  52. const item_detail = data.items[item_id];
  53. const {name, market_value} = item_detail;
  54. if(blockList.indexOf(name) !== -1){
  55. const origThreshold = blockThreshold[name];
  56. if(market_value < origThreshold){
  57. blockThreshold[name] = market_value;
  58. console.log(`[Counterfeit-Blocker] Update market value of ${name} to ${market_value}`);
  59. }
  60. }
  61. }
  62. }
  63. }
  64.  
  65. const pricePattern = /\$(\d{1,3}(?:,\d{3})*|\d{1,2})(?:\.\d+)?/;
  66.  
  67. const blockCounterfeit = function (item) {
  68. const name = item.find("p[class^='name']").text();
  69. const priceText = item.find("p[class^='price']").text().trim();
  70. const match = pricePattern.exec(priceText);
  71. if (name !== "" && match) {
  72. const matchPrice = match[1];
  73. const stringWithoutCommas = matchPrice.replace(/,/g, "");
  74. const priceValue = parseInt(stringWithoutCommas, 10);
  75. if (blockThreshold[name]) {
  76. if (priceValue > blockThreshold[name]*BLOCK_RATE) {
  77. console.log(
  78. `[Counterfeit-Blocker] Detect counterfeid ${name} with price ${priceValue}, block`
  79. );
  80. item.hide();
  81. }
  82. }
  83. }
  84. };
  85.  
  86. updateBlockThreshold().then(()=>{
  87. waitForKeyElements("div[class^='itemDescription']", blockCounterfeit);
  88. });
  89. })();
  90.  
  91. function waitForKeyElements(
  92. selectorTxt,
  93. actionFunction,
  94. bWaitOnce,
  95. iframeSelector
  96. ) {
  97. var targetNodes, btargetsFound;
  98. if (typeof iframeSelector == "undefined") targetNodes = $(selectorTxt);
  99. else targetNodes = $(iframeSelector).contents().find(selectorTxt);
  100.  
  101. if (targetNodes && targetNodes.length > 0) {
  102. btargetsFound = true;
  103. /*--- Found target node(s). Go through each and act if they
  104. are new.
  105. */
  106. targetNodes.each(function () {
  107. var jThis = $(this);
  108. var alreadyFound = jThis.data("alreadyFound") || false;
  109.  
  110. if (!alreadyFound) {
  111. //--- Call the payload function.
  112. var cancelFound = actionFunction(jThis);
  113. if (cancelFound) btargetsFound = false;
  114. else jThis.data("alreadyFound", true);
  115. }
  116. });
  117. } else {
  118. btargetsFound = false;
  119. }
  120.  
  121. //--- Get the timer-control variable for this selector.
  122. var controlObj = waitForKeyElements.controlObj || {};
  123. var controlKey = selectorTxt.replace(/[^\w]/g, "_");
  124. var timeControl = controlObj[controlKey];
  125.  
  126. //--- Now set or clear the timer as appropriate.
  127. if (btargetsFound && bWaitOnce && timeControl) {
  128. //--- The only condition where we need to clear the timer.
  129. clearInterval(timeControl);
  130. delete controlObj[controlKey];
  131. } else {
  132. //--- Set a timer, if needed.
  133. if (!timeControl) {
  134. timeControl = setInterval(function () {
  135. waitForKeyElements(
  136. selectorTxt,
  137. actionFunction,
  138. bWaitOnce,
  139. iframeSelector
  140. );
  141. }, 300);
  142. controlObj[controlKey] = timeControl;
  143. }
  144. }
  145. waitForKeyElements.controlObj = controlObj;
  146. }