1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-21 18:53:29 +00:00

clear clipboard timeout implemented

This commit is contained in:
Kyle Spearrin
2019-02-27 09:28:16 -05:00
parent d2a736ed71
commit 5b088b2b3c
5 changed files with 44 additions and 21 deletions

View File

@@ -27,7 +27,7 @@ describe('Browser Utils Service', () => {
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.ChromeExtension);
});
@@ -37,7 +37,7 @@ describe('Browser Utils Service', () => {
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.FirefoxExtension);
});
@@ -52,7 +52,7 @@ describe('Browser Utils Service', () => {
value: {}
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.OperaExtension);
});
@@ -62,7 +62,7 @@ describe('Browser Utils Service', () => {
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 9) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.EdgeExtension);
});
@@ -77,7 +77,7 @@ describe('Browser Utils Service', () => {
value: {}
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.SafariExtension);
});
@@ -87,7 +87,7 @@ describe('Browser Utils Service', () => {
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.97 Safari/537.36 Vivaldi/1.94.1008.40'
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null);
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.VivaldiExtension);
});
});

View File

@@ -16,7 +16,8 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
private deviceCache: DeviceType = null;
private analyticsIdCache: string = null;
constructor(private messagingService: MessagingService) { }
constructor(private messagingService: MessagingService,
private clipboardWriteCallback: (clipboardValue: string, clearMs: number) => void) { }
getDevice(): DeviceType {
if (this.deviceCache) {
@@ -185,14 +186,22 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
} else if (options && options.doc) {
doc = options.doc;
}
const clearMs: number = options && options.clearMs ? options.clearMs : null;
if (this.isFirefox() && (win as any).navigator.clipboard && (win as any).navigator.clipboard.writeText) {
(win as any).navigator.clipboard.writeText(text);
(win as any).navigator.clipboard.writeText(text).then(() => {
if (this.clipboardWriteCallback != null) {
this.clipboardWriteCallback(text, clearMs);
}
});
} else if ((win as any).clipboardData && (win as any).clipboardData.setData) {
// IE specific code path to prevent textarea being shown while dialog is visible.
(win as any).clipboardData.setData('Text', text);
if (this.clipboardWriteCallback != null) {
this.clipboardWriteCallback(text, clearMs);
}
} else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) {
const textarea = doc.createElement('textarea');
textarea.textContent = text;
textarea.textContent = text == null || text === '' ? ' ' : text;
// Prevent scrolling to bottom of page in MS Edge.
textarea.style.position = 'fixed';
doc.body.appendChild(textarea);
@@ -200,7 +209,9 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
try {
// Security exception may be thrown by some browsers.
doc.execCommand('copy');
if (doc.execCommand('copy') && this.clipboardWriteCallback != null) {
this.clipboardWriteCallback(text, clearMs);
}
} catch (e) {
// tslint:disable-next-line
console.warn('Copy to clipboard failed.', e);