Fixed copy not working in insecure contexts
This commit is contained in:
parent
f38d793f4e
commit
9985aeca86
2 changed files with 29 additions and 4 deletions
|
|
@ -38,6 +38,30 @@ let liveSocket = new LiveSocket('/live', Socket, {
|
|||
}
|
||||
})
|
||||
|
||||
window.copyTextToClipboard = async (text) => {
|
||||
// Navigator clipboard api needs a secure context (https)
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
await navigator.clipboard.writeText(text)
|
||||
} else {
|
||||
const textArea = document.createElement('textarea')
|
||||
textArea.value = text
|
||||
// Move textarea out of the viewport so it's not visible
|
||||
textArea.style.position = 'absolute'
|
||||
textArea.style.left = '-999999px'
|
||||
|
||||
document.body.prepend(textArea)
|
||||
textArea.select()
|
||||
|
||||
try {
|
||||
document.execCommand('copy')
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
} finally {
|
||||
textArea.remove()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Show progress bar on live navigation and form submits
|
||||
topbar.config({ barColors: { 0: '#29d' }, shadowColor: 'rgba(0, 0, 0, .3)' })
|
||||
window.addEventListener('phx:page-loading-start', (_info) => topbar.show(300))
|
||||
|
|
|
|||
|
|
@ -25,12 +25,13 @@
|
|||
<span
|
||||
x-data="{ copied: false }"
|
||||
x-on:click={"
|
||||
navigator.clipboard.writeText('#{rss_feed_url(@conn, @source)}');
|
||||
copied = true;
|
||||
setTimeout(() => copied = false, 4000);
|
||||
window.copyTextToClipboard('#{rss_feed_url(@conn, @source)}')
|
||||
copied = true
|
||||
setTimeout(() => copied = false, 4000)
|
||||
"}
|
||||
>
|
||||
Copy RSS Feed <span x-show="copied"><.icon name="hero-check" class="ml-2 h-4 w-4" /></span>
|
||||
Copy RSS Feed
|
||||
<span x-show="copied" x-transition.duration.150ms><.icon name="hero-check" class="ml-2 h-4 w-4" /></span>
|
||||
</span>
|
||||
</:option>
|
||||
<:option>
|
||||
|
|
|
|||
Loading…
Reference in a new issue