From eace0f32f01fcab3e5783c64d3ea426f7f2a8f5c Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 22 Sep 2017 15:31:31 -0400 Subject: [PATCH] copy totp to clipboard from popup document in ff --- src/popup/app/current/currentController.js | 5 ++++- src/services/autofillService.js | 6 ++++-- src/services/utilsService.js | 13 +++++++------ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/popup/app/current/currentController.js b/src/popup/app/current/currentController.js index f3651656f4..ed7c19f880 100644 --- a/src/popup/app/current/currentController.js +++ b/src/popup/app/current/currentController.js @@ -72,8 +72,11 @@ angular toastr.error(i18nService.autofillError); } - autofillService.doAutoFill(login, pageDetails, false).then(function () { + autofillService.doAutoFill(login, pageDetails, false).then(function (totpCode) { $analytics.eventTrack('Autofilled'); + if (totpCode && utilsService.isFirefox()) { + utilsService.copyToClipboard(totpCode, document); + } $window.close(); }, function () { $analytics.eventTrack('Autofilled Error'); diff --git a/src/services/autofillService.js b/src/services/autofillService.js index a3f27f7bbb..ad0e8d52db 100644 --- a/src/services/autofillService.js +++ b/src/services/autofillService.js @@ -260,13 +260,15 @@ function initAutofill() { if (code) { self.utilsService.copyToClipboard(code); } + + return code; }); } if (didAutofill) { if (totpPromise) { - totpPromise.then(function () { - deferred.resolve(); + totpPromise.then(function (totpCode) { + deferred.resolve(totpCode); }); } else { diff --git a/src/services/utilsService.js b/src/services/utilsService.js index 6660dec6c5..099705f0ce 100644 --- a/src/services/utilsService.js +++ b/src/services/utilsService.js @@ -200,29 +200,30 @@ function initUtilsService() { return null; }; - UtilsService.prototype.copyToClipboard = function (text) { + UtilsService.prototype.copyToClipboard = function (text, doc) { + doc = doc || document; if (window.clipboardData && window.clipboardData.setData) { // IE specific code path to prevent textarea being shown while dialog is visible. return clipboardData.setData('Text', text); } - else if (document.queryCommandSupported && document.queryCommandSupported('copy')) { - var textarea = document.createElement('textarea'); + else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) { + var textarea = doc.createElement('textarea'); textarea.textContent = text; // Prevent scrolling to bottom of page in MS Edge. textarea.style.position = 'fixed'; - document.body.appendChild(textarea); + doc.body.appendChild(textarea); textarea.select(); try { // Security exception may be thrown by some browsers. - return document.execCommand('copy'); + return doc.execCommand('copy'); } catch (ex) { console.warn('Copy to clipboard failed.', ex); return false; } finally { - document.body.removeChild(textarea); + doc.body.removeChild(textarea); } } };