GN_ElementPriceLoader

Обновление цен на элементы

当前为 2021-01-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GN_ElementPriceLoader
  3. // @namespace Gradient
  4. // @description Обновление цен на элементы
  5. // @include /^https{0,1}:\/\/(www\.heroeswm\.ru|178\.248\.235\.15)\/.+/
  6. // @exclude /^https{0,1}:\/\/(www\.heroeswm\.ru|178\.248\.235\.15)\/(login|war|cgame|frames|chat|chatonline|ch_box|chat_line|ticker|chatpost|chat2020|battlechat|campaign)\.php.*/
  7. // @version 1.0.9
  8. // ==/UserScript==
  9.  
  10. "use strict";
  11.  
  12. //----------------------------------------------------------------------------//
  13.  
  14. var script_name = 'GN_ElementPriceLoader'; // Enter your script name here
  15.  
  16. //----------------------------------------------------------------------------//
  17.  
  18. (function(){ try{ // wrapper start
  19.  
  20. //----------------------------------------------------------------------------//
  21. // UnifiedLibrary 1.7.0 start
  22. //----------------------------------------------------------------------------//
  23.  
  24. //----------------------------------------------------------------------------//
  25. // SysUtils
  26. //----------------------------------------------------------------------------//
  27.  
  28. var GN_SysUtils = new SysUtils(script_name);
  29. var SU = GN_SysUtils;
  30.  
  31. //----------------------------------------------------------------------------//
  32.  
  33. function SysUtils(name){ // wrapper start
  34.  
  35. //----------------------------------------------------------------------------//
  36.  
  37. this.show_error = function(error_string, use_alert){
  38. if(use_alert)
  39. alert(error_string);
  40.  
  41. throw new Error(error_string);
  42. };
  43.  
  44. if(arguments.length != 1)
  45. this.show_error('Wrong SysUtils arguments');
  46.  
  47. if(!arguments[0])
  48. this.show_error('Empty SysUtils argument');
  49.  
  50. //----------------------------------------------------------------------------//
  51.  
  52. this.save_value = function(desc, value){
  53. var div = document.getElementById('GN_GM_Handler');
  54. div.setAttribute('desc', desc);
  55. div.setAttribute('value', value);
  56. div.setAttribute('operation', 'save');
  57.  
  58. div.click();
  59.  
  60. if(div.getAttribute('state') != 'complete')
  61. this.show_error('Ошибка при сохранении значения');
  62. };
  63.  
  64. //----------------------------------------------------------------------------//
  65.  
  66. this.load_value = function(value, def){
  67. var div = document.getElementById('GN_GM_Handler');
  68. div.setAttribute('desc', value);
  69. div.setAttribute('operation', 'load');
  70.  
  71. div.click();
  72.  
  73. if(div.getAttribute('state') != 'complete')
  74. this.show_error('Ошибка при загрузке значения');
  75.  
  76. return (div.getAttribute('is_null') == 'true' ? def : div.getAttribute('value'));
  77. };
  78.  
  79. //----------------------------------------------------------------------------//
  80.  
  81. var current_id = null;
  82.  
  83. //----------------------------------------------------------------------------//
  84.  
  85. function check_mandatory_scripts(alerter){
  86. var persistent_storage_sign = document.getElementById('GN_GM_Handler');
  87. var common_values_sign = document.getElementById('GN_CommonValuesSign');
  88. var alert_sign = document.getElementById('GN_AlertSign');
  89.  
  90. if(!alert_sign){
  91. alert_sign = document.createElement('div');
  92. alert_sign.id = 'GN_AlertSign';
  93. alert_sign.setAttribute('alerted', 'false');
  94. document.body.appendChild(alert_sign);
  95. }
  96.  
  97. var alerted = alert_sign.getAttribute('alerted') != 'false';
  98.  
  99. if(!persistent_storage_sign){
  100. alert_sign.setAttribute('alerted', 'true');
  101. alerter('Скрипт ' + name + ' требует для своей работы скрипт управления данными (GN_PersistentStorage), который должен стоять первым в порядке выполнения скриптов.\n'
  102. + 'Подробнее здесь: "https://greasyfork.org/ru/scripts/14049-Как-устанавливать-скрипты-читать-здесь"', !alerted);
  103. }
  104.  
  105. if(!common_values_sign){
  106. alert_sign.setAttribute('alerted', 'true');
  107. alerter('Скрипт ' + name + ' требует для своей работы скрипт, хранящий данные (GN_CommonValuesFiller), который должен стоять вторым в порядке выполнения скриптов.\n'
  108. + 'Подробнее здесь: "https://greasyfork.org/ru/scripts/14049-Как-устанавливать-скрипты-читать-здесь"', !alerted);
  109. }
  110. }
  111.  
  112. this.check_login = function(){
  113. var re = /.*?pl_id=(\d+)[^\d]*?/gmi;
  114. var matches = re.exec(document.cookie.toString());
  115.  
  116. if(matches){
  117. current_id = +matches[1];
  118.  
  119. check_mandatory_scripts(this.show_error);
  120. }
  121. };
  122.  
  123. //----------------------------------------------------------------------------//
  124.  
  125. function get_char(e){
  126. if(e.which && e.charCode){
  127. if(e.which < 32)
  128. return null;
  129.  
  130. return String.fromCharCode(+e.which)
  131. }
  132.  
  133. return null;
  134. }
  135.  
  136. this.number_input = function(e){
  137. if(e.ctrlKey || e.altKey || e.metaKey)
  138. return false;
  139.  
  140. var chr = get_char(e);
  141.  
  142. return chr == null || (chr >= '0' && chr <= '9');
  143. };
  144.  
  145. //----------------------------------------------------------------------------//
  146.  
  147. this.reload_page = function(){
  148. document.location.href = document.location.href;
  149. };
  150.  
  151. //----------------------------------------------------------------------------//
  152.  
  153. this.check_login();
  154.  
  155. //----------------------------------------------------------------------------//
  156.  
  157. } // wrapper end
  158.  
  159. //----------------------------------------------------------------------------//
  160. // CommonValues
  161. //----------------------------------------------------------------------------//
  162.  
  163. var GN_CommonValues = new CommonValues();
  164.  
  165. //----------------------------------------------------------------------------//
  166.  
  167. function CommonValues(){ // wrapper start
  168.  
  169. //----------------------------------------------------------------------------//
  170. // Elements
  171. //----------------------------------------------------------------------------//
  172.  
  173. this.elements = JSON.parse(SU.load_value('GN_CommonValues_Elements', '[]'));
  174.  
  175. //----------------------------------------------------------------------------//
  176.  
  177. } // wrapper end
  178.  
  179. //----------------------------------------------------------------------------//
  180. // GUIController
  181. //----------------------------------------------------------------------------//
  182.  
  183. var GN_GUIController = new GUIController();
  184.  
  185. //----------------------------------------------------------------------------//
  186.  
  187. function GUIController(){ // wrapper start
  188.  
  189. //----------------------------------------------------------------------------//
  190.  
  191. clear_flash_z_index();
  192.  
  193. //----------------------------------------------------------------------------//
  194.  
  195. var script_name = 'GN_GUIController';
  196. this.script_name = function(){
  197. return script_name;
  198. };
  199.  
  200. //----------------------------------------------------------------------------//
  201.  
  202. this.registerObject = function(object){
  203. root_div = document.getElementById(root.div.id);
  204.  
  205. if(!root_div)
  206. root_div = create_node(root, document.body);
  207. else{
  208. var custom = root_div.getAttribute('custom').split('|');
  209. root.div.top = +custom[0];
  210. root.div.left = +custom[1];
  211. root.div.width = +custom[2];
  212. root.div.height = +custom[3];
  213. }
  214.  
  215. object.div.left = root.div.left + left;
  216. object.div.top = top;
  217.  
  218. var childs = root_div.childNodes;
  219.  
  220. for(var i = 0; i < childs.length; ++i)
  221. if(childs[i].nodeName.toLowerCase() == 'div'){
  222. var height = +childs[i].getAttribute('custom').split('|')[3];
  223. object.div.top += height;
  224. }
  225.  
  226. create_node(object, root_div);
  227. align_childs(root_div);
  228. collapse_childs(root_div);
  229. };
  230.  
  231. //----------------------------------------------------------------------------//
  232.  
  233. this.hide_all = function(){
  234. if(!root_div)
  235. return;
  236.  
  237. var childs = root_div.childNodes;
  238. for(var i = 0; i < childs.length; ++i)
  239. if(childs[i].nodeName.toLowerCase() == 'div')
  240. childs[i].style.top = +childs[i].getAttribute('custom').split('|')[0];
  241.  
  242. align_childs(root_div);
  243. collapse_childs(root_div);
  244. };
  245.  
  246. var hide_all = this.hide_all;
  247.  
  248. //----------------------------------------------------------------------------//
  249.  
  250. const left = 10;
  251. const top = 10;
  252.  
  253. var root = {
  254. div: {
  255. id: script_name + 'MainDiv',
  256. top: top,
  257. left: left,
  258. width: 0,
  259. height: 0
  260. },
  261.  
  262. input: {
  263. id: script_name + 'MainInput',
  264. value: 'Скрипты',
  265. title: 'Конфигурация и запуск скриптов, не относящихся к определенной странице'
  266. },
  267.  
  268. child_divs: []
  269. };
  270.  
  271. var root_div = document.getElementById(root.div.id);
  272.  
  273. //----------------------------------------------------------------------------//
  274.  
  275. function create_node(object, parent){
  276. var div_ = div(object.div);
  277. div_.setAttribute('expanded', 'false');
  278. parent.appendChild(div_);
  279.  
  280. set_div_style(object.div);
  281.  
  282. var input_ = input(object.input);
  283. div_.appendChild(input_);
  284.  
  285. set_input_style(object.input);
  286.  
  287. object.div.left += div_.clientWidth;
  288. object.div.width = div_.clientWidth;
  289. object.div.height = div_.clientHeight;
  290.  
  291. var custom = [ object.div.top, object.div.left, object.div.width, object.div.height ];
  292. div_.setAttribute('custom', custom.join('|'));
  293.  
  294. if(object.child_divs.length || object.div.id == root.div.id){
  295. input_.addEventListener('click', function(){
  296. expand_childs(div_);
  297. });
  298.  
  299. create_child_nodes(object, div_);
  300. }
  301.  
  302. return div_;
  303. }
  304.  
  305. //----------------------------------------------------------------------------//
  306.  
  307. function create_child_nodes(object, parent){
  308. var childs = object.child_divs;
  309.  
  310. for(var i = 0; i < childs.length; ++i){
  311. var child = childs[i];
  312. child.div.top = top;
  313. child.div.left = left;
  314.  
  315. if(i){
  316. var total_height = 0;
  317.  
  318. for(var j = 0; j < i; ++j){
  319. var sibling = childs[j];
  320. var sibling_div = document.getElementById(sibling.div.id);
  321.  
  322. total_height += sibling_div.clientHeight;
  323. }
  324.  
  325. child.div.top += total_height;
  326. }
  327.  
  328. child.div.left += object.div.left;
  329.  
  330. create_node(child, parent);
  331. }
  332. }
  333.  
  334. //----------------------------------------------------------------------------//
  335.  
  336. function expand_childs(el){
  337. var now_expanded = (el.getAttribute('expanded') == 'true');
  338.  
  339. if(now_expanded && el == root_div){
  340. hide_all();
  341. return;
  342. }
  343.  
  344. var childs = el.childNodes;
  345.  
  346. for(var i = 0; i < childs.length; ++i)
  347. if(childs[i].nodeName.toLowerCase() == 'div')
  348. childs[i].style.display = !now_expanded ? 'block' : 'none';
  349.  
  350. if(now_expanded){
  351. collapse_childs(el);
  352.  
  353. if(el.parentNode == root_div){
  354. childs = root_div.childNodes;
  355.  
  356. for(i = 0; i < childs.length; ++i)
  357. if(childs[i].nodeName.toLowerCase() == 'div' && childs[i] != el)
  358. childs[i].style.display = 'block';
  359.  
  360. el.style.top = +el.getAttribute('custom').split('|')[0];
  361. el.style.width = +el.getAttribute('custom').split('|')[2];
  362.  
  363. align_childs(root_div);
  364. }
  365. }
  366.  
  367. if(!now_expanded && el.parentNode == root_div){
  368. childs = root_div.childNodes;
  369.  
  370. for(i = 0; i < childs.length; ++i){
  371. if(childs[i].nodeName.toLowerCase() == 'div' && childs[i] != el)
  372. childs[i].style.display = 'none';
  373. }
  374.  
  375. el.style.top = top;
  376. el.style.width = +el.getAttribute('custom').split('|')[2];
  377. }
  378.  
  379. el.setAttribute('expanded', now_expanded ? 'false' : 'true');
  380. }
  381.  
  382. //----------------------------------------------------------------------------//
  383.  
  384. function align_childs(el){
  385. var max_width = 0;
  386. var childs = el.childNodes;
  387.  
  388. for(var i = 0; i < childs.length; ++i)
  389. if(childs[i].nodeName.toLowerCase() == 'div'){
  390. var width = +childs[i].getAttribute('custom').split('|')[2];
  391.  
  392. if(width >= max_width)
  393. max_width = width;
  394. }
  395.  
  396. for(i = 0; i < childs.length; ++i)
  397. if(childs[i].nodeName.toLowerCase() == 'div')
  398. childs[i].style.width = max_width;
  399. }
  400.  
  401. //----------------------------------------------------------------------------//
  402.  
  403. function collapse_childs(el){
  404. var divs = el.querySelectorAll('div');
  405.  
  406. for(var i = 0; i < divs.length; ++i){
  407. divs[i].setAttribute('expanded', 'false');
  408. divs[i].style.display = 'none';
  409. }
  410.  
  411. el.setAttribute('expanded', 'false');
  412. }
  413.  
  414. //----------------------------------------------------------------------------//
  415.  
  416. function div(object){
  417. var div = document.createElement('div');
  418. div.id = object.id;
  419.  
  420. return div;
  421. }
  422.  
  423. //----------------------------------------------------------------------------//
  424.  
  425. function set_div_style(object){
  426. var div = document.getElementById(object.id);
  427. var style = div.style;
  428.  
  429. style.position = 'fixed';
  430. style.top = object.top + 'px';
  431. style.left = object.left + 'px';
  432. style.zIndex = 100;
  433. }
  434.  
  435. //----------------------------------------------------------------------------//
  436.  
  437. function input(object){
  438. var input = document.createElement('input');
  439. input.type = 'button';
  440. input.id = object.id;
  441. input.value = object.value;
  442. input.title = object.title;
  443.  
  444. return input;
  445. }
  446.  
  447. //----------------------------------------------------------------------------//
  448.  
  449. function set_input_style(object){
  450. var input = document.getElementById(object.id);
  451. var style = input.style;
  452.  
  453. style.display = 'block';
  454. style.width = '95%';
  455. style.border = '1px solid rgb(153, 153, 153)';
  456. style.padding = '1px';
  457. style.margin = '2px';
  458. style.background = 'none repeat scroll 0% 0% rgb(204, 204, 204)';
  459. style.fontSize = '12px';
  460. style.cursor = 'pointer';
  461. style.zIndex = 100;
  462. }
  463.  
  464. //----------------------------------------------------------------------------//
  465.  
  466. function clear_flash_z_index(){
  467. var objects = document.querySelectorAll('object');
  468.  
  469. for(var i = 0; i < objects.length; ++i){
  470. var o = objects[i];
  471.  
  472. if(!o.querySelector('param[name="wmode"]')){
  473. var param = document.createElement('param');
  474. param.setAttribute('name', 'wmode');
  475. param.setAttribute('value', 'opaque');
  476.  
  477. o.insertBefore(param, o.firstChild);
  478. }
  479. }
  480. }
  481.  
  482. //----------------------------------------------------------------------------//
  483.  
  484. } // wrapper end
  485.  
  486. //----------------------------------------------------------------------------//
  487. // UnifiedLibrary end
  488. //----------------------------------------------------------------------------//
  489.  
  490. var CV = GN_CommonValues;
  491. var elements = CV.elements;
  492.  
  493. var save_value = SU.save_value;
  494. var load_value = SU.load_value;
  495. var show_error = SU.show_error;
  496. var number_input = SU.number_input;
  497. var reload_page = SU.reload_page;
  498.  
  499. var GC = GN_GUIController;
  500. GC.registerObject(
  501. {
  502. div: { id: GC.script_name() + '_' + script_name + 'Div' },
  503.  
  504. input: {
  505. id: GC.script_name() + '_' + script_name + 'Input',
  506. value: 'Обновление цен элементов',
  507. title: 'Обновление цен элементов'
  508. },
  509.  
  510. child_divs: []
  511. }
  512. );
  513.  
  514. var start_button = document.getElementById(GC.script_name() + '_' + script_name + 'Input');
  515.  
  516. start_button.addEventListener('click', function(e){
  517. draw_div(document.body);
  518. });
  519.  
  520. //----------------------------------------------------------------------------//
  521.  
  522. function draw_div(parent){
  523. var div = document.createElement('div');
  524. div.id = script_name + 'Div';
  525. div.style.position = 'fixed';
  526. div.style.display = 'block';
  527. div.style.top = start_button.parentNode.style.top;
  528. div.style.zIndex = 100;
  529.  
  530. var left = /(\d+)px/.exec(start_button.parentNode.style.left);
  531. div.style.left = +left[1] + start_button.parentNode.clientWidth + 'px';
  532. div.style.width = '300px';
  533. div.style.background = start_button.style.backgroundColor;
  534.  
  535. parent.appendChild(div);
  536.  
  537. draw_content(div);
  538. }
  539.  
  540. //----------------------------------------------------------------------------//
  541.  
  542. function draw_content(parent){
  543. var table = document.createElement('table');
  544. table.style.width = '100%';
  545. table.style.border = 'medium none';
  546.  
  547. parent.appendChild(table);
  548.  
  549. draw_header(table);
  550.  
  551. elements.forEach(function(current){
  552. draw_row(table, current);
  553. });
  554.  
  555. draw_buttons(table);
  556. }
  557.  
  558. //----------------------------------------------------------------------------//
  559.  
  560. function draw_header(parent){
  561. var upd_date = JSON.parse(load_value(script_name + '_LastUpdate', '0'));
  562. if(upd_date)
  563. upd_date = new Date(Date.parse(upd_date));
  564.  
  565. var tr = document.createElement('tr');
  566. parent.appendChild(tr);
  567.  
  568. var td = document.createElement('td');
  569. td.setAttribute('colspan', '4');
  570. td.id = script_name + '_LastUpdate';
  571. td.textContent = 'Дата последнего обновления: ' + (upd_date ? upd_date.toLocaleString() : 'еще не считывалось');
  572. tr.appendChild(td);
  573.  
  574. tr = document.createElement('tr');
  575. tr.setAttribute('bgColor', '#DCDCDC');
  576. parent.appendChild(tr);
  577.  
  578. ['Наименование', 'Текущее значение', 'Новое значение', 'Разница' ].forEach(function(current){
  579. td = document.createElement('td');
  580. td.textContent = current;
  581. tr.appendChild(td);
  582. });
  583. }
  584.  
  585. //----------------------------------------------------------------------------//
  586.  
  587. function draw_row(parent, object_){
  588. var tr = document.createElement('tr');
  589. tr.id = object_.id;
  590. parent.appendChild(tr);
  591.  
  592. var td = document.createElement('td');
  593. td.textContent = object_.name + ':';
  594. tr.appendChild(td);
  595.  
  596. td = document.createElement('td');
  597. td.textContent = object_.average_price;
  598. tr.appendChild(td);
  599.  
  600. td = document.createElement('td');
  601. var input = document.createElement('input');
  602. input.type = 'text';
  603. input.style.width = 40;
  604. input.value = object_.average_price;
  605. input.onkeypress = number_input;
  606. input.addEventListener('input', function(e){
  607. show_difference(input);
  608. });
  609. td.appendChild(input);
  610. tr.appendChild(td);
  611.  
  612. td = document.createElement('td');
  613. var font = document.createElement('font');
  614. font.color = 'black';
  615. font.textContent = 0;
  616. td.appendChild(font);
  617. tr.appendChild(td);
  618. }
  619.  
  620. //----------------------------------------------------------------------------//
  621.  
  622. function draw_buttons(parent){
  623. var tr = document.createElement('tr');
  624. parent.appendChild(tr);
  625.  
  626. var td = document.createElement('td');
  627. td.setAttribute('align', 'right');
  628. td.setAttribute('colspan', '4');
  629. tr.appendChild(td);
  630.  
  631. var button = document.createElement('input');
  632. button.type = 'button';
  633. button.value = 'Применить';
  634. button.addEventListener('click', function(e){
  635. save_prices();
  636. });
  637. td.appendChild(button);
  638.  
  639. button = document.createElement('input');
  640. button.type = 'button';
  641. button.value = 'Обновить';
  642. button.addEventListener('click', function(e){
  643. load_prices();
  644. });
  645. td.appendChild(button);
  646.  
  647. button = document.createElement('input');
  648. button.type = 'button';
  649. button.value = 'Отмена';
  650. button.addEventListener('click', function(e){
  651. remove_div();
  652. });
  653. td.appendChild(button);
  654. }
  655.  
  656. //----------------------------------------------------------------------------//
  657.  
  658. function save_prices(){
  659. var errors = [];
  660.  
  661. var div = document.getElementById(script_name + 'Div');
  662. var trs = div.querySelectorAll('table > tr[id]');
  663.  
  664. for(var i = 0; i < trs.length; ++i){
  665. var input = trs[i].querySelector('input');
  666. if(isNaN(+input.value) || +input.value <= 0){
  667. errors.push('Цена элемента должна быть положительной');
  668. break;
  669. }
  670. }
  671.  
  672. if(errors.length){
  673. alert('Ошибки при сохранении:\n\n' + errors.join('\n'));
  674. return;
  675. }
  676.  
  677. var el_prices = [];
  678.  
  679. for(var i = 0; i < trs.length; ++i){
  680. var input = trs[i].querySelector('input');
  681. el_prices.push({ id: trs[i].id, price: input.value });
  682. }
  683.  
  684. save_value('GN_CommonValues_ElementPrices', JSON.stringify(el_prices));
  685.  
  686. reload_page();
  687. }
  688.  
  689. //----------------------------------------------------------------------------//
  690.  
  691. function load_prices(){
  692. document.body.style.cursor = 'wait';
  693.  
  694. var xhr = new XMLHttpRequest();
  695. xhr.open('GET', '/auction.php?cat=elements&sort=0', true);
  696. xhr.overrideMimeType('text/plain; charset=windows-1251');
  697. xhr.onreadystatechange = function(){
  698. if(xhr.readyState == 4)
  699. if(xhr.status == 200){
  700. var objects = parse_page(xhr.response);
  701. objects = filter_objects(objects);
  702.  
  703. var div = document.getElementById(script_name + 'Div');
  704. objects.forEach(function(current){
  705. var tr = div.querySelector('table > tr[id="' + current.id + '"]');
  706.  
  707. if(tr){
  708. var input = tr.querySelector('input');
  709. input.value = Math.floor(current.sum/current.count);
  710. show_difference(input);
  711. }
  712. });
  713.  
  714. var upd_date = new Date();
  715. var td = document.getElementById(script_name + '_LastUpdate');
  716. td.textContent = 'Дата последнего обновления: ' + upd_date.toLocaleString();
  717.  
  718. save_value(script_name + '_LastUpdate', JSON.stringify(upd_date));
  719.  
  720. document.body.style.cursor = 'default';
  721. }
  722. };
  723.  
  724. xhr.send(null);
  725. }
  726.  
  727. //----------------------------------------------------------------------------//
  728.  
  729. function parse_page(response){
  730. var all_objects = [];
  731.  
  732. response = response.replace(/(\n(\r)?)/g, ' ');
  733.  
  734. var re = /.*?(<tr bgcolor='#(?:ffffff|eeeeee)' class=wb>.+?gold.png.+?show_js_button.+?<\/tr>).*?/gmi;
  735. var matches = [];
  736.  
  737. while(matches = re.exec(response))
  738. {
  739. var el_re = /.+\/i\/(.+?)\.(gif|png)'.+?gold.png.+?<td>([0-9,]+?)<\/td>.+?onclick=.+?/i;
  740. var el_matches = el_re.exec(matches[1]);
  741.  
  742. if(!el_matches)
  743. show_error('Ошибка при парсинге ' + matches[1]);
  744.  
  745. var object = {
  746. id: el_matches[1],
  747. price: +(el_matches[3].replace(/,/g, '')),
  748. count: 1
  749. };
  750.  
  751. var count_re = /.*<b>(\d+?) шт.<\/b>/i;
  752. var c_matches = count_re.exec(matches[1]);
  753.  
  754. if(c_matches)
  755. object.count = +c_matches[1];
  756.  
  757. all_objects.push(object);
  758. }
  759.  
  760. return all_objects;
  761. }
  762.  
  763. //----------------------------------------------------------------------------//
  764.  
  765. function filter_objects(objects){
  766. var el_objects = [];
  767. var step = 1.05;
  768. var max_count = 45;
  769.  
  770. objects.forEach(function(current){
  771. var el = get_object(current.id, el_objects);
  772.  
  773. if(!el)
  774. el_objects.push({ id: current.id, max_price: current.price, sum: current.price*current.count, count: current.count });
  775. else{
  776. if(current.price >= el.max_price && current.price <= el.max_price*step)
  777. el.max_price = current.price;
  778.  
  779. if(current.price <= el.max_price*step && el.count < max_count){
  780. el.count += current.count;
  781. el.sum += current.price*current.count;
  782. }
  783. }
  784. });
  785.  
  786. return el_objects;
  787. }
  788.  
  789. //----------------------------------------------------------------------------//
  790.  
  791. function show_difference(input){
  792. var diff = +input.parentNode.previousSibling.textContent - +input.value;
  793.  
  794. var font = input.parentNode.nextSibling.firstChild;
  795. font.color = diff === 0 ? 'black' : diff > 0 ? 'green' : 'red';
  796. font.textContent = diff + '';
  797. }
  798.  
  799. //----------------------------------------------------------------------------//
  800.  
  801. function get_object(id, array_){
  802. for(var i = 0; i < array_.length; ++i)
  803. if(array_[i].id == id)
  804. return array_[i];
  805.  
  806. return null;
  807. }
  808.  
  809. //----------------------------------------------------------------------------//
  810.  
  811. function remove_div(){
  812. var div = document.getElementById(script_name + 'Div');
  813. div.parentNode.removeChild(div);
  814. }
  815.  
  816. //----------------------------------------------------------------------------//
  817.  
  818. } catch(e){
  819. alert('Ошибка в скрипте ' + script_name + ', обратитесь к разработчику:\n' + e);
  820. throw e;
  821. }}()); // wrapper end
  822.  
  823. //----------------------------------------------------------------------------//