93 lines
3.7 KiB
JavaScript
93 lines
3.7 KiB
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
// --- Helper Functions ---
|
|
function downloadBibtex(bibtexContent, filename) {
|
|
if (!bibtexContent || !filename) {
|
|
console.error('BibTeX content or filename missing for download.');
|
|
alert('Could not prepare BibTeX for download.');
|
|
return;
|
|
}
|
|
try {
|
|
// Parse the JSON string to get the actual BibTeX content
|
|
const parsedBibtexContent = JSON.parse(bibtexContent);
|
|
const blob = new Blob([parsedBibtexContent], { type: 'application/x-bibtex;charset=utf-8' });
|
|
const link = document.createElement('a');
|
|
link.href = URL.createObjectURL(blob);
|
|
link.download = filename;
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
document.body.removeChild(link);
|
|
URL.revokeObjectURL(link.href);
|
|
} catch (e) {
|
|
console.error('Error parsing BibTeX JSON from data attribute for download:', e);
|
|
alert('Could not prepare BibTeX for download. Invalid data format.');
|
|
}
|
|
}
|
|
|
|
function copyToClipboard(bibtexJsonContent, buttonElement) {
|
|
if (!bibtexJsonContent) {
|
|
console.error('No BibTeX JSON content provided to copy.');
|
|
return;
|
|
}
|
|
try {
|
|
const textToCopy = JSON.parse(bibtexJsonContent); // Parse the JSON string
|
|
navigator.clipboard.writeText(textToCopy).then(() => {
|
|
if (buttonElement) {
|
|
const originalText = buttonElement.innerHTML; // Store full HTML content
|
|
buttonElement.innerHTML = '<i class="fas fa-check"></i> Copied!';
|
|
buttonElement.disabled = true;
|
|
setTimeout(() => {
|
|
buttonElement.innerHTML = originalText;
|
|
buttonElement.disabled = false;
|
|
}, 2000);
|
|
}
|
|
}).catch(err => {
|
|
console.error('Could not copy text: ', err);
|
|
alert('Failed to copy BibTeX. Ensure you are on a secure connection (HTTPS) or check browser permissions.');
|
|
// Basic fallback (less likely to be needed for navigator.clipboard modern support)
|
|
try {
|
|
const textArea = document.createElement('textarea');
|
|
textArea.value = textToCopy;
|
|
textArea.style.position = 'fixed'; // Avoid scrolling to bottom
|
|
document.body.appendChild(textArea);
|
|
textArea.focus();
|
|
textArea.select();
|
|
document.execCommand('copy');
|
|
document.body.removeChild(textArea);
|
|
if (buttonElement) {
|
|
const originalText = buttonElement.innerHTML;
|
|
buttonElement.innerHTML = '<i class="fas fa-check"></i> Copied (fallback)!';
|
|
buttonElement.disabled = true;
|
|
setTimeout(() => {
|
|
buttonElement.innerHTML = originalText;
|
|
buttonElement.disabled = false;
|
|
}, 2000);
|
|
}
|
|
} catch (fallbackErr) {
|
|
console.error('Fallback copy method failed:', fallbackErr);
|
|
}
|
|
});
|
|
} catch (e) {
|
|
console.error('Error parsing BibTeX JSON from data attribute for copy:', e);
|
|
alert('Could not prepare BibTeX for copying. Invalid data format.');
|
|
}
|
|
}
|
|
|
|
// --- Event Listeners ---
|
|
|
|
// For "BibTeX Download" buttons
|
|
document.querySelectorAll('.btnPub--bibtex-download-direct').forEach(button => {
|
|
button.addEventListener('click', function() {
|
|
const bibtexJson = this.dataset.bibtexJson;
|
|
const bibtexFilename = this.dataset.bibtexFilename;
|
|
downloadBibtex(bibtexJson, bibtexFilename);
|
|
});
|
|
});
|
|
|
|
// For "Copy BibTeX" buttons
|
|
document.querySelectorAll('.btnPub--bibtex-copy-direct').forEach(button => {
|
|
button.addEventListener('click', function() {
|
|
const bibtexJson = this.dataset.bibtexJson;
|
|
copyToClipboard(bibtexJson, this);
|
|
});
|
|
});
|
|
}); |