bitwarden-estensione-browser/src/services/webPlatformUtils.service.ts

168 lines
5.0 KiB
TypeScript
Raw Normal View History

2018-06-05 21:02:53 +02:00
import { DeviceType } from 'jslib/enums/deviceType';
import { MessagingService } from 'jslib/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { AnalyticsIds } from 'jslib/misc/analytics';
2018-06-07 23:12:11 +02:00
import { Utils } from 'jslib/misc/utils';
2018-06-05 21:02:53 +02:00
export class WebPlatformUtilsService implements PlatformUtilsService {
identityClientId: string = 'web';
2018-06-07 23:12:11 +02:00
private browserCache: string = null;
2018-06-05 21:02:53 +02:00
constructor(private messagingService: MessagingService) { }
getDevice(): DeviceType {
2018-06-07 23:12:11 +02:00
return DeviceType.Web;
2018-06-05 21:02:53 +02:00
}
getDeviceString(): string {
2018-06-07 23:12:11 +02:00
return 'Web';
2018-06-05 21:02:53 +02:00
}
isFirefox(): boolean {
2018-06-07 23:12:11 +02:00
return this.getBrowserType() === 'firefox';
2018-06-05 21:02:53 +02:00
}
isChrome(): boolean {
2018-06-07 23:12:11 +02:00
return this.getBrowserType() === 'chrome';
2018-06-05 21:02:53 +02:00
}
isEdge(): boolean {
2018-06-07 23:12:11 +02:00
return this.getBrowserType() === 'edge';
2018-06-06 15:43:28 +02:00
}
2018-06-05 21:02:53 +02:00
isOpera(): boolean {
2018-06-07 23:12:11 +02:00
return this.getBrowserType() === 'opera';
2018-06-05 21:02:53 +02:00
}
isVivaldi(): boolean {
2018-06-07 23:12:11 +02:00
return this.getBrowserType() === 'vivaldi';
2018-06-05 21:02:53 +02:00
}
isSafari(): boolean {
2018-06-07 23:12:11 +02:00
return this.getBrowserType() === 'safari';
}
isIE(): boolean {
return this.getBrowserType() === 'ie';
2018-06-05 21:02:53 +02:00
}
isMacAppStore(): boolean {
return false;
}
analyticsId(): string {
2018-06-07 23:12:11 +02:00
return 'UA-81915606-3';
2018-06-05 21:02:53 +02:00
}
getDomain(uriString: string): string {
2018-06-07 23:12:11 +02:00
return Utils.getHostname(uriString);
2018-06-05 21:02:53 +02:00
}
isViewOpen(): boolean {
return false;
}
launchUri(uri: string, options?: any): void {
2018-06-07 23:12:11 +02:00
const a = document.createElement('a');
a.href = uri;
a.target = '_blank';
a.rel = 'noreferrer noopener';
a.click();
2018-06-05 21:02:53 +02:00
}
saveFile(win: Window, blobData: any, blobOptions: any, fileName: string): void {
2018-06-08 18:23:46 +02:00
const blob = new Blob([blobData], blobOptions);
if (navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
const a = win.document.createElement('a');
a.href = win.URL.createObjectURL(blob);
a.download = fileName;
a.style.position = 'fixed';
win.document.body.appendChild(a);
a.click();
win.document.body.removeChild(a);
}
2018-06-05 21:02:53 +02:00
}
getApplicationVersion(): string {
return '1.2.3';
}
supportsU2f(win: Window): boolean {
if (win != null && (win as any).u2f != null) {
return true;
}
return this.isChrome() || this.isOpera();
}
supportsDuo(): boolean {
return true;
}
showToast(type: 'error' | 'success' | 'warning' | 'info', title: string, text: string, global?: any): void {
throw new Error('showToast not implemented');
}
showDialog(text: string, title?: string, confirmText?: string, cancelText?: string, type?: string) {
2018-06-07 23:12:11 +02:00
return Promise.resolve(false);
2018-06-05 21:02:53 +02:00
}
isDev(): boolean {
return process.env.ENV === 'development';
}
copyToClipboard(text: string, options?: any): void {
const doc = options ? options.doc : window.document;
if ((window as any).clipboardData && (window as any).clipboardData.setData) {
// IE specific code path to prevent textarea being shown while dialog is visible.
(window as any).clipboardData.setData('Text', text);
} else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) {
const textarea = doc.createElement('textarea');
textarea.textContent = text;
// Prevent scrolling to bottom of page in MS Edge.
textarea.style.position = 'fixed';
doc.body.appendChild(textarea);
textarea.select();
try {
// Security exception may be thrown by some browsers.
doc.execCommand('copy');
} catch (e) {
// tslint:disable-next-line
console.warn('Copy to clipboard failed.', e);
} finally {
doc.body.removeChild(textarea);
}
}
}
2018-06-07 23:12:11 +02:00
private getBrowserType(): string {
if (this.browserCache != null) {
return this.browserCache;
2018-06-05 21:02:53 +02:00
}
2018-06-07 23:12:11 +02:00
if (navigator.userAgent.indexOf(' Firefox/') !== -1 || navigator.userAgent.indexOf(' Gecko/') !== -1) {
this.browserCache = 'firefox';
} else if (navigator.userAgent.indexOf(' OPR/') >= 0) {
this.browserCache = 'opera';
} else if (navigator.userAgent.indexOf(' Edge/') !== -1) {
this.browserCache = 'edge';
} else if (navigator.userAgent.indexOf(' Vivaldi/') !== -1) {
this.browserCache = 'vivaldi';
} else if (navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1) {
this.browserCache = 'safari';
} else if ((window as any).chrome && navigator.userAgent.indexOf(' Chrome/') !== -1) {
this.browserCache = 'chrome';
} else if (navigator.userAgent.indexOf(' Trident/') !== -1) {
this.browserCache = 'ie';
}
return this.browserCache;
2018-06-05 21:02:53 +02:00
}
}