Virtomomica: IC

Индикативные цены - парсинг и отображение

  1. // ==UserScript==
  2. // @name Virtomomica: IC
  3. // @namespace virtonomica
  4. // @description Индикативные цены - парсинг и отображение
  5. // @include http://virtonomica.ru/*/main/geo/countrydutylist/*
  6. // @include http://virtonomica.ru/*/main/unit/view/*/sale
  7. // @version 0.1
  8. // ==/UserScript==
  9. var run = function() {
  10. //---------------------------------------------------------------------
  11. // работа с локальным хранилищем
  12. //---------------------------------------------------------------------
  13. /**
  14. * записать данные в локальнео хранилище, с проверкой ошибок
  15. */
  16. function ToStorage(name, val)
  17. {
  18. try {
  19. window.localStorage.setItem( name, JSON.stringify( val ) );
  20. } catch(e) {
  21. out = "Ошибка добавления в локальное хранилище";
  22. //console.log(out);
  23. }
  24. }
  25.  
  26. function getFromStorage(obj, id_shop)
  27. {
  28. if (obj[id_shop] == null) return '';
  29. return JSON.stringify(obj[id_shop]);
  30. }
  31. //---------------------------------------------------------------------
  32. // end of работа с локальным хранилищем
  33. //---------------------------------------------------------------------
  34. var wc_info = $("<div id=p_info></div>");
  35.  
  36. // Проверим ссылку что это игровое поле
  37. var href = location.href;
  38. console.log( href );
  39. if (href.indexOf('countrydutylist') > 0) {
  40. console.log('parsing....');
  41. var wc_parsing = $("<li><div id=parsing style='float:left;cursor:pointer; color: white;'> <img title='Запомнить ИЦ' alt='Запомнить ИЦ' src='http://www.iconsearch.ru/uploads/icons/snowish/32x32/document-save-as.png'> </div>");
  42. var container = $('#topblock').next();
  43. container = $("li:last", container).prev().parent();
  44. container.append( wc_parsing ) ;
  45.  
  46. $("table.list").before( wc_info );
  47.  
  48. $("#parsing").click( function() {
  49. ic_array = new Object();
  50.  
  51. table = $("table.list");
  52. //console.log('table = ' + table.length);
  53. tr = $("tr.odd, tr.even", table);
  54. //console.log('tr = ' + tr.length);
  55. for (i=0; i<tr.length; i++){
  56. td = $("td > img", tr.eq(i) );
  57. //console.log('td = ' + td.length);
  58. for(j=0; j< td.length; j++){
  59. src = td.eq(j).attr('src');
  60. //console.log( src );
  61. name = td.eq(j).parent().next().text();
  62. //console.log( name );
  63. ic = td.eq(j).parent().next().next().next().next().text();
  64. ic = parseFloat( ic.replace('$', '').replace(' ', '').replace(' ', '').replace(' ', '') );
  65. //console.log( ic );
  66. ic_array[ src ] = new Object();
  67. ic_array[ src ][ 'name' ] = name;
  68. ic_array[ src ][ 'ic' ] = ic;
  69. }
  70. }
  71.  
  72. //console.log( JSON.stringify( ic_array ) );
  73. ToStorage('ic_array', ic_array );
  74. $("#p_info").html("Запомнили значения ИЦ").css('color', 'green');
  75.  
  76. });
  77. }
  78. console.log('start IC');
  79.  
  80. $("table.grid").before( wc_info );
  81.  
  82. // Все остальные страницы
  83. ic_array = JSON.parse( window.localStorage.getItem('ic_array') );
  84. if ( ic_array == null ) {
  85. ic_array = new Object();
  86. $("#p_info").html("Не удалось получить значения ИЦ из локального хранилища").css('color', 'red');
  87. } else{
  88. //$("#p_info").html( JSON.stringify( ic_array ) );
  89. }
  90.  
  91. // снабжение
  92. if (href.indexOf('sale') > 0) {
  93.  
  94. table = $("table.grid");
  95. tr = $("tr.odd, tr.even", table);
  96. console.log('tr = ' + tr.length);
  97. for (i=0; i<tr.length; i++){
  98. td = $("td > a > img", tr.eq(i) );
  99. if ( td.length == 2) td = td.eq(1);
  100. else td = td.eq(0);
  101.  
  102. //console.log("td= " + td.parent().html() );
  103. src = td.attr('src');
  104. console.log( JSON.stringify( ic_array[src] ) );
  105. if ( ic_array[src] == null ) {
  106. $("#p_info").append("<br>" + td.attr('alt') + " - нет данных по ИЦ").css('color', 'red');
  107. continue;
  108. }
  109. inp = $("input[name^='storageData']", tr.eq(i) );
  110. if (inp.length == 3) inp = inp.eq(1);
  111.  
  112. console.log("inp = " + inp.length);
  113. inp.before( "ИЦ: " + ic_array[src]['ic'] + "$<br>" );
  114. //td.parent().next().next().next().next().next().before( "ИЦ: " + ic_array[src]['ic'] + "$<br>" );
  115. }
  116. }
  117.  
  118. console.log('end IC');
  119. }
  120. if(window.top == window) {
  121. var script = document.createElement("script");
  122. script.textContent = '(' + run.toString() + ')();';
  123. document.documentElement.appendChild(script);
  124. }