2017-11-02 04:35:39 +01:00
|
|
|
import { BrowserType } from '../enums/browserType.enum';
|
2017-11-01 14:18:05 +01:00
|
|
|
|
|
|
|
const AnalyticsIds = {
|
2017-11-02 04:35:39 +01:00
|
|
|
[BrowserType.Chrome]: 'UA-81915606-6',
|
|
|
|
[BrowserType.Firefox]: 'UA-81915606-7',
|
|
|
|
[BrowserType.Opera]: 'UA-81915606-8',
|
|
|
|
[BrowserType.Edge]: 'UA-81915606-9',
|
2017-11-01 14:18:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class UtilsService {
|
2017-11-02 22:49:00 +01:00
|
|
|
static urlBase64Decode(str: string): string {
|
|
|
|
let output = str.replace(/-/g, '+').replace(/_/g, '/');
|
|
|
|
switch (output.length % 4) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
output += '==';
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
output += '=';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('Illegal base64url string!');
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodeURIComponent(escape(window.atob(output)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ref: http://stackoverflow.com/a/2117523/1090359
|
2017-11-02 20:51:05 +01:00
|
|
|
static newGuid(): string {
|
|
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
2017-11-02 21:33:54 +01:00
|
|
|
// tslint:disable-next-line
|
2017-11-02 20:51:05 +01:00
|
|
|
const r = Math.random() * 16 | 0;
|
2017-11-02 21:33:54 +01:00
|
|
|
// tslint:disable-next-line
|
2017-11-02 20:51:05 +01:00
|
|
|
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
|
|
return v.toString(16);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-02 17:33:43 +01:00
|
|
|
static extendObject(...objects: any[]): any {
|
|
|
|
for (let i = 1; i < objects.length; i++) {
|
|
|
|
for (const key in objects[i]) {
|
|
|
|
if (objects[i].hasOwnProperty(key)) {
|
|
|
|
objects[0][key] = objects[i][key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return objects[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// EFForg/OpenWireless
|
|
|
|
// ref https://github.com/EFForg/OpenWireless/blob/master/app/js/diceware.js
|
|
|
|
static secureRandomNumber(min: number, max: number): number {
|
|
|
|
let rval = 0;
|
|
|
|
const range = max - min + 1;
|
|
|
|
const bitsNeeded = Math.ceil(Math.log2(range));
|
|
|
|
if (bitsNeeded > 53) {
|
|
|
|
throw new Error('We cannot generate numbers larger than 53 bits.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const bytesNeeded = Math.ceil(bitsNeeded / 8);
|
|
|
|
const mask = Math.pow(2, bitsNeeded) - 1;
|
|
|
|
// 7776 -> (2^13 = 8192) -1 == 8191 or 0x00001111 11111111
|
|
|
|
|
|
|
|
// Create byte array and fill with N random numbers
|
|
|
|
const byteArray = new Uint8Array(bytesNeeded);
|
|
|
|
window.crypto.getRandomValues(byteArray);
|
|
|
|
|
|
|
|
let p = (bytesNeeded - 1) * 8;
|
|
|
|
for (let i = 0; i < bytesNeeded; i++) {
|
|
|
|
rval += byteArray[i] * Math.pow(2, p);
|
|
|
|
p -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use & to apply the mask and reduce the number of recursive lookups
|
|
|
|
// tslint:disable-next-line
|
|
|
|
rval = rval & mask;
|
|
|
|
|
|
|
|
if (rval >= range) {
|
|
|
|
// Integer out of acceptable range
|
|
|
|
return UtilsService.secureRandomNumber(min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an integer that falls within the range
|
|
|
|
return min + rval;
|
|
|
|
}
|
|
|
|
|
2017-11-02 04:35:39 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-02 14:30:28 +01:00
|
|
|
static fromBufferToB64(buffer: ArrayBuffer): string {
|
|
|
|
let binary = '';
|
|
|
|
const bytes = new Uint8Array(buffer);
|
|
|
|
for (let i = 0; i < bytes.byteLength; i++) {
|
|
|
|
binary += String.fromCharCode(bytes[i]);
|
|
|
|
}
|
|
|
|
return window.btoa(binary);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromBufferToUtf8(buffer: ArrayBuffer): string {
|
|
|
|
const bytes = new Uint8Array(buffer);
|
|
|
|
const encodedString = String.fromCharCode.apply(null, bytes);
|
|
|
|
return decodeURIComponent(escape(encodedString));
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromUtf8ToArray(str: string): Uint8Array {
|
|
|
|
const strUtf8 = unescape(encodeURIComponent(str));
|
|
|
|
const arr = new Uint8Array(strUtf8.length);
|
|
|
|
for (let i = 0; i < strUtf8.length; i++) {
|
|
|
|
arr[i] = strUtf8.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static saveObjToStorage(key: string, obj: any) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.storage.local.set({ [key]: obj }, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static removeFromStorage(key: string) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.storage.local.remove(key, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static getObjFromStorage<T>(key: string): Promise<T> {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.storage.local.get(key, (obj: any) => {
|
|
|
|
if (obj && obj[key]) {
|
|
|
|
resolve(obj[key] as T);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-02 04:35:39 +01:00
|
|
|
private browserCache: BrowserType = null;
|
2017-11-01 14:18:05 +01:00
|
|
|
private analyticsIdCache: string = null;
|
|
|
|
|
2017-11-02 04:35:39 +01:00
|
|
|
getBrowser(): BrowserType {
|
2017-11-01 14:18:05 +01:00
|
|
|
if (this.browserCache) {
|
|
|
|
return this.browserCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1) {
|
2017-11-02 04:35:39 +01:00
|
|
|
this.browserCache = BrowserType.Firefox;
|
2017-11-01 18:40:38 +01:00
|
|
|
} else if ((!!(window as any).opr && !!(window as any).opr.addons) || !!(window as any).opera ||
|
|
|
|
navigator.userAgent.indexOf(' OPR/') >= 0) {
|
2017-11-02 04:35:39 +01:00
|
|
|
this.browserCache = BrowserType.Opera;
|
2017-11-01 14:18:05 +01:00
|
|
|
} else if (navigator.userAgent.indexOf(' Edge/') !== -1) {
|
2017-11-02 04:35:39 +01:00
|
|
|
this.browserCache = BrowserType.Edge;
|
2017-11-01 14:18:05 +01:00
|
|
|
} else if ((window as any).chrome) {
|
2017-11-02 04:35:39 +01:00
|
|
|
this.browserCache = BrowserType.Chrome;
|
2017-11-01 14:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.browserCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDeviceType(): number {
|
|
|
|
return this.getBrowser();
|
|
|
|
}
|
|
|
|
|
|
|
|
isFirefox(): boolean {
|
2017-11-02 04:35:39 +01:00
|
|
|
return this.getBrowser() === BrowserType.Firefox;
|
2017-11-01 14:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isChrome(): boolean {
|
2017-11-02 04:35:39 +01:00
|
|
|
return this.getBrowser() === BrowserType.Chrome;
|
2017-11-01 14:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isEdge(): boolean {
|
2017-11-02 04:35:39 +01:00
|
|
|
return this.getBrowser() === BrowserType.Edge;
|
2017-11-01 14:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isOpera(): boolean {
|
2017-11-02 04:35:39 +01:00
|
|
|
return this.getBrowser() === BrowserType.Opera;
|
2017-11-01 14:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
analyticsId(): string {
|
|
|
|
if (this.analyticsIdCache) {
|
|
|
|
return this.analyticsIdCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.analyticsIdCache = AnalyticsIds[this.getBrowser()];
|
|
|
|
|
|
|
|
return this.analyticsIdCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
initListSectionItemListeners(doc: any, angular: any) {
|
|
|
|
if (!doc) {
|
|
|
|
throw new Error('doc parameter required');
|
|
|
|
}
|
|
|
|
|
|
|
|
doc.on('click', '.list-section-item', (e: JQuery.Event) => {
|
|
|
|
if (e.isDefaultPrevented && e.isDefaultPrevented.name === 'returnTrue') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-01 18:40:38 +01:00
|
|
|
const text = $(this).find('input, textarea')
|
|
|
|
.not('input[type="checkbox"], input[type="radio"], input[type="hidden"]');
|
2017-11-01 14:18:05 +01:00
|
|
|
const checkbox = $(this).find('input[type="checkbox"]');
|
|
|
|
const select = $(this).find('select');
|
|
|
|
|
|
|
|
if (text.length > 0 && e.target === text[0]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (checkbox.length > 0 && e.target === checkbox[0]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (select.length > 0 && e.target === select[0]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (text.length > 0) {
|
|
|
|
text.focus();
|
|
|
|
} else if (checkbox.length > 0) {
|
|
|
|
checkbox.prop('checked', !checkbox.is(':checked'));
|
|
|
|
if (angular) {
|
|
|
|
angular.element(checkbox[0]).triggerHandler('click');
|
|
|
|
}
|
|
|
|
} else if (select.length > 0) {
|
|
|
|
select.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
doc.on(
|
|
|
|
'focus',
|
|
|
|
'.list-section-item input, .list-section-item select, .list-section-item textarea',
|
|
|
|
(e: Event) => {
|
2017-11-01 16:41:12 +01:00
|
|
|
$(this).parent().addClass('active');
|
|
|
|
});
|
2017-11-01 14:18:05 +01:00
|
|
|
doc.on(
|
|
|
|
'blur', '.list-section-item input, .list-section-item select, .list-section-item textarea', (e: Event) => {
|
2017-11-01 16:41:12 +01:00
|
|
|
$(this).parent().removeClass('active');
|
|
|
|
});
|
2017-11-01 14:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getDomain(uriString: string) {
|
|
|
|
if (!uriString) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
uriString = uriString.trim();
|
|
|
|
if (uriString === '') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uriString.startsWith('http://') || uriString.startsWith('https://')) {
|
|
|
|
try {
|
|
|
|
const url = new URL(uriString);
|
|
|
|
if (!url || !url.hostname) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.hostname === 'localhost' || this.validIpAddress(url.hostname)) {
|
|
|
|
return url.hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((window as any).tldjs) {
|
|
|
|
const domain = (window as any).tldjs.getDomain(uriString);
|
|
|
|
if (domain) {
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.hostname;
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else if ((window as any).tldjs) {
|
|
|
|
const domain2 = (window as any).tldjs.getDomain(uriString);
|
|
|
|
if (domain2) {
|
|
|
|
return domain2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getHostname(uriString: string) {
|
|
|
|
if (!uriString) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
uriString = uriString.trim();
|
|
|
|
if (uriString === '') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uriString.startsWith('http://') || uriString.startsWith('https://')) {
|
|
|
|
try {
|
|
|
|
const url = new URL(uriString);
|
|
|
|
if (!url || !url.hostname) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.hostname;
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
copyToClipboard(text: string, doc: Document) {
|
|
|
|
doc = doc || document;
|
|
|
|
if ((window as any).clipboardData && (window as any).clipboardData.setData) {
|
|
|
|
// IE specific code path to prevent textarea being shown while dialog is visible.
|
|
|
|
return (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.
|
|
|
|
return doc.execCommand('copy');
|
|
|
|
} catch (ex) {
|
|
|
|
// tslint:disable-next-line
|
|
|
|
console.warn('Copy to clipboard failed.', ex);
|
|
|
|
return false;
|
|
|
|
} finally {
|
|
|
|
doc.body.removeChild(textarea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inSidebar(theWindow: Window) {
|
|
|
|
return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=sidebar') > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inTab(theWindow: Window) {
|
|
|
|
return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=tab') > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inPopout(theWindow: Window) {
|
|
|
|
return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=popout') > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inPopup(theWindow: Window) {
|
|
|
|
return theWindow.location.search === '' || theWindow.location.search.indexOf('uilocation=') === -1 ||
|
|
|
|
theWindow.location.search.indexOf('uilocation=popup') > -1;
|
|
|
|
}
|
|
|
|
|
2017-11-02 14:30:28 +01:00
|
|
|
// remove these in favor of static
|
|
|
|
|
2017-11-01 14:18:05 +01:00
|
|
|
saveObjToStorage(key: string, obj: any) {
|
|
|
|
return new Promise((resolve) => {
|
2017-11-01 16:41:12 +01:00
|
|
|
chrome.storage.local.set({ [key]: obj }, () => {
|
2017-11-01 14:18:05 +01:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
removeFromStorage(key: string) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.storage.local.remove(key, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-02 14:30:28 +01:00
|
|
|
getObjFromStorage(key: string) {
|
2017-11-01 14:18:05 +01:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.storage.local.get(key, (obj: any) => {
|
|
|
|
if (obj && obj[key]) {
|
2017-11-02 14:30:28 +01:00
|
|
|
resolve(obj[key]);
|
2017-11-01 14:18:05 +01:00
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private validIpAddress(ipString: string) {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|