Advertisement
Guest User

Untitled

a guest
May 10th, 2022
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Show price combinations list (10.05.2022)
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.7
  5. // @author Dimokin - https://mipped.com/f/members/dimokin.65355/
  6. // @match https://ru.aliexpress.com/item/*
  7. // @match https://www.aliexpress.com/item/*
  8. // @match https://aliexpress.ru/item/*
  9. // @grant none
  10. // @require http://code.jquery.com/jquery-3.3.1.min.js
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14. var $ = window.jQuery;
  15.  
  16. $(document).ready(function() {
  17. let startHTML = document.querySelector("html").outerHTML.split('window.runParams =')[1];
  18. let endHTML = startHTML.split('var GaData')[0];
  19.  
  20. function strToObj(str) {
  21. var obj = {};
  22. if (str && typeof str === 'string') {
  23. var objStr = str.match(/\{(.)+\}/g);
  24. eval("obj =" + objStr);
  25. }
  26.  
  27. return obj
  28. }
  29.  
  30. let mainObject = strToObj(endHTML);
  31.  
  32.  
  33. mainObject.skuModule.skuPriceList.sort(function(a, b) {
  34. let actPriceField = a.skuVal.skuActivityAmount ? 'skuActivityAmount' : 'skuAmount';
  35. let defaultPriceField;
  36.  
  37. if (a.skuVal.skuActivityAmount || a.skuVal.skuAmount) {
  38. defaultPriceField = a.skuVal.skuActivityAmount ? 'skuActivityAmount' : 'skuAmount';
  39. } else {
  40. defaultPriceField = a.skuVal.skuAmount ? 'skuAmount' : 'skuActivityAmount';
  41. }
  42.  
  43.  
  44. var keyA = parseFloat(a.skuVal[actPriceField].value),
  45. keyB = parseFloat(b.skuVal[actPriceField].value),
  46. keyC = parseFloat(a.skuVal[defaultPriceField].value),
  47. keyD = parseFloat(b.skuVal[defaultPriceField].value);
  48.  
  49. if (keyA == keyB) {
  50. return (keyC > keyD) ? -1 : (keyA < keyD) ? 1 : 0;
  51. } else {
  52. return (keyA < keyB) ? -1 : 1;
  53. }
  54. });
  55.  
  56. var div = document.createElement('div');
  57. div.className = 'prices';
  58.  
  59. div.innerHTML += `
  60. <button class="price-toggle">
  61. Show/Hide
  62. </button>
  63. `;
  64.  
  65. $(div).append('<div class="price-list"></div>');
  66.  
  67. let pricesIndexes = [];
  68.  
  69. let newPriceList = [];
  70.  
  71. let hasCountries = false;
  72.  
  73. mainObject.skuModule.productSKUPropertyList.forEach(function(element) {
  74. if (element.skuPropertyId === 200007763) {
  75. hasCountries = true;
  76. }
  77. });
  78.  
  79.  
  80. mainObject.skuModule.skuPriceList.forEach(function(element) {
  81. if (hasCountries) {
  82. if (element.skuPropIds.includes('201336100') || element.skuPropIds.includes('201336103') || element.skuPropIds.includes('203124901')) {
  83. newPriceList.push(element);
  84. }
  85. } else {
  86. newPriceList.push(element);
  87. }
  88.  
  89. });
  90.  
  91.  
  92.  
  93. newPriceList.forEach(function(element) {
  94. let propsId = element.skuPropIds.split(',');
  95. let availableCount = element.skuVal.availQuantity;
  96. let propString = '';
  97. let propIndex = [];
  98.  
  99. if (propsId.length) {
  100. propsId.forEach(function(prop, index) {
  101. if (mainObject.skuModule.productSKUPropertyList && mainObject.skuModule.productSKUPropertyList.length) {
  102. mainObject.skuModule.productSKUPropertyList.forEach(function(listItem) {
  103. listItem.skuPropertyValues.forEach(function(skuProp, skuIndex) {
  104. if (prop == skuProp.propertyValueId && availableCount > 0) {
  105. propString += skuProp.propertyValueDisplayName;
  106. propIndex.push(skuIndex);
  107. }
  108. });
  109. });
  110.  
  111. if (index !== propsId.length - 1) {
  112. propString += ' - '
  113. }
  114. }
  115.  
  116. });
  117. }
  118.  
  119.  
  120. if (propIndex.length) {
  121. pricesIndexes.push(propIndex);
  122. }
  123.  
  124.  
  125. let priceField = element.skuVal.skuActivityAmount ? 'skuActivityAmount' : 'skuAmount';
  126. let price = element.skuVal[priceField].value;
  127.  
  128. var oldHtml = $(div).find('.price-list').html();
  129.  
  130. if (element.skuVal.availQuantity > 0) {
  131. $(div).find('.price-list').html(oldHtml + `
  132. <div class="price-row">
  133. <div class="price-row__left">${propString} </div>
  134. <div class="price-row__right">${price} ${mainObject.commonModule.currencyCode}</div>
  135. </div>
  136. `);
  137. }
  138.  
  139.  
  140. });
  141.  
  142.  
  143. var styles = `
  144. .prices {
  145. position: fixed;
  146. top: 0;
  147. right: 0;
  148. background: white;
  149. box-shadow: 1px -1px 4px 1px;
  150. max-width: 40%;
  151. max-height: 400px;
  152. padding: 10px 20px;
  153. overflow-y: auto;
  154. overflow-x: hidden;
  155. z-index:9999;
  156. }
  157.  
  158. .price-list {
  159. margin-top: 15px;
  160. }
  161.  
  162. .price-row {
  163. display:flex;
  164. padding:3px;
  165. margin-bottom:0;
  166. justify-content: space-between;
  167. cursor:pointer;
  168. }
  169.  
  170. .price-row:hover,.price-row.active {
  171. background: lightgrey;
  172. }
  173.  
  174. .price-row__left {}
  175.  
  176. .price-row__right {
  177. min-width: 90px;
  178. text-align: right;
  179. margin-left: 20px;
  180. width: 10%;
  181. flex-shrink: 0;
  182. }
  183.  
  184. .price-toggle {
  185. display: block;
  186. margin-right: auto;
  187. cursor:pointer;
  188. }`
  189.  
  190. var styleSheet = document.createElement("style")
  191. styleSheet.type = "text/css"
  192. styleSheet.innerText = styles
  193. document.head.appendChild(styleSheet)
  194. document.body.appendChild(div);
  195.  
  196. $(document).on("click", ".price-toggle", function() {
  197. $(div).find('.price-list').toggle();
  198. setTimeout($(window).trigger('resize'), 300);
  199. });
  200.  
  201.  
  202. let propertyLists = $('.product-sku').find('.sku-property-list');
  203.  
  204. if (pricesIndexes.length) {
  205. pricesIndexes[0].forEach(function(price, priceIndex) {
  206. for (var i = 0; i < propertyLists.length; i++) {
  207. if (i === priceIndex) {
  208. let propIndex = pricesIndexes[0][priceIndex];
  209. let prop = $('.sku-property-list').eq(i).find('.sku-property-item').eq(propIndex)
  210.  
  211. if (prop.hasClass("selected")) {} else {
  212. prop.trigger('click');
  213. }
  214. }
  215. }
  216. });
  217. }
  218.  
  219. $('.price-row').eq(0).addClass('active');
  220.  
  221.  
  222. $('.price-row').click(function(event) {
  223. let index = $(this).index();
  224.  
  225. $('.price-row').each(function() {
  226. if ($(this).hasClass("active")) {
  227. $(this).removeClass('active');
  228. }
  229. });
  230.  
  231. $(this).addClass('active');
  232.  
  233.  
  234. let propertyLists = $('.product-sku').find('.sku-property-list');
  235.  
  236. pricesIndexes[index].forEach(function(price, priceIndex) {
  237. for (var i = 0; i < propertyLists.length; i++) {
  238. if (i === priceIndex) {
  239. let propIndex = pricesIndexes[index][priceIndex];
  240. let prop = $('.sku-property-list').eq(i).find('.sku-property-item').eq(propIndex)
  241.  
  242. if (prop.hasClass("selected")) {} else {
  243. prop.trigger('click');
  244. }
  245. }
  246. }
  247. });
  248. });
  249. });
  250. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement