From b93a33eba47533fdd52a846e2a23e508ad17e724 Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Thu, 3 Aug 2017 20:13:17 -0700 Subject: [PATCH] an ios friendly copy --- frontend/src/upload.js | 15 +++------------ frontend/src/utils.js | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/frontend/src/upload.js b/frontend/src/upload.js index 0d0aa48c..0f768244 100644 --- a/frontend/src/upload.js +++ b/frontend/src/upload.js @@ -2,6 +2,7 @@ require('./common'); const FileSender = require('./fileSender'); const { + copyToClipboard, notify, gcmCompliant, findMetric, @@ -79,12 +80,7 @@ $(document).ready(function() { sendEvent('sender', 'copied', { cd4: 'success-screen' }); - const aux = document.createElement('input'); - aux.setAttribute('value', $('#link').attr('value')); - document.body.appendChild(aux); - aux.select(); - document.execCommand('copy'); - document.body.removeChild(aux); + copyToClipboard($('#link').attr('value')); //disable button for 3s $copyBtn.attr('disabled', true); $('#link').attr('disabled', true); @@ -392,12 +388,7 @@ $(document).ready(function() { sendEvent('sender', 'copied', { cd4: 'upload-list' }); - const aux = document.createElement('input'); - aux.setAttribute('value', url); - document.body.appendChild(aux); - aux.select(); - document.execCommand('copy'); - document.body.removeChild(aux); + copyToClipboard(url); document.l10n.formatValue('copiedUrl').then(translated => { link.innerHTML = translated; }); diff --git a/frontend/src/utils.js b/frontend/src/utils.js index 91b8da04..2566914f 100644 --- a/frontend/src/utils.js +++ b/frontend/src/utils.js @@ -114,9 +114,31 @@ function sendEvent() { .catch(() => 0); } +function copyToClipboard(str) { + const aux = document.createElement('input'); + aux.setAttribute('value', str); + aux.contentEditable = true; + aux.readOnly = true; + document.body.appendChild(aux); + if (navigator.userAgent.match(/iphone|ipad|ipod/i)) { + const range = document.createRange(); + range.selectNodeContents(aux); + const sel = window.getSelection(); + sel.removeAllRanges(); + sel.addRange(range); + aux.setSelectionRange(0, str.length); + } + else { + aux.select(); + } + document.execCommand('copy'); + document.body.removeChild(aux); +} + const ONE_DAY_IN_MS = 86400000; module.exports = { + copyToClipboard, arrayToHex, hexToArray, notify,