/* ============================================================ share.js — Lightweight social sharing for listing pages Zero dependencies. Provides WhatsApp, Telegram and copy-link buttons that read their share data from the page's tags. ============================================================ */ (function(){ 'use strict'; /* ── Read share data from the page ─────────────────────────── */ function meta(prop){ var el = document.querySelector('meta[property="'+prop+'"]') || document.querySelector('meta[name="'+prop+'"]'); return el ? el.getAttribute('content') : ''; } var url = meta('og:url') || location.href; var title = meta('og:title') || document.title; var desc = meta('og:description') || meta('description') || ''; var text = title + ' — ' + desc; /* ── Attach handlers ───────────────────────────────────────── */ document.addEventListener('click', function(e){ var btn = e.target.closest('[data-share]'); if(!btn) return; var action = btn.getAttribute('data-share'); if(action === 'whatsapp'){ window.open('https://wa.me/?text=' + encodeURIComponent(text + '\n' + url), '_blank'); } else if(action === 'telegram'){ window.open('https://t.me/share/url?url=' + encodeURIComponent(url) + '&text=' + encodeURIComponent(text), '_blank'); } else if(action === 'copy'){ if(navigator.clipboard && navigator.clipboard.writeText){ navigator.clipboard.writeText(url).then(function(){ _flash(btn, 'Copied!'); }); } else { // Fallback for older browsers var ta = document.createElement('textarea'); ta.value = url; ta.style.position = 'fixed'; ta.style.left = '-9999px'; document.body.appendChild(ta); ta.select(); document.execCommand('copy'); document.body.removeChild(ta); _flash(btn, 'Copied!'); } } else if(action === 'native' && navigator.share){ navigator.share({ title: title, text: desc, url: url }).catch(function(){}); } }); function _flash(btn, msg){ var orig = btn.textContent; btn.textContent = msg; btn.classList.add('share-flash'); setTimeout(function(){ btn.textContent = orig; btn.classList.remove('share-flash'); }, 1800); } })();