24 lines
927 B
TypeScript
24 lines
927 B
TypeScript
document.addEventListener('DOMContentLoaded', () => {
|
|
document.querySelectorAll('.copyable-text').forEach(el => {
|
|
const contentEl = el.querySelector('.content');
|
|
const selection = window.getSelection();
|
|
if (contentEl && selection) {
|
|
contentEl.addEventListener('click', () => {
|
|
selection.selectAllChildren(contentEl);
|
|
});
|
|
el.querySelector('.copy-button')?.addEventListener('click', () => {
|
|
selection.selectAllChildren(contentEl);
|
|
document.execCommand('copy');
|
|
});
|
|
}
|
|
});
|
|
|
|
document.querySelectorAll<HTMLElement>('.copy-button[data-content]').forEach(el => {
|
|
el.addEventListener('click', () => {
|
|
navigator.clipboard.writeText(`${el.dataset.content}`)
|
|
.then(() => console.log('copy success'))
|
|
.catch(console.error);
|
|
});
|
|
});
|
|
});
|