utils service additions

This commit is contained in:
Kyle Spearrin 2017-11-01 23:35:39 -04:00
parent 9633ffe265
commit c073d74e04
1 changed files with 26 additions and 22 deletions

View File

@ -1,35 +1,39 @@
enum Browser { import { BrowserType } from '../enums/browserType.enum';
Chrome = 2,
Firefox = 3,
Opera = 4,
Edge = 5,
}
const AnalyticsIds = { const AnalyticsIds = {
[Browser.Chrome]: 'UA-81915606-6', [BrowserType.Chrome]: 'UA-81915606-6',
[Browser.Firefox]: 'UA-81915606-7', [BrowserType.Firefox]: 'UA-81915606-7',
[Browser.Opera]: 'UA-81915606-8', [BrowserType.Opera]: 'UA-81915606-8',
[Browser.Edge]: 'UA-81915606-9', [BrowserType.Edge]: 'UA-81915606-9',
}; };
export default class UtilsService { export default class UtilsService {
private browserCache: Browser = null; static fromB64ToArray(str: string): Uint8Array {
const binaryString = window.atob(str);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
private browserCache: BrowserType = null;
private analyticsIdCache: string = null; private analyticsIdCache: string = null;
getBrowser(): Browser { getBrowser(): BrowserType {
if (this.browserCache) { if (this.browserCache) {
return this.browserCache; return this.browserCache;
} }
if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1) { if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1) {
this.browserCache = Browser.Firefox; this.browserCache = BrowserType.Firefox;
} else if ((!!(window as any).opr && !!(window as any).opr.addons) || !!(window as any).opera || } else if ((!!(window as any).opr && !!(window as any).opr.addons) || !!(window as any).opera ||
navigator.userAgent.indexOf(' OPR/') >= 0) { navigator.userAgent.indexOf(' OPR/') >= 0) {
this.browserCache = Browser.Opera; this.browserCache = BrowserType.Opera;
} else if (navigator.userAgent.indexOf(' Edge/') !== -1) { } else if (navigator.userAgent.indexOf(' Edge/') !== -1) {
this.browserCache = Browser.Edge; this.browserCache = BrowserType.Edge;
} else if ((window as any).chrome) { } else if ((window as any).chrome) {
this.browserCache = Browser.Chrome; this.browserCache = BrowserType.Chrome;
} }
return this.browserCache; return this.browserCache;
@ -40,19 +44,19 @@ export default class UtilsService {
} }
isFirefox(): boolean { isFirefox(): boolean {
return this.getBrowser() === Browser.Firefox; return this.getBrowser() === BrowserType.Firefox;
} }
isChrome(): boolean { isChrome(): boolean {
return this.getBrowser() === Browser.Chrome; return this.getBrowser() === BrowserType.Chrome;
} }
isEdge(): boolean { isEdge(): boolean {
return this.getBrowser() === Browser.Edge; return this.getBrowser() === BrowserType.Edge;
} }
isOpera(): boolean { isOpera(): boolean {
return this.getBrowser() === Browser.Opera; return this.getBrowser() === BrowserType.Opera;
} }
analyticsId(): string { analyticsId(): string {
@ -243,11 +247,11 @@ export default class UtilsService {
}); });
} }
getObjFromStorage(key: string) { getObjFromStorage<T>(key: string): Promise<T> {
return new Promise((resolve) => { return new Promise((resolve) => {
chrome.storage.local.get(key, (obj: any) => { chrome.storage.local.get(key, (obj: any) => {
if (obj && obj[key]) { if (obj && obj[key]) {
resolve(obj[key]); resolve(obj[key] as T);
} else { } else {
resolve(null); resolve(null);
} }