NikolayBezay

Aprile get products information from category page

May 28th, 2021 (edited)
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.     function updateSlideContent(sourceSlide, index) {
  3.         var productLink = sourceSlide.querySelector('a');
  4.         var productImage = sourceSlide.querySelector('.image img');
  5.         var productTitle = sourceSlide.querySelector('.description');
  6.         var productOldPrice = sourceSlide.querySelector('.oldprice');
  7.         var productPrice = sourceSlide.querySelector('.saleprice');
  8.  
  9.         if (!productLink) {
  10.             console.log('index - ', index);
  11.             console.log('productLink not exit');
  12.         }
  13.         if (!productImage) {
  14.             console.log('index - ', index);
  15.             console.log('productImage not exit');
  16.         }
  17.         if(!productTitle) {
  18.             console.log('index - ', index);
  19.             console.log('productTitle not exit');
  20.         }
  21.         if(!productOldPrice) {
  22.             console.log('index - ', index);
  23.             console.log('productOldPrice not exit');
  24.         }
  25.         if (!productPrice) {
  26.             console.log('index - ', index);
  27.             console.log('productPrice not exit');
  28.         }
  29.         var productData = {
  30.             productLink: productLink.href,
  31.             productImage: window.location.origin + '/' + productImage.getAttribute('src'),
  32.             productTitle: productTitle.textContent,
  33.             productOldPrice: productOldPrice.textContent,
  34.             productPrice: productPrice.textContent
  35.         };
  36.         products.push(productData);
  37.     }
  38.  
  39.     var products = [];
  40.     var pageProducts = document.querySelectorAll('.item');
  41.     for(var i = 0; i < pageProducts.length; i++) {
  42.         updateSlideContent(pageProducts[i], i);
  43.     }
  44.     products.forEach(element => {
  45.         console.log(JSON.stringify(element));    
  46.     });
  47.    
  48. })();
  49.  
Add Comment
Please, Sign In to add comment