hwm_inventory_new_lot

Выставление лота из инвентаря, сохранение цены для предмета

  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 Выставление лота из инвентаря, сохранение цены для предмета
  7. // @homepage https://greasyfork.org/uk/scripts/13223-hwm-inventory-new-lot
  8. // @icon http://dcdn.heroeswm.ru/avatars/30/nc-5/30547.gif
  9. // @version 1.73
  10. // @encoding utf-8
  11. // @include http://*heroeswm.ru/inventory.php*
  12. // @include http://*heroeswm.ru/auction_new_lot.php*
  13. // @include http://*heroeswm.ru/transfer.php*
  14. // @include http://*lordswm.com/inventory.php*
  15. // @include http://*lordswm.com/auction_new_lot.php*
  16. // @include http://*lordswm.ru/transfer.php*
  17. // @include http://178.248.235.15/inventory.php*
  18. // @include http://178.248.235.15/auction_new_lot.php*
  19. // @include http://178.248.235.15/transfer.php*
  20. // @grant GM_deleteValue
  21. // @grant GM_getValue
  22. // @grant GM_listValues
  23. // @grant GM_setValue
  24. // @grant GM_addStyle
  25. // @grant GM_log
  26. // @grant GM_openInTab
  27. // @grant GM_xmlhttpRequest
  28. // ==/UserScript==
  29.  
  30. // settings
  31. LNewLotDurationDef = '3';
  32. // settings end
  33.  
  34. var version = '1.73';
  35. var idn = 'inventory_new_lot';
  36. var str_url = 'https://greasyfork.org/uk/scripts/13223-hwm-inventory-new-lot';
  37. var str_script_name = 'Новый лот из инвентаря';
  38. var str_url_aut = '/sms-create.php?mailto=Pahan&subject=Скрипт: '+str_script_name+' v'+version+'. Найдена ошибка:';
  39. var str_url_sps = '/transfer.php?pahan_sps=' + encodeURIComponent('Подарок. Спасибо за скрипт: '+str_script_name+' v'+version+'.');
  40.  
  41. if (typeof GM_deleteValue != 'function') {
  42. this.GM_getValue=function (key,def) {return localStorage[key] || def;};
  43. this.GM_setValue=function (key,value) {return localStorage[key]=value;};
  44. this.GM_deleteValue=function (key) {return delete localStorage[key];};
  45.  
  46. this.GM_addStyle=function (key) {
  47. var style = document.createElement('style');
  48. style.textContent = key;
  49. document.querySelector("head").appendChild(style);
  50. }
  51. }
  52. if (typeof GM_listValues != 'function') {
  53. this.GM_listValues=function () {
  54. var values = [];
  55. for (var i=0; i<localStorage.length; i++) {
  56. values.push(localStorage.key(i));
  57. }
  58. return values;
  59. }
  60. }
  61.  
  62. GM_addStyle('.dialog {background-color: #F6F3EA; border-radius: 5px; box-sizing: border-box; box-shadow: 0 0 0px 12px rgba(200, 200, 200, 0.5); left: calc(50% - 300px); max-height: calc(100% - 100px); overflow: auto; padding: 15px; position: fixed; top: 50px; z-index: 1105;}');
  63. GM_addStyle('.dialogOverlay {background-color: rgba(0, 0, 0, 0.7); height: 100%; left: 0; position: fixed; top: 0; width: 100%;}');
  64. GM_addStyle('.btn_close {position:absolute;left:calc(100% - 45px);float:right;border:1px solid #abc;width:15px;height:15px;text-align:center;cursor:pointer;}');
  65. GM_addStyle('.btn_settings {text-decoration:underline;cursor:pointer;font-weight:bold;font-size:10px;width:500px;}');
  66. GM_addStyle('.small_text {font-weight:bold;font-size:10px;}');
  67.  
  68. function addEvent(elem, evType, fn) {
  69. // elem["on" + evType] = fn;
  70. if (elem.addEventListener) {
  71. elem.addEventListener(evType, fn, false);
  72. }
  73. else if (elem.attachEvent) {
  74. elem.attachEvent("on" + evType, fn);
  75. }
  76. else {
  77. elem["on" + evType] = fn;
  78. }
  79. }
  80.  
  81. function $(id) { return document.querySelector(id); }
  82.  
  83. function URLAttrValueGet(attr_name, aurl)
  84. {
  85. attr_name = attr_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  86. var regexS = "[\\?&]" + attr_name + "=([^&#]*)";
  87. var regex = new RegExp( regexS );
  88. var results = regex.exec( aurl );
  89. if( results == null )
  90. return "";
  91. else
  92. return results[1];
  93. }
  94.  
  95. function Ajax(AMethod, AURL, AParams, ACallBackFunc)
  96. {
  97. var LRequest = new XMLHttpRequest();
  98. LRequest.open(AMethod, AURL, true);
  99. LRequest.overrideMimeType('text/html; charset=windows-1251');
  100. LRequest.send(AParams);
  101. LRequest.onreadystatechange = function()
  102. {
  103. if (LRequest.readyState == 4)
  104. {
  105. ACallBackFunc(LRequest.responseText);
  106. }
  107. };
  108. }
  109.  
  110. function Trim(AText)
  111. {
  112. return AText.replace(/^\s+|\s+$|^(&nbsp;)+|(&nbsp;)+$/g, '');
  113. }
  114.  
  115. function DeleteCRLF(AText)
  116. {
  117. return AText.replace(/[\n\r]/g, ' ').replace(/\s{2,}/g, ' ');
  118. }
  119.  
  120. function Assert(ACondition, AMessage)
  121. {
  122. if (ACondition)
  123. return;
  124. localStorage['trade.status'] = 'Stoped';
  125. alert('ERROR: ' + AMessage);
  126. throw new Error(AMessage);
  127. }
  128.  
  129. function FloatFormat(AFloat)
  130. {
  131. return Math.round(AFloat).toString();
  132. }
  133.  
  134. function IntFormatWithThouthandSeparator(num){
  135. var n = num.toString(), p = n.indexOf('.');
  136. return n.replace(/\d(?=(?:\d{3})+(?:\.|$))/g, function($0, i){
  137. return p<0 || i<p ? ($0+',') : $0;
  138. });
  139. }
  140.  
  141. // ------------------ dialog
  142.  
  143. function close_setting_form() {
  144. bg = $('#bgOverlay' + idn) ;
  145. bgc = $('#bgCenter' + idn) ;
  146. if( bg ) {
  147. bg.style.display = bgc.style.display = 'none' ;
  148. }
  149. }
  150.  
  151. function clear_all_params()
  152. {
  153. if (confirm("Вы действительно хотите скинуть все сохраненные настройки?"))
  154. {
  155. var keys = GM_listValues();
  156. for (var i=0, key=null; key=keys[i]; i++) {
  157. GM_deleteValue(key);
  158. }
  159. close_find_work_info();
  160. }
  161. }
  162.  
  163. function setCheck(ch)
  164. {
  165. if( GM_getValue( ch ) && GM_getValue( ch ) == 1 )
  166. GM_setValue( ch , 0 );
  167. else
  168. GM_setValue( ch , 1 );
  169. }
  170.  
  171. function setEdit(id, key)
  172. {
  173. var LValue = parseInt($('#' + id).value);
  174. GM_setValue(key, LValue);
  175. }
  176.  
  177. function show_dialog_base(ASettingsHTML)
  178. {
  179. bg = $('#bgOverlay' + idn) ;
  180. bgc = $('#bgCenter' + idn) ;
  181. if( !bg ) {
  182. bg = document.createElement('div') ;
  183. bg.id = 'bgOverlay' + idn;
  184. document.body.appendChild( bg );
  185. bg.className = 'dialogOverlay';
  186. addEvent(bg, 'click', close_setting_form);
  187.  
  188. bgc = document.createElement('div') ;
  189. bgc.id = 'bgCenter' + idn ;
  190. document.body.appendChild( bgc );
  191. bgc.className = 'dialog';
  192.  
  193. bgc.innerHTML =
  194. '<div style="border:1px solid #abc;padding:5px;margin:2px;">' +
  195. ' <div class="btn_close" id="bt_close'+idn+'" title="Закрыть">x</div>' +
  196. ' <center>' +
  197. ' <table>' +
  198. ' <tr>' +
  199. ' <td><b>'+str_script_name+' <font style="color:#0070FF;">'+version+'</font></b></td>' +
  200. ' </tr>' +
  201. ' <tr><td><hr/></td></tr>' +
  202. ' <tr>' +
  203. ' <td><div id="dialog_content'+idn+'">(Empty)</div></td>' +
  204. ' </tr>' +
  205. ' <tr><td><hr/></td></tr>' +
  206. ' <tr>' +
  207. ' <td class="small_text" >' +
  208. ' <a class="small_text" href="'+str_url+'">Обновить</a>&nbsp;&nbsp;' +
  209. ' <a class="small_text" href="'+str_url_aut+'">Нашли ошибку?</a>&nbsp;&nbsp;' +
  210. ' <a class="small_text" href="'+str_url_sps+'">Сказать спасибо</a>&nbsp;&nbsp;' +
  211. ' <a class="small_text" style="left:calc(50% - 100px);" href="javascript:void(0);" id="settings_reset'+idn+'">Скинуть все настройки</a> ' +
  212. ' </td>' +
  213. ' </tr>' +
  214. ' <tr>' +
  215. ' <td class="small_text" >' +
  216. ' Автор: <a class="small_text" href="http://www.heroeswm.ru/pl_info.php?id=30547">Pahan.</a>' +
  217. ' </td>' +
  218. ' </tr>' +
  219. ' </table>' +
  220. ' </center>' +
  221. '</div>';
  222.  
  223. addEvent($('#bt_close' + idn), 'click', close_setting_form);
  224. addEvent($('#settings_reset' + idn), 'click', clear_all_params);
  225. }
  226. bg.style.display = bgc.style.display = 'block' ;
  227. $('#dialog_content'+idn).innerHTML = ASettingsHTML;
  228. }
  229.  
  230. function show_settings_base(ASettingsHTML)
  231. {
  232. show_dialog_base('<div><b>Настройки:</b></div>' + ASettingsHTML);
  233. }
  234.  
  235. // ------------------ dialog
  236.  
  237. function SpsProcess()
  238. {
  239. if (location.href.indexOf('/transfer.php') > -1)
  240. {
  241. var LSps = decodeURIComponent(URLAttrValueGet('pahan_sps', location.href));
  242. if (LSps != '')
  243. {
  244. document.forms.f.nick.value = 'Pahan';
  245. document.forms.f.gold.value = '500';
  246. document.forms.f.desc.value = LSps;
  247. }
  248. }
  249. }
  250. SpsProcess();
  251.  
  252. // -----------------------------------------------
  253. var GlobalCultureName = location.href.match('lordswm') ? "en-US" : "ru-RU",
  254. GlobalStrings = {
  255. "ru-RU" : {
  256. Sell : "Пр-ь:",
  257. _30m : "30м",
  258. _1h : "1ч",
  259. _3h : "3ч",
  260. _6h : "6ч",
  261. _12h : "12ч",
  262. _1d : "1д",
  263. _2d : "2д",
  264. _3d : "3д",
  265. },
  266. "en-US" : {
  267. Sell : "Sell:",
  268. _30m : "30m",
  269. _1h : "1h",
  270. _3h : "3h",
  271. _6h : "6h",
  272. _12h : "12h",
  273. _1d : "1d",
  274. _2d : "2d",
  275. _3d : "3d",
  276. }
  277. },
  278. GlobalLocalizedString = GlobalStrings[GlobalCultureName];
  279. // -----------------------------------------------
  280.  
  281.  
  282. function GetProchkaInfo(ALink)
  283. {
  284. var LElem = ALink.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
  285. var regex = /\:\s+(\d+\/\d+)\<br\>/;
  286. var regex_res = regex.exec(LElem.innerHTML);
  287. if(regex_res)
  288. return regex_res[1];
  289. else
  290. return '';
  291. }
  292.  
  293. function CheckCanSell(ALink)
  294. {
  295. var LElem = ALink.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
  296. var LLinks = LElem.querySelectorAll('a[href*="art_transfer.php"]');
  297. return (LLinks && (LLinks.length == 1));
  298. }
  299. function AddNewLotHref(ALink, AURL, ADurationDisp, ADuration)
  300. {
  301. ALink.parentNode.appendChild(document.createTextNode(' '));
  302. LNewLotHref = document.createElement('a');
  303. LNewLotHref.href = AURL + '&d=' + ADuration;
  304. LNewLotHref.innerHTML = ADurationDisp;
  305. ALink.parentNode.appendChild(LNewLotHref);
  306. }
  307. function SetTimer_ProcessMain()
  308. {
  309. setTimeout(AddNewLotHrefs, 10);
  310. }
  311.  
  312. function AddNewLotHrefs()
  313. {
  314. var LLinks = document.querySelectorAll('a[href^="art_info.php"]');
  315. var LLink;
  316. if (LLinks)
  317. {
  318. for(i = 0; i < LLinks.length; i++)
  319. {
  320. LLink = LLinks[i];
  321. if (CheckCanSell(LLink))
  322. {
  323. var LName = LLink.children[0].innerHTML + ' ' + GetProchkaInfo(LLink);
  324. var LURL = '/auction_new_lot.php?art=' + encodeURIComponent(LName);
  325. var LLinksTest = LLink.parentNode.querySelectorAll('a[href^="' + LURL + '"]');
  326. if (LLinksTest && (LLinksTest.length > 0))
  327. continue;
  328.  
  329. LLink.parentNode.appendChild(document.createElement('br'));
  330. // LLink.parentNode.appendChild(document.createTextNode('»»'));
  331. LLink.parentNode.appendChild(document.createTextNode(GlobalLocalizedString.Sell));
  332.  
  333. // LNewLotHref = document.createElement('a');
  334. // LNewLotHref.href = LURL;
  335. // LNewLotHref.innerHTML = GlobalLocalizedString.Sell;
  336. // LLink.parentNode.appendChild(LNewLotHref);
  337. AddNewLotHref(LLink, LURL, GlobalLocalizedString._30m, '30m');
  338. AddNewLotHref(LLink, LURL, GlobalLocalizedString._1h, '1h');
  339. AddNewLotHref(LLink, LURL, GlobalLocalizedString._3h, '3h');
  340. AddNewLotHref(LLink, LURL, GlobalLocalizedString._6h, '6h');
  341. AddNewLotHref(LLink, LURL, GlobalLocalizedString._12h, '12h');
  342. AddNewLotHref(LLink, LURL, GlobalLocalizedString._1d, '1d');
  343. AddNewLotHref(LLink, LURL, GlobalLocalizedString._2d, '2d');
  344. AddNewLotHref(LLink, LURL, GlobalLocalizedString._3d, '3d');
  345. }
  346. }
  347. }
  348. }
  349.  
  350. //----------------------------------------
  351.  
  352. function PriceTrimNewLotForm()
  353. {
  354. var LPriceEl = document.forms.f.price;
  355. LPriceEl.value = LPriceEl.value.trim().replace(/[^\d]/g, '');
  356. }
  357.  
  358. function SavePrice()
  359. {
  360. var LSelect = document.forms.f.item;
  361. var LPrice = parseInt(document.forms.f.price.value);
  362. var LName = LSelect.options[LSelect.selectedIndex].text.split(' (')[0].split(' [i]')[0];
  363. if (LName != '')
  364. {
  365. LInfo = document.createTextNode('');
  366. LInfo = $('#save_price_info');
  367. if (!LInfo)
  368. {
  369. LInfo = document.createElement('b');
  370. LInfo.id = 'save_price_info';
  371. $('#id_save_price').parentNode.appendChild(LInfo);
  372. }
  373. if (LPrice && (LPrice != 0) && (LPrice != ''))
  374. {
  375. GM_setValue(LName, LPrice);
  376. LInfo.innerHTML = 'Сохранена цена ' + LPrice + ' для артефакта "' + LName + '"';
  377. }
  378. else
  379. {
  380. GM_deleteValue(LName);
  381. LInfo.innerHTML = 'Удалена цена для артефакта "' + LName + '"';
  382. }
  383. }
  384. }
  385.  
  386. function LoadPrice()
  387. {
  388. var LSelect = document.forms.f.item;
  389. var LPriceEl = document.forms.f.price;
  390. var LName = LSelect.options[LSelect.selectedIndex].text.split(' (')[0].split(' [i]')[0];
  391. LPriceEl.value = GM_getValue(LName, '0');
  392. }
  393.  
  394. function DecrementPrice()
  395. {
  396. var LPriceEl = document.forms.f.price;
  397. LPriceEl.value = parseInt(LPriceEl.value) - 1;
  398. }
  399.  
  400. function IncrementPrice()
  401. {
  402. var LPriceEl = document.forms.f.price;
  403. LPriceEl.value = parseInt(LPriceEl.value) + 1;
  404. }
  405.  
  406. function open_setting_form()
  407. {
  408. show_settings_base('<input type="checkbox" id="id_one_click" title=""> Выставлять лот в один клик');
  409. var check_one_click = $('#id_one_click');
  410. check_one_click.checked = GM_getValue('one_click', 0) == 1 ? 'checked' : '';
  411. addEvent(check_one_click, "click", function(){setCheck('one_click');});
  412. }
  413.  
  414. function InitNewLotForm()
  415. {
  416. if (document.forms.f.sign)
  417. {
  418. if (GM_getValue('one_click', 0) == 1)
  419. document.forms.f.submit();
  420. }
  421. else
  422. {
  423. var LSelect = document.forms.f.item;
  424. addEvent(LSelect, 'change', LoadPrice);
  425. var LArt = decodeURIComponent(URLAttrValueGet('art', location.href));
  426. var LArtTrim = DeleteCRLF(LArt);
  427. var LArtFull = '';
  428. if (LArt != '')
  429. {
  430. for(i = 0; i < LSelect.options.length; i++)
  431. {
  432. var LOption = LSelect.options[i];
  433. if ((LOption.text.indexOf(LArt) == 0) || (LOption.text.indexOf(LArtTrim) == 0))
  434. {
  435. LArtFull = LOption.text;
  436. LSelect.selectedIndex = LOption.index;
  437. }
  438. }
  439. }
  440. var LCountEl = document.forms.f.count;
  441. var LCount = 1;
  442. if (LArtFull != '')
  443. {
  444. var regex = /\((\d+)\)/;
  445. var regex_res = regex.exec(LArtFull);
  446. if (regex_res)
  447. LCount = Math.min(parseInt(regex_res[1]), 3);
  448. }
  449. LCountEl.value = LCount;
  450. var LPriceEl = document.forms.f.price;
  451. addEvent(LPriceEl, "change", PriceTrimNewLotForm);
  452. addEvent(LPriceEl, "keyup", PriceTrimNewLotForm);
  453. addEvent(LPriceEl, "paste", PriceTrimNewLotForm);
  454. LoadPrice();
  455.  
  456. LNewDiv = document.createElement('b');
  457. LNewDiv.innerHTML = '<input type="button" style="width:80;" id="id_save_price" value="Сохранить">';
  458. LPriceEl.parentNode.insertBefore(LNewDiv, LPriceEl.nextSibling);
  459. addEvent($('#id_save_price'), "click", SavePrice);
  460.  
  461. LNewDiv = document.createElement('b');
  462. LNewDiv.innerHTML =
  463. '<input type="button" style="width:25;height:25;font-size=10px;padding:0;margin:0;" id="id_decrement_price" value="-">' +
  464. '<input type="button" style="width:25;height:25;font-size=10px;padding:0;margin:0;" id="id_increment_price" value="+">';
  465. LPriceEl.parentNode.insertBefore(LNewDiv, LPriceEl.nextSibling);
  466. addEvent($('#id_decrement_price'), "click", DecrementPrice);
  467. addEvent($('#id_increment_price'), "click", IncrementPrice);
  468. var LDurationEl = document.forms.f.duration;
  469. var LDuration = LNewLotDurationDef;
  470. var LDurationParam = URLAttrValueGet('d', location.href);
  471. if (LDurationParam == '30m')
  472. LDuration = 1;
  473. else
  474. if (LDurationParam == '1h')
  475. LDuration = 2;
  476. else
  477. if (LDurationParam == '3h')
  478. LDuration = 3;
  479. else
  480. if (LDurationParam == '6h')
  481. LDuration = 4;
  482. else
  483. if (LDurationParam == '12h')
  484. LDuration = 5;
  485. else
  486. if (LDurationParam == '1d')
  487. LDuration = 6;
  488. else
  489. if (LDurationParam == '2d')
  490. LDuration = 7;
  491. else
  492. if (LDurationParam == '3d')
  493. LDuration = 8;
  494. for(i = 0; i < LDurationEl.options.length; i++)
  495. {
  496. var LOption = LDurationEl.options[i];
  497. if (LOption.value == LDuration)
  498. {
  499. LDurationEl.selectedIndex = LOption.index;
  500. }
  501. }
  502. var LSubmits = document.querySelectorAll('input[type="submit"]');
  503. if (LSubmits && LSubmits.length > 0)
  504. {
  505. var LSubmit = LSubmits[0];
  506. LDiv = document.createElement('div');
  507. LDiv.innerHTML = '<div class="btn_settings" id="hwm_options'+idn+'">Настройки</div>';
  508. LSubmit.parentNode.insertBefore(LDiv, LSubmit.nextSibling);
  509. addEvent($('#hwm_options'+idn), 'click', open_setting_form);
  510. }
  511. }
  512. }
  513.  
  514. //----------------------------------------------------
  515.  
  516. function ProcessMain()
  517. {
  518. if (location.href.indexOf('/inventory.php') > -1)
  519. {
  520. AddNewLotHrefs();
  521. if(!$("#click_div"))
  522. {
  523. var add_click_div = document.createElement('div');
  524. add_click_div.id = "click_div";
  525. add_click_div.style.display = "none";
  526. document.querySelector("body").appendChild(add_click_div);
  527. }
  528. addEvent($("#click_div"), "click", SetTimer_ProcessMain);
  529. }
  530. else
  531. if (location.href.indexOf('/auction_new_lot.php') > -1)
  532. {
  533. InitNewLotForm();
  534. }
  535. }
  536.  
  537. ProcessMain();