Subeta Searching

This script searches for shops.

目前为 2017-06-28 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/30529/203146/Subeta%20Searching.js

  1. // ==UserScript==
  2. // @name Subeta Searching
  3. // @namespace http://artees.pw/
  4. // @description This script searches for shops.
  5. // @version 1.3
  6. // @author Artees
  7. // @match *://subeta.net/user_shops.php/mine/*
  8. // @match *://subeta.net/shop.php?shop*
  9. // @exclude *quick_stock*
  10. // @exclude *profits*
  11. // @exclude *history*
  12. // @exclude *edit_shop*
  13. // @exclude *delete_shop*
  14. // @grant none
  15. // @icon https://subeta.net/favicon.ico
  16. // @icon64 https://img.subeta.net/items/identifier_04_crystal.gif
  17. // ==/UserScript==
  18.  
  19. const CY = " sP",
  20. SPLITTER = " | ",
  21. XHR = new XMLHttpRequest(),
  22. KEY_ITEM_DELAY = "itemDelay";
  23.  
  24. var itemDelay = getFromStorage(localStorage, KEY_ITEM_DELAY, 5),
  25. itemCountdown = itemDelay;
  26.  
  27. function parseItems(itemDivs, getPriceField) {
  28. var prevItem = null,
  29. firstItem;
  30. for (var i = 0; i < itemDivs.length; i++) {
  31. var priceField = getPriceField(itemDivs[i]);
  32. if (priceField === null) {
  33. continue;
  34. }
  35. var curItem = new Item(itemDivs[i], priceField);
  36. if (curItem.priceField === null) {
  37. continue;
  38. }
  39. if (prevItem === null) {
  40. firstItem = curItem;
  41. } else {
  42. prevItem.next = curItem;
  43. }
  44. prevItem = curItem;
  45. }
  46. prevItem.next = firstItem;
  47. return firstItem;
  48. }
  49.  
  50. function getFromStorage(storage, key, defaultValue) {
  51. if (storage[key] === undefined) {
  52. return defaultValue;
  53. } else {
  54. return typeof defaultValue === "boolean" ? storage[key] === "true" : storage[key];
  55. }
  56. }
  57.  
  58. function forEachItem(action) {
  59. var f = curItem,
  60. i = f;
  61. do {
  62. if (!action(i)) {
  63. return;
  64. }
  65. i = i.next;
  66. } while (i !== f);
  67. }
  68.  
  69. function createSpace() {
  70. var space = document.createElement("SPAN");
  71. space.innerText = "|";
  72. space.style = "display: inline-block; width: 50px;";
  73. return space;
  74. }
  75.  
  76. function search() {
  77. XHR.open("GET", "https://subeta.net/user_shops.php/search/shops/" + curItem.name, true);
  78. XHR.send();
  79. curItem.priceField.value += SPLITTER;
  80. loadIntervalId = setInterval(updateSearchProgress, 100);
  81. }
  82.  
  83. function updateSearchProgress() {
  84. if (curItem.priceField.value.length > 20) {
  85. curItem.priceField.value = "";
  86. XHR.abort();
  87. clearInterval(loadIntervalId);
  88. search();
  89. return;
  90. }
  91. curItem.priceField.value += ".";
  92. }
  93.  
  94. XHR.onreadystatechange = function setLowestPrice() {
  95. if (XHR.readyState !== XMLHttpRequest.DONE || XHR.status !== 200) return;
  96. clearInterval(loadIntervalId);
  97. var pricesText = XHR.responseText.split(CY),
  98. prices = [];
  99. for (var i = 0; i < pricesText.length; i++) {
  100. var priceWithCommas = pricesText[i].substring(pricesText[i].lastIndexOf(">") + 1),
  101. priceText = replaceAll(priceWithCommas, ",", ""),
  102. price = parseInt(priceText);
  103. if (isNaN(price)) continue;
  104. var isNPC = pricesText[i].indexOf("(NPC Shop)") !== -1,
  105. priceObject = new Price(price, isNPC);
  106. prices.push(priceObject);
  107. }
  108. if (prices.length > 0) {
  109. prices.sort(compareNumbers);
  110. }
  111. filterPrices(prices);
  112. curItem = curItem.next;
  113. if (curItem.onSelect()) {
  114. itemIntervalId = setInterval(updateItemCountdown, 1000);
  115. }
  116. };
  117.  
  118. function Price(value, isNPC) {
  119. this.isNPC = function () {
  120. return isNPC;
  121. };
  122. this.valueOf = function () {
  123. return value;
  124. };
  125. this.toString = function () {
  126. return value.toString();
  127. };
  128. }
  129.  
  130. function replaceAll(target, searchValue, replaceValue) {
  131. return target.replace(new RegExp(searchValue, 'g'), replaceValue);
  132. }
  133.  
  134. function compareNumbers(a, b)
  135. {
  136. return a - b;
  137. }
  138.  
  139. function updateItemCountdown() {
  140. curItem.priceField.value = curItem.resetPrice() + SPLITTER + itemCountdown;
  141. itemCountdown--;
  142. if (itemCountdown >= 0) return;
  143. curItem.priceField.value = curItem.resetPrice();
  144. itemCountdown = itemDelay;
  145. clearInterval(itemIntervalId);
  146. search();
  147. }