| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
/* ── Copy to clipboard ──────────────────────────────────────── */ |
| 5 |
function setupCopy(coupon) { |
| 6 |
var btn = coupon.querySelector('.bkcp-copy-btn'); |
| 7 |
if (!btn) return; |
| 8 |
|
| 9 |
var code = btn.dataset.copy || ''; |
| 10 |
var copiedLabel = coupon.dataset.copiedLabel || 'Copied!'; |
| 11 |
var origLabel = btn.textContent; |
| 12 |
|
| 13 |
btn.addEventListener('click', function () { |
| 14 |
navigator.clipboard.writeText(code).then(function () { |
| 15 |
btn.textContent = copiedLabel; |
| 16 |
btn.classList.add('copied'); |
| 17 |
setTimeout(function () { |
| 18 |
btn.textContent = origLabel; |
| 19 |
btn.classList.remove('copied'); |
| 20 |
}, 2000); |
| 21 |
}).catch(function () { |
| 22 |
// Fallback |
| 23 |
var ta = document.createElement('textarea'); |
| 24 |
ta.value = code; |
| 25 |
ta.style.position = 'fixed'; |
| 26 |
ta.style.opacity = '0'; |
| 27 |
document.body.appendChild(ta); |
| 28 |
ta.select(); |
| 29 |
try { document.execCommand('copy'); } catch (e) {} |
| 30 |
document.body.removeChild(ta); |
| 31 |
btn.textContent = copiedLabel; |
| 32 |
btn.classList.add('copied'); |
| 33 |
setTimeout(function () { |
| 34 |
btn.textContent = origLabel; |
| 35 |
btn.classList.remove('copied'); |
| 36 |
}, 2000); |
| 37 |
}); |
| 38 |
}); |
| 39 |
} |
| 40 |
|
| 41 |
/* ── Countdown ──────────────────────────────────────────────── */ |
| 42 |
function setupCountdown(countdown) { |
| 43 |
var expiryStr = countdown.dataset.expiry; |
| 44 |
if (!expiryStr) { countdown.style.display = 'none'; return; } |
| 45 |
|
| 46 |
var target = new Date(expiryStr).getTime(); |
| 47 |
if (isNaN(target)) { countdown.style.display = 'none'; return; } |
| 48 |
|
| 49 |
function tick() { |
| 50 |
var now = Date.now(); |
| 51 |
var diff = target - now; |
| 52 |
if (diff <= 0) { |
| 53 |
countdown.textContent = '(expired)'; |
| 54 |
return; |
| 55 |
} |
| 56 |
var d = Math.floor(diff / 86400000); |
| 57 |
var h = Math.floor((diff % 86400000) / 3600000); |
| 58 |
var m = Math.floor((diff % 3600000) / 60000); |
| 59 |
var s = Math.floor((diff % 60000) / 1000); |
| 60 |
|
| 61 |
var parts = []; |
| 62 |
if (d > 0) parts.push(d + 'd'); |
| 63 |
if (h > 0) parts.push(h + 'h'); |
| 64 |
if (m > 0) parts.push(m + 'm'); |
| 65 |
parts.push(s + 's'); |
| 66 |
|
| 67 |
countdown.textContent = '(' + parts.join(' ') + ' left)'; |
| 68 |
} |
| 69 |
|
| 70 |
tick(); |
| 71 |
setInterval(tick, 1000); |
| 72 |
} |
| 73 |
|
| 74 |
function initCoupon(coupon) { |
| 75 |
setupCopy(coupon); |
| 76 |
coupon.querySelectorAll('.bkcp-countdown[data-expiry]').forEach(setupCountdown); |
| 77 |
} |
| 78 |
|
| 79 |
function init() { |
| 80 |
document.querySelectorAll('.bkcp-coupon').forEach(initCoupon); |
| 81 |
} |
| 82 |
|
| 83 |
if (document.readyState === 'loading') { |
| 84 |
document.addEventListener('DOMContentLoaded', init); |
| 85 |
} else { |
| 86 |
init(); |
| 87 |
} |
| 88 |
}()); |
| 89 |
|