Webotvurci

profigrass.cz - pp - js - v2

Sep 29th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.addEventListener('DOMContentLoaded', function() {
  2.     if (!window.dataLayer || !window.dataLayer[0] || !window.dataLayer[0].shoptet || !window.dataLayer[0].shoptet.product || !window.dataLayer[0].shoptet.product.guid) {
  3.         return;
  4.     }
  5.  
  6.     const productId = window.dataLayer[0].shoptet.product.guid;
  7.     const eshopId = window.dataLayer[0].shoptet.projectId;
  8.     let customButtonsData = null;
  9.  
  10.     fetch(`https://shoptet.webotvurci.cz/eshop-endpoints/${eshopId}/product-variants/setting-hash`)
  11.         .then(response => response.json())
  12.         .then(response => {
  13.             const settingUrl = `https://cdn.myshoptet.com/usr/webotvurci.myshoptet.com/user/documents/product-variants/${eshopId}/eshopSetting.json?${response}`;
  14.             return fetch(settingUrl);
  15.         })
  16.         .then(response => response.json())
  17.         .then(json => {
  18.             customButtonsData = parseCustomButtonsFromNotes();
  19.             const products = json.products[productId];
  20.             if (products) {
  21.                 products.forEach(configId => {
  22.                     const configData = json.configs[configId];
  23.                     renderButtons(configData);
  24.                 });
  25.             }
  26.         })
  27.         .catch(error => console.error('Error:', error));
  28.  
  29.     function renderButtons(configData) {
  30.         const container = document.querySelector('.detail-parameters');
  31.         if (!container) return;
  32.  
  33.         const existingRow = Array.from(container.querySelectorAll('tr')).find(row =>
  34.             row.querySelector('th .row-header-label').textContent.trim().replace(':', '') === configData.title
  35.         );
  36.  
  37.         let row, th, td;
  38.         if (existingRow) {
  39.             row = existingRow;
  40.             th = row.querySelector('th');
  41.             td = row.querySelector('td');
  42.             td.innerHTML = '';
  43.         } else {
  44.             row = document.createElement('tr');
  45.             th = document.createElement('th');
  46.             th.innerHTML = `<span class="row-header-label">${configData.title}<span class="row-header-label-colon">:</span></span>`;
  47.             td = document.createElement('td');
  48.             row.appendChild(th);
  49.             row.appendChild(td);
  50.             container.appendChild(row);
  51.         }
  52.  
  53.         const buttonContainer = document.createElement('div');
  54.         buttonContainer.className = 'variant-buttons';
  55.  
  56.         let buttons = [];
  57.         for (const [key, value] of Object.entries(configData.productsSetting)) {
  58.             const button = document.createElement('button');
  59.             button.className = 'variant-button';
  60.             button.textContent = value.number || value.label;
  61.             button.dataset.link = value.link;
  62.  
  63.             if (key === productId) {
  64.                 button.classList.add('active');
  65.             } else {
  66.                 button.classList.add('clickable');
  67.                 button.addEventListener('click', function() {
  68.                     window.location.href = this.dataset.link;
  69.                 });
  70.             }
  71.  
  72.             buttons.push(button);
  73.         }
  74.  
  75.         if (customButtonsData && customButtonsData[configData.title]) {
  76.             customButtonsData[configData.title].forEach(value => {
  77.                 const button = document.createElement('button');
  78.                 button.className = 'variant-button disable';
  79.                 button.textContent = value;
  80.                 buttons.push(button);
  81.             });
  82.         }
  83.  
  84.         buttons.sort((a, b) => {
  85.             const aValue = parseFloat(a.textContent.replace(/[^\d,]/g, '').replace(',', '.'));
  86.             const bValue = parseFloat(b.textContent.replace(/[^\d,]/g, '').replace(',', '.'));
  87.             return aValue - bValue;
  88.         });
  89.  
  90.         buttons.forEach(button => buttonContainer.appendChild(button));
  91.         td.appendChild(buttonContainer);
  92.     }
  93.  
  94.     function parseCustomButtonsFromNotes() {
  95.         const container = document.querySelector('.extended-description .detail-parameters');
  96.         if (!container) return null;
  97.  
  98.         const customButtonsRow = Array.from(container.querySelectorAll('tr')).find(row => {
  99.             const label = row.querySelector('th .row-header-label');
  100.             return label && label.textContent.trim().toLowerCase().startsWith('wt_custombuttons');
  101.         });
  102.  
  103.         if (!customButtonsRow) return null;
  104.  
  105.         const customButtonsData = customButtonsRow.querySelector('td').textContent.trim();
  106.  
  107.         try {
  108.             const unescapedData = customButtonsData.replace(/&quot;/g, '"');
  109.             const parsedData = JSON.parse(unescapedData);
  110.             customButtonsRow.style.display = 'none';
  111.             return parsedData;
  112.         } catch (e) {
  113.             console.error('Error parsing custom buttons data:', e);
  114.             return null;
  115.         }
  116.     }
  117. });
Advertisement
Add Comment
Please, Sign In to add comment