(function() { var s = document.currentScript || document.querySelector('script[src*="embed.js"]'); if (!s) return; var autoKey = (window.location.hostname + window.location.pathname).replace(/\/+$/, ''); var key = s.getAttribute('data-key') || autoKey || 'default'; var target = s.getAttribute('data-target'); var mode = s.getAttribute('data-mode') || 'hit'; var base = new URL(s.src, window.location.href).origin; var endpoint = mode === 'read' ? '/count' : '/p'; var url = base + endpoint + '?key=' + encodeURIComponent(key) + '&ref=' + encodeURIComponent(document.referrer || ''); fetch(url, { method: mode === 'read' ? 'GET' : 'POST', mode: 'cors', keepalive: true }) .then(function(r) { return r.json(); }) .then(function(d) { if (target && d && typeof d.hits === 'number') { var el = document.querySelector(target); if (el) el.textContent = d.hits.toLocaleString(); } }) .catch(function() {}); })();