(() => { 'use strict'; const categoryPath = '/eroticka-pericka-simratka/'; const stockUrl = 'https://schema.org/InStock'; const blackId = '2673'; const blackSku = '2673/CER2'; // Shoptet product IDs, in display priority. Reserved Bijoux (2182 / SKU 7200) is excluded. const slots = [ { id: 'pericka-jemne', products: [blackId, '2512', '2664', '333', '2507', '342', '2667', '336', '2670', '339'] }, { id: 'pericka-placacka', products: ['2679', '2682', '2628', '2631', '2923'] } ]; const block = document.getElementById('vyber-pericka'); const onFeathers = block && location.pathname.startsWith(categoryPath) && slots.every(slot => block.querySelector('#' + slot.id)); const entries = { 'navlek-uvod-fantasy': '/navleky-na-penis/navlek-na-penis-33-obvodu/', 'navlek-uvod-vibrace': '/navleky-na-penis/vibracni-navlek-na-penis-18-cm/', 'oralni-sex-satisfyer': '/womanizery/satisfyer-love-triangel-tlakovy-vibrator/', 'hry-dalsi-napady': '/eroticke-polohy-a-hry/5-netradicnich-erotickych-her/' }; const links = Object.entries(entries).flatMap(([id, path]) => { const link = document.getElementById(id); return link instanceof HTMLAnchorElement && link.origin === location.origin && link.pathname === path ? [link] : []; }); if (!links.length && !onFeathers) return; let consentOverride; const shoptet = window.shoptet; if (typeof shoptet?.consent?.onAccept === 'function') { shoptet.consent.onAccept(values => { consentOverride = values.includes(shoptet.config.cookiesConsentOptAnalytics); }); } function hasAnalyticsConsent() { if (consentOverride !== undefined) return consentOverride; let granted = false; for (const entry of window.dataLayer || []) { if (entry?.cookie_consent) granted = entry.cookie_consent.analytics === 'granted'; if (entry?.[0] === 'consent' && entry[2]?.analytics_storage) { granted = entry[2].analytics_storage === 'granted'; } } return granted; } function track(event) { if (!event.isTrusted || (event.type === 'auxclick' && event.button !== 1)) return; if (!hasAnalyticsConsent() || typeof window.gtag !== 'function') return; const link = event.currentTarget; const data = { source_path: location.pathname, target_path: link.pathname, link_position: link.id, send_to: 'G-T3SG380G23' }; if (document.body.classList.contains('admin-logged')) { data.traffic_type = 'internal'; data.debug_mode = true; console.info('[SV-VSTUP]', 'landing_link_click', JSON.stringify(data)); } window.gtag('event', 'landing_link_click', data); } for (const link of links) { link.addEventListener('click', track); link.addEventListener('auxclick', track); } if (!onFeathers) return; const controllerTimeout = 8000; let revision = 0; let lastCheck = 0; async function publicDocument(url) { const response = await fetch(url, { cache: 'no-cache', signal: AbortSignal.timeout(controllerTimeout) }); if (!response.ok || new URL(response.url).origin !== location.origin) throw Error('Storefront unavailable'); return new DOMParser().parseFromString(await response.text(), 'text/html'); } function fromCard(card) { const offer = card.querySelector('[data-micro="offer"]'); const availability = card.querySelector('.availability')?.textContent.trim().replace(/\s+/g, ' '); const stocked = offer?.dataset.microAvailability === stockUrl && /^skladem a na prodejně$/i.test(availability); if (!stocked && !/^obdržíte do 3 pracovních dnů$/i.test(availability)) return null; const url = new URL(card.querySelector('a.name').getAttribute('href'), location.origin); if (url.origin !== location.origin || !url.pathname.startsWith(categoryPath)) return null; const image = card.querySelector('a.image img'); const picture = (image?.dataset.src || image?.dataset.microImage || image?.getAttribute('src') || '').trim(); const price = card.querySelector('.price-final')?.textContent.trim(); const name = card.querySelector('[data-micro="name"]')?.textContent.trim(); if (!name || !price || !picture || !picture.startsWith('https://')) return null; return { url, picture, name, price, availability, stocked }; } async function blackVariant(card) { const item = fromCard(card); if (!item) return null; item.url.searchParams.set('variantId', '3357'); const detail = await publicDocument(item.url); const form = detail.querySelector('#product-detail-form'); const offer = form?.querySelector('[itemprop="sku"][content="' + blackSku + '"]')?.closest('[itemprop="offers"]'); const option = form?.querySelector('option[value="3357"][selected]'); if (!offer || !item.stocked || !/skladem a na prodejně/i.test(option?.textContent || '') || offer.querySelector('[itemprop="availability"]')?.getAttribute('href') !== stockUrl || !option || option.disabled || option.dataset.disableButton === '1' || !(Number(option.dataset.stock) > 0) || !option.textContent.includes('černá')) return null; const price = offer.querySelector('[itemprop="price"]')?.getAttribute('content'); const currency = offer.querySelector('[itemprop="priceCurrency"]')?.getAttribute('content'); if (!price || !currency || !Number.isFinite(Number(price))) return null; item.name = 'Černé peříčko Darkness 38 cm'; item.price = new Intl.NumberFormat('cs-CZ', {style: 'currency', currency, maximumFractionDigits: 2, minimumFractionDigits: 0}).format(Number(price)); return item; } async function firstAvailable(slot, cards) { let delivery; for (const id of slot.products) { const card = cards.get(id); if (!card) continue; const item = id === blackId ? await blackVariant(card).catch(() => null) : fromCard(card); if (item?.stocked) return item; if (item && !delivery) delivery = item; } return delivery || null; } function paint(link, item) { link.style.display = item ? 'flex' : 'none'; if (!item) { link.removeAttribute('href'); return; } link.href = item.url.href; link.querySelector('img').src = item.picture; link.querySelector('strong').textContent = item.name; const caption = link.querySelector('span'); const price = caption.querySelector('[data-sv-offer]') || document.createElement('span'); price.dataset.svOffer = ''; price.style.cssText = 'display:block;font-size:13px;color:#332b2e'; price.textContent = item.price + ' · ' + item.availability; caption.append(price); } async function refresh() { const current = ++revision; lastCheck = Date.now(); block.style.display = 'none'; try { const cards = new Map(); const visited = new Set(); let url = new URL(categoryPath, location.origin); while (url && !visited.has(url.href)) { visited.add(url.href); const page = await publicDocument(url); for (const card of page.querySelectorAll('#products [data-micro-product-id]')) { cards.set(card.dataset.microProductId, card); } const next = page.querySelector('link[rel="next"], .pagination a[rel="next"]')?.getAttribute('href'); url = next ? new URL(next, url) : null; if (url && (url.origin !== location.origin || !url.pathname.startsWith(categoryPath))) break; } const choices = await Promise.all(slots.map(slot => firstAvailable(slot, cards))); if (current !== revision) return; slots.forEach((slot, i) => paint(block.querySelector('#' + slot.id), choices[i])); if (choices.some(Boolean)) block.style.display = ''; } catch { // The regular product listing stays available when stock cannot be verified. } } for (const slot of slots) { const link = block.querySelector('#' + slot.id); link.addEventListener('click', track); link.addEventListener('auxclick', track); } refresh(); window.addEventListener('pageshow', event => { if (event.persisted) refresh(); }); document.addEventListener('visibilitychange', () => { if (!document.hidden && Date.now() - lastCheck > 60000) refresh(); }); })();