Browser viewer: Try using document.execCommand to copy since it works on non-secure origins as well

This commit is contained in:
Kovid Goyal 2020-10-19 17:04:02 +05:30
parent 20e84ebcc8
commit 87629e32a7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 30 additions and 0 deletions

View File

@ -871,6 +871,13 @@ class IframeBoss:
select_crw(crw)
def copy_selection(self):
if not runtime.is_standalone_viewer:
try:
if document.execCommand('copy'):
return
except:
pass
s = window.getSelection()
text = s.toString()
if text:

View File

@ -84,6 +84,29 @@ class ReadUI:
ui_operations.open_url = def(url):
window.open(url, '_blank')
ui_operations.copy_selection = def(text, html):
if not html:
# try using document.execCommand which on chrome allows the
# copy on non-secure origin if this is close to a user
# interaction event
active_elem = document.activeElement
ta = document.createElement("textarea")
ta.value = text
ta.style.position = 'absolute'
ta.style.top = window.innerHeight + 'px'
ta.style.left = window.innerWidth + 'px'
document.body.appendChild(ta)
ta.focus()
ta.select()
ok = False
try:
ok = document.execCommand('copy')
except:
pass
document.body.removeChild(ta)
if active_elem:
active_elem.focus()
if ok:
return
if not window.navigator.clipboard:
return error_dialog(_('No clipboard access'), _(
'Your browser requires you to access the Content server over an HTTPS connection'