diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index e7d31b7319..1051cbfab7 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -65,7 +65,7 @@ export default class CommandsBackground { const options = await this.passwordGenerationService.getOptions(); const password = await this.passwordGenerationService.generatePassword(options); - this.platformUtilsService.copyToClipboard(password); + this.platformUtilsService.copyToClipboard(password, { window: window }); this.passwordGenerationService.addHistory(password); this.analytics.ga('send', { diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index 248d39590b..bc3f066c9e 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -37,7 +37,7 @@ export default class ContextMenusBackground { private async generatePasswordToClipboard() { const options = await this.passwordGenerationService.getOptions(); const password = await this.passwordGenerationService.generatePassword(options); - this.platformUtilsService.copyToClipboard(password); + this.platformUtilsService.copyToClipboard(password, { window: window }); this.passwordGenerationService.addHistory(password); this.analytics.ga('send', { @@ -73,13 +73,13 @@ export default class ContextMenusBackground { hitType: 'event', eventAction: 'Copied Username From Context Menu', }); - this.platformUtilsService.copyToClipboard(cipher.login.username); + this.platformUtilsService.copyToClipboard(cipher.login.username, { window: window }); } else if (info.parentMenuItemId === 'copy-password') { this.analytics.ga('send', { hitType: 'event', eventAction: 'Copied Password From Context Menu', }); - this.platformUtilsService.copyToClipboard(cipher.login.password); + this.platformUtilsService.copyToClipboard(cipher.login.password, { window: window }); } break; diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index 13a3763f32..a7123408ff 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -149,9 +149,8 @@ export default class RuntimeBackground { tab: msg.tab, details: msg.details, }], msg.sender === 'autofill_cmd'); - - if (totpCode !== null && !this.platformUtilsService.isFirefox()) { - this.platformUtilsService.copyToClipboard(totpCode); + if (totpCode !== null) { + this.platformUtilsService.copyToClipboard(totpCode, { window: window }); } break; case 'contextMenu': @@ -178,8 +177,8 @@ export default class RuntimeBackground { pageDetails: this.pageDetailsToAutoFill, }); - if (totpCode !== null && !this.platformUtilsService.isFirefox()) { - this.platformUtilsService.copyToClipboard(totpCode); + if (totpCode !== null) { + this.platformUtilsService.copyToClipboard(totpCode, { window: window }); } // reset diff --git a/src/popup/components/action-buttons.component.ts b/src/popup/components/action-buttons.component.ts index a89b27d8c7..89e250c48c 100644 --- a/src/popup/components/action-buttons.component.ts +++ b/src/popup/components/action-buttons.component.ts @@ -52,7 +52,7 @@ export class ActionButtonsComponent { } this.analytics.eventTrack.next({ action: 'Copied ' + aType }); - this.platformUtilsService.copyToClipboard(value, { doc: window.document }); + this.platformUtilsService.copyToClipboard(value, { window: window }); this.toasterService.popAsync('info', null, this.i18nService.t('valueCopied', this.i18nService.t(typeI18nKey))); } diff --git a/src/popup/vault/current-tab.component.ts b/src/popup/vault/current-tab.component.ts index b2cca4262b..e1a1494ae1 100644 --- a/src/popup/vault/current-tab.component.ts +++ b/src/popup/vault/current-tab.component.ts @@ -146,7 +146,7 @@ export class CurrentTabComponent implements OnInit, OnDestroy { this.totpCode = totpCode; this.analytics.eventTrack.next({ action: 'Autofilled' }); if (totpCode != null && !this.platformUtilsService.isSafari()) { - this.platformUtilsService.copyToClipboard(totpCode, { doc: window.document }); + this.platformUtilsService.copyToClipboard(totpCode, { window: window }); } if (this.popupUtilsService.inPopup(window)) { @@ -164,7 +164,7 @@ export class CurrentTabComponent implements OnInit, OnDestroy { if (cipher.type === CipherType.Login && this.platformUtilsService.isSafari()) { this.totpTimeout = window.setTimeout(() => { if (this.totpCode != null) { - this.platformUtilsService.copyToClipboard(this.totpCode, { doc: window.document }); + this.platformUtilsService.copyToClipboard(this.totpCode, { window: window }); } }, 500); } diff --git a/src/services/browserPlatformUtils.service.ts b/src/services/browserPlatformUtils.service.ts index daaa085859..cf214fc336 100644 --- a/src/services/browserPlatformUtils.service.ts +++ b/src/services/browserPlatformUtils.service.ts @@ -206,10 +206,19 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService } copyToClipboard(text: string, options?: any): void { - const doc = options ? options.doc : window.document; - if ((window as any).clipboardData && (window as any).clipboardData.setData) { + let win = window; + let doc = window.document; + if (options && options.window) { + win = options.window; + doc = win.document; + } else if (options && options.doc) { + doc = options.doc; + } + if ((win as any).navigator.clipboard && (win as any).navigator.clipboard.writeText) { + (win as any).navigator.clipboard.writeText(text); + } else if ((win as any).clipboardData && (win as any).clipboardData.setData) { // IE specific code path to prevent textarea being shown while dialog is visible. - (window as any).clipboardData.setData('Text', text); + (win as any).clipboardData.setData('Text', text); } else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) { const textarea = doc.createElement('textarea'); textarea.textContent = text;