1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-13 23:03:32 +00:00

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

@@ -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;