added workaround to copy arbitrary text to che clipboard

This commit is contained in:
Roberto Tonino 2020-08-19 17:35:25 +02:00
parent 40c7ba9a0c
commit e1a3e8b562
3 changed files with 37 additions and 16 deletions

File diff suppressed because one or more lines are too long

View File

@ -15,7 +15,7 @@
<script>
import Downloads from '@/utils/downloads'
import downloadQualities from '@js/qualities'
import { generatePath } from '@/utils/utils'
import { generatePath, copyToClipboard } from '@/utils/utils'
export default {
data() {
@ -55,9 +55,10 @@ export default {
show: false,
position: 3,
action: () => {
navigator.clipboard.writeText(this.generalHref).catch(err => {
console.error('Link copying failed', err)
})
// navigator.clipboard.writeText(this.generalHref).catch(err => {
// console.error('Link copying failed', err)
// })
copyToClipboard(this.generalHref)
}
},
copyImageLink: {
@ -65,9 +66,10 @@ export default {
show: false,
position: 4,
action: () => {
navigator.clipboard.writeText(this.imgSrc).catch(err => {
console.error('Image copying failed', err)
})
// navigator.clipboard.writeText(this.imgSrc).catch(err => {
// console.error('Image copying failed', err)
// })
copyToClipboard(this.imgSrc)
}
},
copyDeezerLink: {
@ -75,14 +77,15 @@ export default {
show: false,
position: 5,
action: () => {
navigator.clipboard.writeText(this.generalHref).catch(err => {
console.error('Deezer link copying failed', err)
})
// navigator.clipboard.writeText(this.deezerHref).catch(err => {
// console.error('Deezer link copying failed', err)
// })
copyToClipboard(this.deezerHref)
}
},
paste: {
label: this.$t('globals.paste'),
show: true,
show: !window.clientMode,
position: 6,
action: () => {
navigator.clipboard.readText().then(text => {

View File

@ -73,6 +73,24 @@ export function debounce(func, wait, immediate) {
}
}
/**
* Workaround to copy to the clipboard cross-OS by generating a
* ghost input and copying the passed String
*
* @param {string} text Text to copy
*/
export function copyToClipboard(text) {
const ghostInput = document.createElement('input')
document.body.appendChild(ghostInput)
ghostInput.setAttribute('type', 'text')
ghostInput.setAttribute('value', text)
ghostInput.select()
ghostInput.setSelectionRange(0, 99999)
document.execCommand('copy')
ghostInput.remove()
}
export const COUNTRIES = {
AF: 'Afghanistan',
AX: '\u00c5land Islands',