add copy button
Thu Jun 01 2023 14:32:38 GMT+0000 (Coordinated Universal Time)
Saved by
@miltonmatiasj
copyButtons = document.querySelectorAll('.copy-button')
img = document.createElement('img')
img.src = '/loading.svg'
img.style.filter = 'invert(45%) sepia(99%) saturate(2815%) hue-rotate(179deg) brightness(97%) contrast(103%)'
for(button of copyButtons) {
button.addEventListener('click', async (event) => {
clickedButton = event.target
resultId = clickedButton.id.split('-')[1]
// select button icon and remove it
let buttonNode = document.querySelector(`#copyButton-${resultId}`)
let icon = buttonNode.children[0]
icon.classList.remove('icon-copy')
buttonNode.insertBefore(img, buttonNode.firstChild)
// Get the text field
let copyText = document.getElementById(`urlInput-${resultId}`);
// Select the text field
copyText.select();
copyText.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
navigator.clipboard.writeText(await shortenUrl(copyText, 1));
buttonNode.removeChild(buttonNode.children[0])
icon.classList.add('icon-copy')
})
}
content_copyCOPY
Comments