hwm_inventory_new_lot

adds new lot links for all items in inventar

当前为 2015-10-22 提交的版本,查看 最新版本

  1. //
  2. // ==UserScript==
  3. // @name hwm_inventory_new_lot
  4. // @author Pahan https://greasyfork.org/uk/users/18377-pahan
  5. // @namespace hwm_pahan
  6. // @description adds new lot links for all items in inventar
  7. // @homepage https://greasyfork.org/en/users/18377-pahan
  8. // @icon http://dcdn.heroeswm.ru/avatars/30/nc-5/30547.gif
  9. // @version 1.4
  10. // @encoding utf-8
  11. // @include http://*heroeswm.ru/inventory.php*
  12. // @include http://*heroeswm.ru/auction_new_lot.php*
  13. // @include http://*lordswm.com/inventory.php*
  14. // @include http://*lordswm.com/auction_new_lot.php*
  15. // @include http://178.248.235.15/inventory.php*
  16. // @include http://178.248.235.15/auction_new_lot.php*
  17. // @grant GM_deleteValue
  18. // @grant GM_getValue
  19. // @grant GM_listValues
  20. // @grant GM_setValue
  21. // @grant GM_addStyle
  22. // @grant GM_log
  23. // @grant GM_openInTab
  24. // @grant GM_xmlhttpRequest
  25. // ==/UserScript==
  26.  
  27. // settings
  28. LNewLotDurationDef = '3';
  29. // settings end
  30.  
  31. if (typeof GM_deleteValue != 'function') {
  32. this.GM_getValue=function (key,def) {return localStorage[key] || def;};
  33. this.GM_setValue=function (key,value) {return localStorage[key]=value;};
  34. this.GM_deleteValue=function (key) {return delete localStorage[key];};
  35.  
  36. this.GM_addStyle=function (key) {
  37. var style = document.createElement('style');
  38. style.textContent = key;
  39. document.querySelector("head").appendChild(style);
  40. }
  41. }
  42. if (typeof GM_listValues != 'function') {
  43. this.GM_listValues=function () {
  44. var values = [];
  45. for (var i=0; i<localStorage.length; i++) {
  46. values.push(localStorage.key(i));
  47. }
  48. return values;
  49. }
  50. }
  51.  
  52. function addEvent(elem, evType, fn) {
  53. // elem["on" + evType] = fn;
  54. if (elem.addEventListener) {
  55. elem.addEventListener(evType, fn, false);
  56. }
  57. else if (elem.attachEvent) {
  58. elem.attachEvent("on" + evType, fn);
  59. }
  60. else {
  61. elem["on" + evType] = fn;
  62. }
  63. }
  64.  
  65. function $(id) { return document.querySelector(id); }
  66.  
  67. function URLAttrValueGet(attr_name, aurl)
  68. {
  69. attr_name = attr_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  70. var regexS = "[\\?&]" + attr_name + "=([^&#]*)";
  71. var regex = new RegExp( regexS );
  72. var results = regex.exec( aurl );
  73. if( results == null )
  74. return "";
  75. else
  76. return results[1];
  77. }
  78.  
  79. // -----------------------------------------------
  80. var GlobalCultureName = location.href.match('lordswm') ? "en-US" : "ru-RU",
  81. GlobalStrings = {
  82. "ru-RU" : {
  83. Sell : "Пр-ь:",
  84. _30m : "30м",
  85. _1h : "1ч",
  86. _3h : "3ч",
  87. _6h : "6ч",
  88. _12h : "12ч",
  89. _1d : "1д",
  90. _2d : "2д",
  91. _3d : "3д",
  92. },
  93. "en-US" : {
  94. Sell : "Sell:",
  95. _30m : "30m",
  96. _1h : "1h",
  97. _3h : "3h",
  98. _6h : "6h",
  99. _12h : "12h",
  100. _1d : "1d",
  101. _2d : "2d",
  102. _3d : "3d",
  103. }
  104. },
  105. GlobalLocalizedString = GlobalStrings[GlobalCultureName];
  106. // -----------------------------------------------
  107.  
  108.  
  109. function GetProchkaInfo(ALink)
  110. {
  111. var LElem = ALink.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
  112. var regex = /\:\s+(\d+\/\d+)\<br\>/;
  113. var regex_res = regex.exec(LElem.innerHTML);
  114. if(regex_res)
  115. return regex_res[1];
  116. else
  117. return '';
  118. }
  119.  
  120. function CheckCanSell(ALink)
  121. {
  122. var LElem = ALink.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
  123. var LLinks = LElem.querySelectorAll('a[href*="art_transfer.php"]');
  124. return (LLinks && (LLinks.length == 1));
  125. }
  126. function AddNewLotHref(ALink, AURL, ADurationDisp, ADuration)
  127. {
  128. ALink.parentNode.appendChild(document.createTextNode(' '));
  129. LNewLotHref = document.createElement('a');
  130. LNewLotHref.href = AURL + '&d=' + ADuration;
  131. LNewLotHref.innerHTML = ADurationDisp;
  132. ALink.parentNode.appendChild(LNewLotHref);
  133. }
  134. function SetTimer_ProcessMain()
  135. {
  136. setTimeout(AddNewLotHrefs, 10);
  137. }
  138.  
  139. function AddNewLotHrefs()
  140. {
  141. var LLinks = document.querySelectorAll('a[href^="art_info.php"]');
  142. var LLink;
  143. if (LLinks)
  144. {
  145. for(i = 0; i < LLinks.length; i++)
  146. {
  147. LLink = LLinks[i];
  148. if (CheckCanSell(LLink))
  149. {
  150. var LName = LLink.children[0].innerHTML + ' ' + GetProchkaInfo(LLink);
  151. var LURL = '/auction_new_lot.php?art=' + encodeURIComponent(LName);
  152. var LLinksTest = LLink.parentNode.querySelectorAll('a[href^="' + LURL + '"]');
  153. if (LLinksTest && (LLinksTest.length > 0))
  154. continue;
  155.  
  156. LLink.parentNode.appendChild(document.createElement('br'));
  157. // LLink.parentNode.appendChild(document.createTextNode('»»'));
  158. LLink.parentNode.appendChild(document.createTextNode(GlobalLocalizedString.Sell));
  159.  
  160. // LNewLotHref = document.createElement('a');
  161. // LNewLotHref.href = LURL;
  162. // LNewLotHref.innerHTML = GlobalLocalizedString.Sell;
  163. // LLink.parentNode.appendChild(LNewLotHref);
  164. AddNewLotHref(LLink, LURL, GlobalLocalizedString._30m, '30m');
  165. AddNewLotHref(LLink, LURL, GlobalLocalizedString._1h, '1h');
  166. AddNewLotHref(LLink, LURL, GlobalLocalizedString._3h, '3h');
  167. AddNewLotHref(LLink, LURL, GlobalLocalizedString._6h, '6h');
  168. AddNewLotHref(LLink, LURL, GlobalLocalizedString._12h, '12h');
  169. AddNewLotHref(LLink, LURL, GlobalLocalizedString._1d, '1d');
  170. AddNewLotHref(LLink, LURL, GlobalLocalizedString._2d, '2d');
  171. AddNewLotHref(LLink, LURL, GlobalLocalizedString._3d, '3d');
  172. }
  173. }
  174. }
  175. }
  176.  
  177. //----------------------------------------
  178.  
  179. function PriceTrimNewLotForm()
  180. {
  181. var LPriceEl = document.forms.f.price;
  182. LPriceEl.value = LPriceEl.value.trim().replace(/[^\d]/g, '');
  183. }
  184.  
  185. function SavePrice()
  186. {
  187. var LSelect = document.forms.f.item;
  188. var LPriceEl = document.forms.f.price;
  189. var LName = LSelect.options[LSelect.selectedIndex].text.split(' (')[0].split(' [i]')[0];
  190. GM_setValue(LName, LPriceEl.value);
  191. LInfo = document.createTextNode('<b>Сохранена цена ' + LPriceEl.value + ' для артефакта "' + LName + '"<b>');
  192.  
  193. LInfo = $('#save_price_info');
  194. if (!LInfo)
  195. {
  196. LInfo = document.createElement('b');
  197. LInfo.id = 'save_price_info';
  198. $('#id_save_price').parentNode.appendChild(LInfo);
  199. }
  200. LInfo.innerHTML = 'Сохранена цена ' + LPriceEl.value + ' для артефакта "' + LName + '"';
  201. }
  202.  
  203. function LoadPrice()
  204. {
  205. var LSelect = document.forms.f.item;
  206. var LPriceEl = document.forms.f.price;
  207. var LName = LSelect.options[LSelect.selectedIndex].text.split(' (')[0].split(' [i]')[0];
  208. LPriceEl.value = GM_getValue(LName, '0');
  209. }
  210.  
  211. function DecrementPrice()
  212. {
  213. var LPriceEl = document.forms.f.price;
  214. LPriceEl.value = parseInt(LPriceEl.value) - 1;
  215. }
  216.  
  217. function IncrementPrice()
  218. {
  219. var LPriceEl = document.forms.f.price;
  220. LPriceEl.value = parseInt(LPriceEl.value) + 1;
  221. }
  222.  
  223. function InitNewLotForm()
  224. {
  225. var LSelect = document.forms.f.item;
  226. if (LSelect && (LSelect.type != 'hidden'))
  227. {
  228. addEvent(LSelect, 'change', LoadPrice);
  229. var LArt = decodeURIComponent(URLAttrValueGet('art', location.href));
  230. var LArtFull = '';
  231. if (LArt != '')
  232. {
  233. for(i = 0; i < LSelect.options.length; i++)
  234. {
  235. var LOption = LSelect.options[i];
  236. if (LOption.text.indexOf(LArt) == 0)
  237. {
  238. LArtFull = LOption.text;
  239. LSelect.selectedIndex = LOption.index;
  240. }
  241. }
  242. }
  243. }
  244. var LCountEl = document.forms.f.count;
  245. if (LCountEl && (LCountEl.type != 'hidden'))
  246. {
  247. var LCount = 1;
  248. if (LArtFull != '')
  249. {
  250. var regex = /\((\d+)\)/;
  251. var regex_res = regex.exec(LArtFull);
  252. if (regex_res)
  253. LCount = Math.min(parseInt(regex_res[1]), 3);
  254. }
  255. LCountEl.value = LCount;
  256. }
  257. var LPriceEl = document.forms.f.price;
  258. if (LPriceEl && (LPriceEl.type != 'hidden'))
  259. {
  260. addEvent(LPriceEl, "change", PriceTrimNewLotForm);
  261. addEvent(LPriceEl, "keyup", PriceTrimNewLotForm);
  262. addEvent(LPriceEl, "paste", PriceTrimNewLotForm);
  263. LoadPrice();
  264.  
  265. LNewDiv = document.createElement('b');
  266. LNewDiv.innerHTML = '<input type="button" style="width:80;" id="id_save_price" value="Сохранить">';
  267. LPriceEl.parentNode.insertBefore(LNewDiv, LPriceEl.nextSibling);
  268. addEvent($('#id_save_price'), "click", SavePrice);
  269.  
  270. LNewDiv = document.createElement('b');
  271. LNewDiv.innerHTML =
  272. '<input type="button" style="width:25;height:25;font-size=10px;padding:0;margin:0;" id="id_decrement_price" value="-">' +
  273. '<input type="button" style="width:25;height:25;font-size=10px;padding:0;margin:0;" id="id_increment_price" value="+">';
  274. LPriceEl.parentNode.insertBefore(LNewDiv, LPriceEl.nextSibling);
  275. addEvent($('#id_decrement_price'), "click", DecrementPrice);
  276. addEvent($('#id_increment_price'), "click", IncrementPrice);
  277. }
  278. var LDurationEl = document.forms.f.duration;
  279. if (LDurationEl && (LDurationEl.type != 'hidden'))
  280. {
  281. var LDuration = LNewLotDurationDef;
  282. var LDurationParam = URLAttrValueGet('d', location.href);
  283. if (LDurationParam == '30m')
  284. LDuration = 1;
  285. else
  286. if (LDurationParam == '1h')
  287. LDuration = 2;
  288. else
  289. if (LDurationParam == '3h')
  290. LDuration = 3;
  291. else
  292. if (LDurationParam == '6h')
  293. LDuration = 4;
  294. else
  295. if (LDurationParam == '12h')
  296. LDuration = 5;
  297. else
  298. if (LDurationParam == '1d')
  299. LDuration = 6;
  300. else
  301. if (LDurationParam == '2d')
  302. LDuration = 7;
  303. else
  304. if (LDurationParam == '3d')
  305. LDuration = 8;
  306. for(i = 0; i < LDurationEl.options.length; i++)
  307. {
  308. var LOption = LDurationEl.options[i];
  309. if (LOption.value == LDuration)
  310. {
  311. LDurationEl.selectedIndex = LOption.index;
  312. }
  313. }
  314. }
  315. }
  316.  
  317. //----------------------------------------------------
  318.  
  319. function ProcessMain()
  320. {
  321. if (location.href.indexOf('/inventory.php') > -1)
  322. {
  323. AddNewLotHrefs();
  324. if(!$("#click_div"))
  325. {
  326. var add_click_div = document.createElement('div');
  327. add_click_div.id = "click_div";
  328. add_click_div.style.display = "none";
  329. document.querySelector("body").appendChild(add_click_div);
  330. }
  331. addEvent($("#click_div"), "click", SetTimer_ProcessMain);
  332. }
  333. else
  334. if (location.href.indexOf('/auction_new_lot.php') > -1)
  335. {
  336. InitNewLotForm();
  337. }
  338. }
  339.  
  340. ProcessMain();