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

291 lines
9.9 KiB
TypeScript
Raw Normal View History

import * as _swal from 'sweetalert';
import { SweetAlert } from 'sweetalert/typings/core';
2018-06-08 20:56:26 +02:00
2018-06-05 21:02:53 +02:00
import { DeviceType } from 'jslib/enums/deviceType';
2018-06-08 20:56:26 +02:00
import { I18nService } from 'jslib/abstractions/i18n.service';
import { MessagingService } from 'jslib/abstractions/messaging.service';
2018-06-05 21:02:53 +02:00
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-06-07 23:12:11 +02:00
import { Utils } from 'jslib/misc/utils';
2018-06-05 21:02:53 +02:00
// Hack due to Angular 5.2 bug
const swal: SweetAlert = _swal as any;
2018-06-05 21:02:53 +02:00
export class WebPlatformUtilsService implements PlatformUtilsService {
identityClientId: string = 'web';
2018-07-09 15:32:24 +02:00
private browserCache: DeviceType = null;
2018-06-05 21:02:53 +02:00
constructor(private i18nService: I18nService, private messagingService: MessagingService) { }
2018-06-05 21:02:53 +02:00
getDevice(): DeviceType {
2018-07-09 15:32:24 +02:00
if (this.browserCache != null) {
return this.browserCache;
}
if (navigator.userAgent.indexOf(' Firefox/') !== -1 || navigator.userAgent.indexOf(' Gecko/') !== -1) {
this.browserCache = DeviceType.FirefoxBrowser;
} else if (navigator.userAgent.indexOf(' OPR/') >= 0) {
this.browserCache = DeviceType.OperaBrowser;
} else if (navigator.userAgent.indexOf(' Edge/') !== -1) {
this.browserCache = DeviceType.EdgeBrowser;
} else if (navigator.userAgent.indexOf(' Vivaldi/') !== -1) {
this.browserCache = DeviceType.VivaldiBrowser;
} else if (navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1) {
this.browserCache = DeviceType.SafariBrowser;
} else if ((window as any).chrome && navigator.userAgent.indexOf(' Chrome/') !== -1) {
this.browserCache = DeviceType.ChromeBrowser;
} else if (navigator.userAgent.indexOf(' Trident/') !== -1) {
this.browserCache = DeviceType.IEBrowser;
} else {
this.browserCache = DeviceType.UnknownBrowser;
}
return this.browserCache;
2018-06-05 21:02:53 +02:00
}
getDeviceString(): string {
2018-07-09 15:32:24 +02:00
const device = DeviceType[this.getDevice()].toLowerCase();
return device.replace('browser', '');
2018-06-05 21:02:53 +02:00
}
isFirefox(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.FirefoxBrowser;
2018-06-05 21:02:53 +02:00
}
isChrome(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.ChromeBrowser;
2018-06-05 21:02:53 +02:00
}
isEdge(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.EdgeBrowser;
2018-06-06 15:43:28 +02:00
}
2018-06-05 21:02:53 +02:00
isOpera(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.OperaBrowser;
2018-06-05 21:02:53 +02:00
}
isVivaldi(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.VivaldiBrowser;
2018-06-05 21:02:53 +02:00
}
isSafari(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.SafariBrowser;
2018-06-07 23:12:11 +02:00
}
isIE(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.IEBrowser;
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
}
isViewOpen(): boolean {
return false;
}
2018-06-09 20:55:34 +02:00
lockTimeout(): number {
return null;
2018-06-09 20:55:34 +02:00
}
2018-06-05 21:02:53 +02:00
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.classList.add('d-none');
document.body.appendChild(a);
2018-06-07 23:12:11 +02:00
a.click();
document.body.removeChild(a);
2018-06-05 21:02:53 +02:00
}
saveFile(win: Window, blobData: any, blobOptions: any, fileName: string): void {
let blob: Blob = null;
2018-10-30 14:54:14 +01:00
let type: string = null;
const fileNameLower = fileName.toLowerCase();
let doDownload = true;
2018-10-30 14:54:14 +01:00
if (fileNameLower.endsWith('.pdf')) {
type = 'application/pdf';
doDownload = false;
2018-10-30 14:54:14 +01:00
} else if (fileNameLower.endsWith('.xlsx')) {
type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
} else if (fileNameLower.endsWith('.docx')) {
type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
} else if (fileNameLower.endsWith('.pptx')) {
type = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
} else if (fileNameLower.endsWith('.csv')) {
type = 'text/csv';
} else if (fileNameLower.endsWith('.png')) {
type = 'image/png';
} else if (fileNameLower.endsWith('.jpg') || fileNameLower.endsWith('.jpeg')) {
type = 'image/jpeg';
} else if (fileNameLower.endsWith('.gif')) {
type = 'image/gif';
}
if (type != null) {
blobOptions = blobOptions || {};
if (blobOptions.type == null) {
blobOptions.type = type;
}
}
if (blobOptions != null && !this.isIE()) {
blob = new Blob([blobData], blobOptions);
} else {
blob = new Blob([blobData]);
}
2018-06-08 18:23:46 +02:00
if (navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
const a = win.document.createElement('a');
if (doDownload) {
a.download = fileName;
} else {
a.target = '_blank';
}
2018-06-08 18:23:46 +02:00
a.href = win.URL.createObjectURL(blob);
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 {
2018-06-11 15:31:11 +02:00
return process.env.APPLICATION_VERSION || '-';
2018-06-05 21:02:53 +02:00
}
supportsU2f(win: Window): boolean {
if (win != null && (win as any).u2f != null) {
return true;
}
2019-01-10 17:23:16 +01:00
return this.isChrome() || ((this.isOpera() || this.isVivaldi()) && !Utils.isMobileBrowser);
2018-06-05 21:02:53 +02:00
}
supportsDuo(): boolean {
return true;
}
showToast(type: 'error' | 'success' | 'warning' | 'info', title: string, text: string | string[],
options?: any): void {
this.messagingService.send('showToast', {
text: text,
title: title,
type: type,
options: options,
});
2018-06-05 21:02:53 +02:00
}
2018-06-08 20:56:26 +02:00
async showDialog(text: string, title?: string, confirmText?: string, cancelText?: string, type?: string) {
const buttons = [confirmText == null ? this.i18nService.t('ok') : confirmText];
if (cancelText != null) {
buttons.unshift(cancelText);
}
const contentDiv = document.createElement('div');
if (type != null) {
const icon = document.createElement('i');
icon.classList.add('swal-custom-icon');
switch (type) {
case 'success':
icon.classList.add('fa', 'fa-check', 'text-success');
break;
case 'warning':
icon.classList.add('fa', 'fa-warning', 'text-warning');
break;
case 'error':
icon.classList.add('fa', 'fa-bolt', 'text-danger');
break;
case 'info':
icon.classList.add('fa', 'fa-info-circle', 'text-info');
break;
default:
break;
}
if (icon.classList.contains('fa')) {
contentDiv.appendChild(icon);
}
}
if (title != null) {
const titleDiv = document.createElement('div');
titleDiv.classList.add('swal-title');
titleDiv.appendChild(document.createTextNode(title));
contentDiv.appendChild(titleDiv);
}
if (text != null) {
const textDiv = document.createElement('div');
textDiv.classList.add('swal-text');
textDiv.appendChild(document.createTextNode(text));
contentDiv.appendChild(textDiv);
}
2018-11-14 21:45:03 +01:00
const confirmed = buttons.length > 1 ? await swal({
2018-06-08 20:56:26 +02:00
content: { element: contentDiv },
buttons: buttons,
2018-11-14 21:45:03 +01:00
}) : await (swal as any)({
content: { element: contentDiv },
button: buttons[0],
2018-06-08 20:56:26 +02:00
});
return confirmed;
2018-06-05 21:02:53 +02:00
}
eventTrack(action: string, label?: string, options?: any) {
this.messagingService.send('analyticsEventTrack', {
action: action,
label: label,
options: options,
});
}
2018-06-05 21:02:53 +02:00
isDev(): boolean {
return process.env.ENV === 'development';
}
2018-06-30 20:14:52 +02:00
isSelfHost(): boolean {
return process.env.SELF_HOST.toString() === 'true';
}
2018-06-05 21:02:53 +02:00
copyToClipboard(text: string, options?: any): void {
2018-08-17 18:25:21 +02:00
let win = window;
let doc = window.document;
if (options && (options.window || options.win)) {
win = options.window || options.win;
doc = win.document;
} else if (options && options.doc) {
doc = options.doc;
}
2018-08-20 15:20:24 +02:00
if ((win as any).clipboardData && (win as any).clipboardData.setData) {
2018-06-05 21:02:53 +02:00
// IE specific code path to prevent textarea being shown while dialog is visible.
2018-08-17 18:25:21 +02:00
(win as any).clipboardData.setData('Text', text);
2018-06-05 21:02:53 +02:00
} 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';
2018-07-26 04:01:26 +02:00
let copyEl = doc.body;
// For some reason copy command won't work in Firefox when modal is open if appending to body
if (this.isFirefox() && doc.body.classList.contains('modal-open')) {
copyEl = doc.body.querySelector<HTMLElement>('.modal');
}
copyEl.appendChild(textarea);
2018-06-05 21:02:53 +02:00
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 {
2018-07-26 04:01:26 +02:00
copyEl.removeChild(textarea);
2018-06-05 21:02:53 +02:00
}
}
}
}