use `navigator.clipboard` to copy text if available

This commit is contained in:
Kyle Spearrin 2018-08-13 09:44:59 -04:00
parent 2a9cd36a01
commit ada83aae8f
6 changed files with 23 additions and 15 deletions

View File

@ -65,7 +65,7 @@ export default class CommandsBackground {
const options = await this.passwordGenerationService.getOptions(); const options = await this.passwordGenerationService.getOptions();
const password = await this.passwordGenerationService.generatePassword(options); const password = await this.passwordGenerationService.generatePassword(options);
this.platformUtilsService.copyToClipboard(password); this.platformUtilsService.copyToClipboard(password, { window: window });
this.passwordGenerationService.addHistory(password); this.passwordGenerationService.addHistory(password);
this.analytics.ga('send', { this.analytics.ga('send', {

View File

@ -37,7 +37,7 @@ export default class ContextMenusBackground {
private async generatePasswordToClipboard() { private async generatePasswordToClipboard() {
const options = await this.passwordGenerationService.getOptions(); const options = await this.passwordGenerationService.getOptions();
const password = await this.passwordGenerationService.generatePassword(options); const password = await this.passwordGenerationService.generatePassword(options);
this.platformUtilsService.copyToClipboard(password); this.platformUtilsService.copyToClipboard(password, { window: window });
this.passwordGenerationService.addHistory(password); this.passwordGenerationService.addHistory(password);
this.analytics.ga('send', { this.analytics.ga('send', {
@ -73,13 +73,13 @@ export default class ContextMenusBackground {
hitType: 'event', hitType: 'event',
eventAction: 'Copied Username From Context Menu', 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') { } else if (info.parentMenuItemId === 'copy-password') {
this.analytics.ga('send', { this.analytics.ga('send', {
hitType: 'event', hitType: 'event',
eventAction: 'Copied Password From Context Menu', eventAction: 'Copied Password From Context Menu',
}); });
this.platformUtilsService.copyToClipboard(cipher.login.password); this.platformUtilsService.copyToClipboard(cipher.login.password, { window: window });
} }
break; break;

View File

@ -149,9 +149,8 @@ export default class RuntimeBackground {
tab: msg.tab, tab: msg.tab,
details: msg.details, details: msg.details,
}], msg.sender === 'autofill_cmd'); }], msg.sender === 'autofill_cmd');
if (totpCode !== null) {
if (totpCode !== null && !this.platformUtilsService.isFirefox()) { this.platformUtilsService.copyToClipboard(totpCode, { window: window });
this.platformUtilsService.copyToClipboard(totpCode);
} }
break; break;
case 'contextMenu': case 'contextMenu':
@ -178,8 +177,8 @@ export default class RuntimeBackground {
pageDetails: this.pageDetailsToAutoFill, pageDetails: this.pageDetailsToAutoFill,
}); });
if (totpCode !== null && !this.platformUtilsService.isFirefox()) { if (totpCode !== null) {
this.platformUtilsService.copyToClipboard(totpCode); this.platformUtilsService.copyToClipboard(totpCode, { window: window });
} }
// reset // reset

View File

@ -52,7 +52,7 @@ export class ActionButtonsComponent {
} }
this.analytics.eventTrack.next({ action: 'Copied ' + aType }); 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.toasterService.popAsync('info', null,
this.i18nService.t('valueCopied', this.i18nService.t(typeI18nKey))); this.i18nService.t('valueCopied', this.i18nService.t(typeI18nKey)));
} }

View File

@ -146,7 +146,7 @@ export class CurrentTabComponent implements OnInit, OnDestroy {
this.totpCode = totpCode; this.totpCode = totpCode;
this.analytics.eventTrack.next({ action: 'Autofilled' }); this.analytics.eventTrack.next({ action: 'Autofilled' });
if (totpCode != null && !this.platformUtilsService.isSafari()) { if (totpCode != null && !this.platformUtilsService.isSafari()) {
this.platformUtilsService.copyToClipboard(totpCode, { doc: window.document }); this.platformUtilsService.copyToClipboard(totpCode, { window: window });
} }
if (this.popupUtilsService.inPopup(window)) { if (this.popupUtilsService.inPopup(window)) {
@ -164,7 +164,7 @@ export class CurrentTabComponent implements OnInit, OnDestroy {
if (cipher.type === CipherType.Login && this.platformUtilsService.isSafari()) { if (cipher.type === CipherType.Login && this.platformUtilsService.isSafari()) {
this.totpTimeout = window.setTimeout(() => { this.totpTimeout = window.setTimeout(() => {
if (this.totpCode != null) { if (this.totpCode != null) {
this.platformUtilsService.copyToClipboard(this.totpCode, { doc: window.document }); this.platformUtilsService.copyToClipboard(this.totpCode, { window: window });
} }
}, 500); }, 500);
} }

View File

@ -206,10 +206,19 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
} }
copyToClipboard(text: string, options?: any): void { copyToClipboard(text: string, options?: any): void {
const doc = options ? options.doc : window.document; let win = window;
if ((window as any).clipboardData && (window as any).clipboardData.setData) { 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. // 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')) { } else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) {
const textarea = doc.createElement('textarea'); const textarea = doc.createElement('textarea');
textarea.textContent = text; textarea.textContent = text;