2018-01-04 18:32:10 +01:00
|
|
|
import * as tldjs from 'tldjs';
|
2018-01-06 20:22:55 +01:00
|
|
|
|
2018-01-09 20:26:20 +01:00
|
|
|
import { DeviceType } from 'jslib/enums';
|
|
|
|
|
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions';
|
2018-01-04 18:32:10 +01:00
|
|
|
|
|
|
|
const AnalyticsIds = {
|
2018-01-09 20:26:20 +01:00
|
|
|
[DeviceType.Chrome]: 'UA-81915606-6',
|
|
|
|
[DeviceType.Firefox]: 'UA-81915606-7',
|
|
|
|
[DeviceType.Opera]: 'UA-81915606-8',
|
|
|
|
[DeviceType.Edge]: 'UA-81915606-9',
|
|
|
|
[DeviceType.Vivaldi]: 'UA-81915606-15',
|
|
|
|
[DeviceType.Safari]: 'UA-81915606-16',
|
2018-01-04 18:32:10 +01:00
|
|
|
};
|
|
|
|
|
2018-01-09 20:26:20 +01:00
|
|
|
export default class BrowserPlatformUtilsService implements PlatformUtilsService {
|
2018-01-04 18:32:10 +01:00
|
|
|
static getDomain(uriString: string): string {
|
|
|
|
if (uriString == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
uriString = uriString.trim();
|
|
|
|
if (uriString === '') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uriString.startsWith('http://') || uriString.startsWith('https://')) {
|
|
|
|
try {
|
|
|
|
const url = new URL(uriString);
|
|
|
|
|
2018-01-05 22:38:50 +01:00
|
|
|
if (url.hostname === 'localhost' || BrowserPlatformUtilsService.validIpAddress(url.hostname)) {
|
2018-01-04 18:32:10 +01:00
|
|
|
return url.hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
const urlDomain = tldjs.getDomain(url.hostname);
|
|
|
|
return urlDomain != null ? urlDomain : url.hostname;
|
|
|
|
} catch (e) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
const domain = tldjs.getDomain(uriString);
|
|
|
|
if (domain != null) {
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static validIpAddress(ipString: string): boolean {
|
|
|
|
// tslint:disable-next-line
|
|
|
|
const ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
|
|
return ipRegex.test(ipString);
|
|
|
|
}
|
|
|
|
|
2018-01-09 20:26:20 +01:00
|
|
|
private deviceCache: DeviceType = null;
|
2018-01-04 18:32:10 +01:00
|
|
|
private analyticsIdCache: string = null;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1) {
|
2018-01-09 20:26:20 +01:00
|
|
|
this.deviceCache = DeviceType.Firefox;
|
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-01-09 20:26:20 +01:00
|
|
|
this.deviceCache = DeviceType.Opera;
|
2018-01-04 18:32:10 +01:00
|
|
|
} else if (navigator.userAgent.indexOf(' Edge/') !== -1) {
|
2018-01-09 20:26:20 +01:00
|
|
|
this.deviceCache = DeviceType.Edge;
|
2018-01-04 18:32:10 +01:00
|
|
|
} else if (navigator.userAgent.indexOf(' Vivaldi/') !== -1) {
|
2018-01-09 20:26:20 +01:00
|
|
|
this.deviceCache = DeviceType.Vivaldi;
|
2018-01-11 21:49:29 +01:00
|
|
|
} else if (navigator.userAgent.indexOf('Safari') !== -1 && navigator.userAgent.indexOf('Chrome') === -1) {
|
|
|
|
this.deviceCache = DeviceType.Safari;
|
2018-01-04 18:32:10 +01:00
|
|
|
} else if ((window as any).chrome) {
|
2018-01-09 20:26:20 +01:00
|
|
|
this.deviceCache = DeviceType.Chrome;
|
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-01-09 20:26:20 +01:00
|
|
|
return DeviceType[this.getDevice()].toLowerCase();
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isFirefox(): boolean {
|
2018-01-09 20:26:20 +01:00
|
|
|
return this.getDevice() === DeviceType.Firefox;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isChrome(): boolean {
|
2018-01-09 20:26:20 +01:00
|
|
|
return this.getDevice() === DeviceType.Chrome;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isEdge(): boolean {
|
2018-01-09 20:26:20 +01:00
|
|
|
return this.getDevice() === DeviceType.Edge;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isOpera(): boolean {
|
2018-01-09 20:26:20 +01:00
|
|
|
return this.getDevice() === DeviceType.Opera;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isVivaldi(): boolean {
|
2018-01-09 20:26:20 +01:00
|
|
|
return this.getDevice() === DeviceType.Vivaldi;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isSafari(): boolean {
|
2018-01-09 20:26:20 +01:00
|
|
|
return this.getDevice() === DeviceType.Safari;
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
analyticsId(): string {
|
|
|
|
if (this.analyticsIdCache) {
|
|
|
|
return this.analyticsIdCache;
|
|
|
|
}
|
|
|
|
|
2018-01-05 17:13:24 +01:00
|
|
|
this.analyticsIdCache = AnalyticsIds[this.getDevice()];
|
2018-01-04 18:32:10 +01:00
|
|
|
return this.analyticsIdCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDomain(uriString: string): string {
|
2018-01-05 22:38:50 +01:00
|
|
|
return BrowserPlatformUtilsService.getDomain(uriString);
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 22:49:58 +01:00
|
|
|
isViewOpen(): boolean {
|
2018-01-12 04:22:07 +01:00
|
|
|
if (this.isSafari()) {
|
|
|
|
// TODO
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-04 22:49:58 +01:00
|
|
|
const popupOpen = chrome.extension.getViews({ type: 'popup' }).length > 0;
|
2018-01-05 17:13:24 +01:00
|
|
|
if (popupOpen) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-01-04 18:32:10 +01:00
|
|
|
}
|