Advertisement
orenchuck

aldo add tags to products

Feb 14th, 2023 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. <script>
  2. (function(){
  3. var productsCount = 0;
  4. var pageScroll = 0;
  5. var scrollStep = 50;
  6.  
  7. function checkPage() {
  8. var productId = document.querySelector('span[class*="productSku"]');
  9. if (productId) {
  10. getTagsFromProductFeed([window.location.href]);
  11. window.adoric && adoric.trigger('pageview', {
  12. type: 'product',
  13. ids: productId.textContent
  14. });
  15. }
  16. var products = document.querySelector('[class*="item-card"]');
  17. if (products) {
  18. addTagsToCategories();
  19. }
  20. }
  21.  
  22. function addTagsToCategories() {
  23. var pageData = getPageData();
  24. productsCount = pageData.length;
  25. getTagsFromProductFeed(pageData);
  26. }
  27.  
  28. function getPageData() {
  29. var products = document.querySelectorAll('[class*="item-card"]');
  30. if (products[0]) {
  31. var productLinks = [];
  32. products.forEach(function(item) {
  33. var link = item.querySelector('a');
  34. if (link) {
  35. productLinks.push(link.href);
  36. }
  37. });
  38. return productLinks;
  39. }
  40. }
  41.  
  42.  
  43. function getTagsFromProductFeed(pageData) {
  44.  
  45. fetch('https://us-east4-adoric-production.cloudfunctions.net/Support_aldo_add_tags2', {
  46. method: 'POST',
  47. body: JSON.stringify({ products: pageData })
  48. })
  49. .then(function(res) {return res.json()}).then(function(res) {
  50. if (res && !res.message) {
  51. if (window.tagsproductList) {
  52. var newList = window.tagsproductList.concat(res);
  53. window.tagsproductList = newList;
  54. } else {
  55. window.tagsproductList = res;
  56. }
  57. if (window.tagsproductList.length > 0) {
  58. adoric && adoric.trigger('addTags');
  59. }
  60. } else if (res && res.message) {
  61. console.info(res.message);
  62. } else {
  63. console.info('result error', res);
  64. }
  65. }).catch(function(error) {
  66. console.info(error);
  67. });
  68.  
  69. }
  70.  
  71. window.addEventListener('scroll', function() {
  72. if ((window.pageYOffset - pageScroll) > scrollStep) {
  73. pageScroll = window.pageYOffset;
  74. var pageData = getPageData();
  75. if (pageData && pageData.length > productsCount) {
  76. var firstIndex = productsCount;
  77. productsCount = pageData.length;
  78. var newPageData = pageData.splice(firstIndex, pageData.length - 1);
  79. getTagsFromProductFeed(newPageData);
  80. }
  81. }
  82.  
  83. });
  84.  
  85. if (document.readyState === 'loading') {
  86. document.addEventListener('DOMContentLoaded', checkPage);
  87. } else {
  88. checkPage();
  89. }
  90. })();
  91. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement