0

I am running the following Coffescript code to copy certain data to the users clipboard.

ready = ->

copyEmailBtn = document.querySelector('.clipboard-copy')
copyEmailBtn.addEventListener 'click', (event) ->
  targetClass = document.querySelector('.clipboard-copy').dataset.targetClass
  target = document.querySelector(targetClass)
  range = document.createRange()
  range.selectNode target
  window.getSelection().addRange range
  try
    document.execCommand('copy')
  catch err
    console.log 'Oops, unable to copy'
  window.getSelection().removeAllRanges()
  return

if $('.clipboard-copy').length > 0
  $(document).ready(ready)
  $(document).on('page:load', ready)

When I click the related button to start the copy process chrome throws the following error

The issue with this is that I am not able to track it down since it only occurs in one out of 4 attempts.

The copy button works as expected those other 3 out 4 times however whenever this error occurs it doesn't copy the text.

I didn't try any other browsers since our app does not work in any other browser and is not supposed to.

Has anyone dealt with this error before?

I tried the following suggestions as you can see from the code Discontiguous selection is not supported

All the other reports about this error say it shouldn't affect functionality and is more like a verbose warning.

Community
  • 1
  • 1
Valvyn
  • 11
  • 3

1 Answers1

0

I fixed it with the following code

ready = ->

copyEmailBtn = document.querySelector('.clipboard-copy')
copyEmailBtn.addEventListener 'click', (event) ->
  window.getSelection().removeAllRanges()
  targetClass = document.querySelector('.clipboard-copy').dataset.targetClass
  target = document.querySelector(targetClass)
  range = document.createRange()
  range.selectNode target
  window.getSelection().addRange range
  try
    document.execCommand('copy')
  catch err
    console.log 'Oops, unable to copy'
  window.getSelection().removeAllRanges()
  return

if $('.clipboard-copy').length > 0
  $(document).ready(ready)
  $(document).on('page:load', ready)
Valvyn
  • 11
  • 3