document.addEventListener('DOMContentLoaded', function () {
var lyricsContainer = document.querySelector('.entry-content.single-content');
var btnCopy = document.getElementById('copy-lyrics-btn');
var statusEl = document.getElementById('copy-status-msg');
if (!lyricsContainer) return;
if (!statusEl) {
statusEl = document.createElement('div');
statusEl.id = 'copy-status-msg';
statusEl.style.marginTop = '0.5rem';
statusEl.style.fontSize = '0.85rem';
statusEl.style.color = '#666';
lyricsContainer.parentNode.insertBefore(statusEl, lyricsContainer.nextSibling);
}
if (btnCopy) {
btnCopy.addEventListener('click', function (e) {
e.preventDefault();
var selection = window.getSelection();
var selectedText = selection ? selection.toString().trim() : '';
if (!selectedText) {
var fullText = lyricsContainer.innerText.trim();
console.log('FULL TEXT SAMPLE:', fullText.slice(0, 200));
// find start: allow both fullwidth and halfwidth colon, or just "作曲"
var startIdx = fullText.indexOf('作曲:');
if (startIdx === -1) {
startIdx = fullText.indexOf('作曲:');
}
if (startIdx === -1) {
startIdx = fullText.indexOf('作曲');
}
// find end: Chinese or English heading
var endIdx = fullText.indexOf('相關歌詞');
if (endIdx === -1) {
endIdx = fullText.indexOf('Related Lyrics');
}
console.log('startIdx:', startIdx, 'endIdx:', endIdx);
if (startIdx !== -1 && endIdx !== -1 && endIdx > startIdx) {
selectedText = fullText.substring(startIdx, endIdx).trim();
} else {
selectedText = fullText;
}
}
if (!selectedText) {
statusEl.textContent = 'No lyrics to copy.';
return;
}
var pageUrl = window.location.href;
var textToCopy = selectedText + '\n\nSource: ' + pageUrl;
navigator.clipboard.writeText(textToCopy).then(function () {
statusEl.textContent = 'Copied lyrics + link. You can paste into IG / Threads / WhatsApp.';
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'lyrics_copy',
copied_text: selectedText,
copied_url: pageUrl
});
window.dataLayer.push({
event: 'lyrics_share',
share_channel: 'copy_link',
share_url: pageUrl
});
}).catch(function (err) {
console.error('Clipboard error:', err);
statusEl.textContent = 'Copy failed. Please use Ctrl+C / Cmd+C.';
});
});
}
var iconButtons = document.querySelectorAll('.lp-lyrics-icon-btn');
iconButtons.forEach(function (btn) {
btn.addEventListener('click', function () {
btn.classList.toggle('is-active');
iconButtons.forEach(function (other) {
if (other !== btn) other.classList.remove('is-active');
});
});
});
document.addEventListener('click', function (e) {
if (!e.target.closest('.lp-lyrics-icon-btn')) {
iconButtons.forEach(function (btn) {
btn.classList.remove('is-active');
});
}
}, true);
});