2018-01-13 03:31:46 +01:00
|
|
|
import { BrowserApi } from '../browser/browserApi';
|
2019-08-19 17:58:43 +02:00
|
|
|
import { SafariApp } from '../browser/safariApp';
|
2018-01-13 03:31:46 +01:00
|
|
|
|
2018-04-10 20:20:03 +02:00
|
|
|
import { DeviceType } from 'jslib/enums/deviceType';
|
2018-01-09 20:26:20 +01:00
|
|
|
|
2018-04-10 20:20:03 +02:00
|
|
|
import { MessagingService } from 'jslib/abstractions/messaging.service';
|
2018-04-11 05:49:46 +02:00
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
2018-01-04 18:32:10 +01:00
|
|
|
|
2018-04-24 23:04:49 +02:00
|
|
|
import { AnalyticsIds } from 'jslib/misc/analytics';
|
2018-01-04 18:32:10 +01:00
|
|
|
|
2018-04-14 15:39:47 +02:00
|
|
|
const DialogPromiseExpiration = 600000; // 10 minutes
|
2018-04-10 20:20:03 +02:00
|
|
|
|
2018-01-09 20:26:20 +01:00
|
|
|
export default class BrowserPlatformUtilsService implements PlatformUtilsService {
|
2018-03-21 16:21:42 +01:00
|
|
|
identityClientId: string = 'browser';
|
|
|
|
|
2018-04-10 20:20:03 +02:00
|
|
|
private showDialogResolves = new Map<number, { resolve: (value: boolean) => void, date: Date }>();
|
2018-01-09 20:26:20 +01:00
|
|
|
private deviceCache: DeviceType = null;
|
2018-01-04 18:32:10 +01:00
|
|
|
private analyticsIdCache: string = null;
|
|
|
|
|
2019-02-27 15:28:16 +01:00
|
|
|
constructor(private messagingService: MessagingService,
|
|
|
|
private clipboardWriteCallback: (clipboardValue: string, clearMs: number) => void) { }
|
2018-04-10 20:20:03 +02:00
|
|
|
|
2018-01-09 20:26:20 +01:00
|
|
|
getDevice(): DeviceType {
|
2018-01-05 17:13:24 +01:00
|
|
|
if (this.deviceCache) {
|
|
|
|
return this.deviceCache;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
2019-08-19 17:58:43 +02:00
|
|
|
if (this.isSafariExtension()) {
|
2019-08-13 17:57:20 +02:00
|
|
|
this.deviceCache = DeviceType.SafariExtension;
|
|
|
|
} else if (navigator.userAgent.indexOf(' Firefox/') !== -1 || navigator.userAgent.indexOf(' Gecko/') !== -1) {
|
2018-07-09 15:12:41 +02:00
|
|
|
this.deviceCache = DeviceType.FirefoxExtension;
|
2018-01-04 18:32:10 +01:00
|
|
|
} else if ((!!(window as any).opr && !!opr.addons) || !!(window as any).opera ||
|
|
|
|
navigator.userAgent.indexOf(' OPR/') >= 0) {
|
2018-07-09 15:12:41 +02:00
|
|
|
this.deviceCache = DeviceType.OperaExtension;
|
2018-01-04 18:32:10 +01:00
|
|
|
} else if (navigator.userAgent.indexOf(' Edge/') !== -1) {
|
2018-07-09 15:12:41 +02:00
|
|
|
this.deviceCache = DeviceType.EdgeExtension;
|
2018-01-04 18:32:10 +01:00
|
|
|
} else if (navigator.userAgent.indexOf(' Vivaldi/') !== -1) {
|
2018-07-09 15:12:41 +02:00
|
|
|
this.deviceCache = DeviceType.VivaldiExtension;
|
2018-01-16 03:40:42 +01:00
|
|
|
} else if ((window as any).chrome && navigator.userAgent.indexOf(' Chrome/') !== -1) {
|
2018-07-09 15:12:41 +02:00
|
|
|
this.deviceCache = DeviceType.ChromeExtension;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 17:13:24 +01:00
|
|
|
return this.deviceCache;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 17:13:24 +01:00
|
|
|
getDeviceString(): string {
|
2018-07-09 15:30:37 +02:00
|
|
|
const device = DeviceType[this.getDevice()].toLowerCase();
|
|
|
|
return device.replace('extension', '');
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isFirefox(): boolean {
|
2018-07-09 15:12:41 +02:00
|
|
|
return this.getDevice() === DeviceType.FirefoxExtension;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isChrome(): boolean {
|
2018-07-09 15:12:41 +02:00
|
|
|
return this.getDevice() === DeviceType.ChromeExtension;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isEdge(): boolean {
|
2018-07-09 15:12:41 +02:00
|
|
|
return this.getDevice() === DeviceType.EdgeExtension;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isOpera(): boolean {
|
2018-07-09 15:12:41 +02:00
|
|
|
return this.getDevice() === DeviceType.OperaExtension;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isVivaldi(): boolean {
|
2018-07-09 15:12:41 +02:00
|
|
|
return this.getDevice() === DeviceType.VivaldiExtension;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isSafari(): boolean {
|
2018-07-09 15:12:41 +02:00
|
|
|
return this.getDevice() === DeviceType.SafariExtension;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
2018-06-08 05:36:58 +02:00
|
|
|
isIE(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-27 05:51:17 +01:00
|
|
|
isMacAppStore(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-04 18:32:10 +01:00
|
|
|
analyticsId(): string {
|
|
|
|
if (this.analyticsIdCache) {
|
|
|
|
return this.analyticsIdCache;
|
|
|
|
}
|
|
|
|
|
2018-01-24 18:24:31 +01:00
|
|
|
this.analyticsIdCache = (AnalyticsIds as any)[this.getDevice()];
|
2018-01-04 18:32:10 +01:00
|
|
|
return this.analyticsIdCache;
|
|
|
|
}
|
|
|
|
|
2018-01-04 22:49:58 +01:00
|
|
|
isViewOpen(): boolean {
|
2018-01-13 03:31:46 +01:00
|
|
|
if (BrowserApi.isPopupOpen()) {
|
2018-01-12 04:22:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-13 03:31:46 +01:00
|
|
|
if (this.isSafari()) {
|
2019-08-19 21:57:34 +02:00
|
|
|
// TODO
|
2018-01-13 03:31:46 +01:00
|
|
|
return false;
|
2018-01-05 17:13:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 22:49:58 +01:00
|
|
|
const sidebarView = this.sidebarViewName();
|
|
|
|
const sidebarOpen = sidebarView != null && chrome.extension.getViews({ type: sidebarView }).length > 0;
|
2018-01-05 17:13:24 +01:00
|
|
|
if (sidebarOpen) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-04 22:49:58 +01:00
|
|
|
|
2018-01-05 17:13:24 +01:00
|
|
|
const tabOpen = chrome.extension.getViews({ type: 'tab' }).length > 0;
|
|
|
|
return tabOpen;
|
2018-01-04 22:49:58 +01:00
|
|
|
}
|
|
|
|
|
2018-06-09 20:50:29 +02:00
|
|
|
lockTimeout(): number {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:31:54 +01:00
|
|
|
launchUri(uri: string, options?: any): void {
|
|
|
|
BrowserApi.createNewTab(uri, options && options.extensionPage === true);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveFile(win: Window, blobData: any, blobOptions: any, fileName: string): void {
|
|
|
|
BrowserApi.downloadFile(win, blobData, blobOptions, fileName);
|
|
|
|
}
|
|
|
|
|
2018-01-26 16:48:32 +01:00
|
|
|
getApplicationVersion(): string {
|
|
|
|
return BrowserApi.getApplicationVersion();
|
|
|
|
}
|
|
|
|
|
2018-02-02 04:53:36 +01:00
|
|
|
supportsU2f(win: Window): boolean {
|
2018-04-14 19:32:56 +02:00
|
|
|
if (win != null && (win as any).u2f != null) {
|
2018-02-02 04:53:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:22:58 +02:00
|
|
|
return this.isChrome() || this.isOpera() || this.isVivaldi();
|
2018-02-02 04:53:36 +01:00
|
|
|
}
|
|
|
|
|
2018-05-16 21:30:36 +02:00
|
|
|
supportsDuo(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-03 15:46:00 +02:00
|
|
|
showToast(type: 'error' | 'success' | 'warning' | 'info', title: string, text: string | string[],
|
|
|
|
options?: any): void {
|
2018-10-03 05:33:56 +02:00
|
|
|
this.messagingService.send('showToast', {
|
|
|
|
text: text,
|
|
|
|
title: title,
|
|
|
|
type: type,
|
|
|
|
options: options,
|
|
|
|
});
|
2018-05-31 20:42:59 +02:00
|
|
|
}
|
|
|
|
|
2018-02-03 05:46:30 +01:00
|
|
|
showDialog(text: string, title?: string, confirmText?: string, cancelText?: string, type?: string) {
|
2018-04-10 20:20:03 +02:00
|
|
|
const dialogId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
|
|
this.messagingService.send('showDialog', {
|
|
|
|
text: text,
|
|
|
|
title: title,
|
|
|
|
confirmText: confirmText,
|
|
|
|
cancelText: cancelText,
|
|
|
|
type: type,
|
|
|
|
dialogId: dialogId,
|
|
|
|
});
|
|
|
|
return new Promise<boolean>((resolve) => {
|
|
|
|
this.showDialogResolves.set(dialogId, { resolve: resolve, date: new Date() });
|
|
|
|
});
|
2018-02-03 05:46:30 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 06:21:22 +02:00
|
|
|
eventTrack(action: string, label?: string, options?: any) {
|
|
|
|
this.messagingService.send('analyticsEventTrack', {
|
|
|
|
action: action,
|
|
|
|
label: label,
|
|
|
|
options: options,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:23:30 +01:00
|
|
|
isDev(): boolean {
|
2018-04-14 19:32:56 +02:00
|
|
|
return process.env.ENV === 'development';
|
2018-02-13 23:23:30 +01:00
|
|
|
}
|
|
|
|
|
2018-06-30 19:51:20 +02:00
|
|
|
isSelfHost(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-16 20:00:41 +01:00
|
|
|
copyToClipboard(text: string, options?: any): void {
|
2018-08-13 15:44:59 +02:00
|
|
|
let win = window;
|
|
|
|
let doc = window.document;
|
2018-08-17 18:25:09 +02:00
|
|
|
if (options && (options.window || options.win)) {
|
|
|
|
win = options.window || options.win;
|
2018-08-13 15:44:59 +02:00
|
|
|
doc = win.document;
|
|
|
|
} else if (options && options.doc) {
|
|
|
|
doc = options.doc;
|
|
|
|
}
|
2019-05-30 15:37:09 +02:00
|
|
|
const clearing = options ? !!options.clearing : false;
|
2019-02-27 15:28:16 +01:00
|
|
|
const clearMs: number = options && options.clearMs ? options.clearMs : null;
|
2019-08-19 17:58:43 +02:00
|
|
|
if (this.isSafariExtension()) {
|
|
|
|
SafariApp.sendMessageToApp('copyToClipboard', text).then(() => {
|
|
|
|
if (!clearing && this.clipboardWriteCallback != null) {
|
|
|
|
this.clipboardWriteCallback(text, clearMs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (this.isFirefox() && (win as any).navigator.clipboard && (win as any).navigator.clipboard.writeText) {
|
2019-02-27 15:28:16 +01:00
|
|
|
(win as any).navigator.clipboard.writeText(text).then(() => {
|
2019-05-30 15:37:09 +02:00
|
|
|
if (!clearing && this.clipboardWriteCallback != null) {
|
2019-02-27 15:28:16 +01:00
|
|
|
this.clipboardWriteCallback(text, clearMs);
|
|
|
|
}
|
|
|
|
});
|
2018-08-13 15:44:59 +02:00
|
|
|
} else if ((win as any).clipboardData && (win as any).clipboardData.setData) {
|
2018-04-23 19:04:11 +02:00
|
|
|
// IE specific code path to prevent textarea being shown while dialog is visible.
|
2018-08-13 15:44:59 +02:00
|
|
|
(win as any).clipboardData.setData('Text', text);
|
2019-05-30 15:37:09 +02:00
|
|
|
if (!clearing && this.clipboardWriteCallback != null) {
|
2019-02-27 15:28:16 +01:00
|
|
|
this.clipboardWriteCallback(text, clearMs);
|
|
|
|
}
|
2018-04-23 19:04:11 +02:00
|
|
|
} else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) {
|
|
|
|
const textarea = doc.createElement('textarea');
|
2019-02-27 15:28:16 +01:00
|
|
|
textarea.textContent = text == null || text === '' ? ' ' : text;
|
2018-04-23 19:04:11 +02:00
|
|
|
// 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.
|
2019-08-13 17:57:20 +02:00
|
|
|
if (doc.execCommand('copy') && !clearing && this.clipboardWriteCallback != null) {
|
2019-02-27 15:28:16 +01:00
|
|
|
this.clipboardWriteCallback(text, clearMs);
|
|
|
|
}
|
2018-04-23 19:04:11 +02:00
|
|
|
} catch (e) {
|
|
|
|
// tslint:disable-next-line
|
|
|
|
console.warn('Copy to clipboard failed.', e);
|
|
|
|
} finally {
|
|
|
|
doc.body.removeChild(textarea);
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 20:00:41 +01:00
|
|
|
}
|
|
|
|
|
2019-02-27 04:37:21 +01:00
|
|
|
async readFromClipboard(options?: any): Promise<string> {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-19 17:58:43 +02:00
|
|
|
if (this.isSafariExtension()) {
|
|
|
|
return await SafariApp.sendMessageToApp('readFromClipboard');
|
|
|
|
} else if (this.isFirefox() && (win as any).navigator.clipboard && (win as any).navigator.clipboard.readText) {
|
2019-02-27 04:37:21 +01:00
|
|
|
return await (win as any).navigator.clipboard.readText();
|
|
|
|
} else if (doc.queryCommandSupported && doc.queryCommandSupported('paste')) {
|
|
|
|
const textarea = doc.createElement('textarea');
|
|
|
|
// Prevent scrolling to bottom of page in MS Edge.
|
|
|
|
textarea.style.position = 'fixed';
|
|
|
|
doc.body.appendChild(textarea);
|
|
|
|
textarea.focus();
|
|
|
|
try {
|
|
|
|
// Security exception may be thrown by some browsers.
|
|
|
|
if (doc.execCommand('paste')) {
|
|
|
|
return textarea.value;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// tslint:disable-next-line
|
|
|
|
console.warn('Read from clipboard failed.', e);
|
|
|
|
} finally {
|
|
|
|
doc.body.removeChild(textarea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-04-10 20:20:03 +02:00
|
|
|
resolveDialogPromise(dialogId: number, confirmed: boolean) {
|
|
|
|
if (this.showDialogResolves.has(dialogId)) {
|
|
|
|
const resolveObj = this.showDialogResolves.get(dialogId);
|
|
|
|
resolveObj.resolve(confirmed);
|
|
|
|
this.showDialogResolves.delete(dialogId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up old promises
|
|
|
|
const deleteIds: number[] = [];
|
|
|
|
this.showDialogResolves.forEach((val, key) => {
|
|
|
|
const age = new Date().getTime() - val.date.getTime();
|
|
|
|
if (age > DialogPromiseExpiration) {
|
|
|
|
deleteIds.push(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
deleteIds.forEach((id) => {
|
|
|
|
this.showDialogResolves.delete(id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-04 22:49:58 +01:00
|
|
|
private sidebarViewName(): string {
|
|
|
|
if ((window as any).chrome.sidebarAction && this.isFirefox()) {
|
|
|
|
return 'sidebar';
|
|
|
|
} else if (this.isOpera() && (typeof opr !== 'undefined') && opr.sidebarAction) {
|
|
|
|
return 'sidebar_panel';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-19 17:58:43 +02:00
|
|
|
|
|
|
|
private isSafariExtension(): boolean {
|
|
|
|
return (window as any).safariAppExtension === true;
|
|
|
|
}
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|