From 4b68163cbd43515ace5631a2ef15a4e010af1679 Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Wed, 1 Nov 2017 14:18:05 +0100 Subject: [PATCH] Convert utilService to typescript. (#342) --- package.json | 4 +- src/background.html | 1 - src/background.js | 1 + src/services/utils.service.ts | 262 +++++++++++++++++++++++++++++++ src/services/utilsService.js | 283 ---------------------------------- 5 files changed, 266 insertions(+), 285 deletions(-) create mode 100644 src/services/utils.service.ts delete mode 100644 src/services/utilsService.js diff --git a/package.json b/package.json index cc618dba72..ded3ed29fe 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,8 @@ "webpack-stream": "4.0.0" }, "dependencies": { - "@types/angular": "^1.6.34" + "@types/angular": "^1.6.34", + "@types/chrome": "0.0.51", + "@types/jquery": "^3.2.16" } } diff --git a/src/background.html b/src/background.html index 206a410e60..4e0713875a 100644 --- a/src/background.html +++ b/src/background.html @@ -10,7 +10,6 @@ - diff --git a/src/background.js b/src/background.js index 03d62e2d16..52afb715a3 100644 --- a/src/background.js +++ b/src/background.js @@ -1,4 +1,5 @@ import LockService from './services/lockService.js'; +import UtilsService from './services/utils.service'; var bg_isBackground = true, bg_utilsService, diff --git a/src/services/utils.service.ts b/src/services/utils.service.ts new file mode 100644 index 0000000000..0f709917f6 --- /dev/null +++ b/src/services/utils.service.ts @@ -0,0 +1,262 @@ +enum Browser { + Chrome = 2, + Firefox = 3, + Opera = 4, + Edge= 5, +} + +const AnalyticsIds = { + [Browser.Chrome]: 'UA-81915606-6', + [Browser.Firefox]: 'UA-81915606-7', + [Browser.Opera]: 'UA-81915606-8', + [Browser.Edge]: 'UA-81915606-9', +}; + +export default class UtilsService { + + private browserCache: Browser = null; + private analyticsIdCache: string = null; + + getBrowser(): Browser { + if (this.browserCache) { + return this.browserCache; + } + + if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1) { + this.browserCache = Browser.Firefox; + } else if ((!!(window as any).opr && !!(window as any).opr.addons) || !!(window as any).opera || navigator.userAgent.indexOf(' OPR/') >= 0) { + this.browserCache = Browser.Opera; + } else if (navigator.userAgent.indexOf(' Edge/') !== -1) { + this.browserCache = Browser.Edge; + } else if ((window as any).chrome) { + this.browserCache = Browser.Chrome; + } + + return this.browserCache; + } + + getDeviceType(): number { + return this.getBrowser(); + } + + isFirefox(): boolean { + return this.getBrowser() === Browser.Firefox; + } + + isChrome(): boolean { + return this.getBrowser() === Browser.Chrome; + } + + isEdge(): boolean { + return this.getBrowser() === Browser.Edge; + } + + isOpera(): boolean { + return this.getBrowser() === Browser.Opera; + } + + 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; + } + + const text = $(this).find('input, textarea').not('input[type="checkbox"], input[type="radio"], input[type="hidden"]'); + 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) => { + $(this).parent().addClass('active'); + }); + doc.on( + 'blur', '.list-section-item input, .list-section-item select, .list-section-item textarea', (e: Event) => { + $(this).parent().removeClass('active'); + }); + } + + 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; + } + + saveObjToStorage(key: string, obj: any) { + return new Promise((resolve) => { + chrome.storage.local.set({[key]: obj}, () => { + resolve(); + }); + }); + } + + removeFromStorage(key: string) { + return new Promise((resolve) => { + chrome.storage.local.remove(key, () => { + resolve(); + }); + }); + } + + getObjFromStorage(key: string) { + return new Promise((resolve) => { + chrome.storage.local.get(key, (obj: any) => { + if (obj && obj[key]) { + resolve(obj[key]); + } 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); + } +} diff --git a/src/services/utilsService.js b/src/services/utilsService.js deleted file mode 100644 index 8673b3a8f2..0000000000 --- a/src/services/utilsService.js +++ /dev/null @@ -1,283 +0,0 @@ -function UtilsService() { - initUtilsService(); - - this.browserCache = null; - this.analyticsIdCache = null; -} - -function initUtilsService() { - UtilsService.prototype.getBrowser = function () { - if (this.browserCache) { - return this.browserCache; - } - - if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1) { - this.browserCache = 'firefox'; - } - else if ((!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0) { - this.browserCache = 'opera'; - } - else if (navigator.userAgent.indexOf(' Edge/') !== -1) { - this.browserCache = 'edge'; - } - else if (window.chrome) { - this.browserCache = 'chrome'; - } - - return this.browserCache; - }; - - UtilsService.prototype.isFirefox = function () { - return this.getBrowser() === 'firefox'; - }; - - UtilsService.prototype.isChrome = function () { - return this.getBrowser() === 'chrome'; - }; - - UtilsService.prototype.isEdge = function () { - return this.getBrowser() === 'edge'; - }; - - UtilsService.prototype.isOpera = function () { - return this.getBrowser() === 'opera'; - }; - - UtilsService.prototype.analyticsId = function () { - if (this.analyticsIdCache) { - return this.analyticsIdCache; - } - - if (this.isChrome()) { - this.analyticsIdCache = 'UA-81915606-6'; - } - else if (this.isFirefox()) { - this.analyticsIdCache = 'UA-81915606-7'; - } - else if (this.isEdge()) { - this.analyticsIdCache = 'UA-81915606-9'; - } - else if (this.isOpera()) { - this.analyticsIdCache = 'UA-81915606-8'; - } - - return this.analyticsIdCache; - }; - - UtilsService.prototype.getDeviceType = function () { - if (this.isChrome()) { - return 2; - } - else if (this.isFirefox()) { - return 3; - } - else if (this.isEdge()) { - return 5; - } - else if (this.isOpera()) { - return 4; - } - - return -1; - }; - - UtilsService.prototype.initListSectionItemListeners = function (doc, angular) { - if (!doc) { - throw 'doc parameter required'; - } - - doc.on('click', '.list-section-item', function (e) { - if (e.isDefaultPrevented && e.isDefaultPrevented.name === 'returnTrue') { - return; - } - - var text = $(this).find('input, textarea').not('input[type="checkbox"], input[type="radio"], input[type="hidden"]'); - var checkbox = $(this).find('input[type="checkbox"]'); - var 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', function (e) { - $(this).parent().addClass('active'); - }); - doc.on('blur', '.list-section-item input, .list-section-item select, .list-section-item textarea', function (e) { - $(this).parent().removeClass('active'); - }); - }; - - UtilsService.prototype.getDomain = function (uriString) { - if (!uriString) { - return null; - } - - uriString = uriString.trim(); - if (uriString === '') { - return null; - } - - if (uriString.startsWith('http://') || uriString.startsWith('https://')) { - try { - var url = new URL(uriString); - if (!url || !url.hostname) { - return null; - } - - if (url.hostname === 'localhost' || validIpAddress(url.hostname)) { - return url.hostname; - } - - if (tldjs) { - var domain = tldjs.getDomain(uriString); - if (domain) { - return domain; - } - } - - return url.hostname; - } - catch (e) { - return null; - } - } - else if (tldjs) { - var domain2 = tldjs.getDomain(uriString); - if (domain2) { - return domain2; - } - } - - return null; - }; - - UtilsService.prototype.getHostname = function (uriString) { - if (!uriString) { - return null; - } - - uriString = uriString.trim(); - if (uriString === '') { - return null; - } - - if (uriString.startsWith('http://') || uriString.startsWith('https://')) { - try { - var url = new URL(uriString); - if (!url || !url.hostname) { - return null; - } - - return url.hostname; - } - catch (e) { - return null; - } - } - - return null; - }; - - UtilsService.prototype.copyToClipboard = function (text, doc) { - doc = doc || document; - if (window.clipboardData && window.clipboardData.setData) { - // IE specific code path to prevent textarea being shown while dialog is visible. - return clipboardData.setData('Text', text); - } - else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) { - var 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) { - console.warn('Copy to clipboard failed.', ex); - return false; - } - finally { - doc.body.removeChild(textarea); - } - } - }; - - UtilsService.prototype.inSidebar = function (theWindow) { - return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=sidebar') > -1; - }; - - UtilsService.prototype.inTab = function (theWindow) { - return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=tab') > -1; - }; - - UtilsService.prototype.inPopout = function (theWindow) { - return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=popout') > -1; - }; - - UtilsService.prototype.inPopup = function (theWindow) { - return theWindow.location.search === '' || theWindow.location.search.indexOf('uilocation=') === -1 || - theWindow.location.search.indexOf('uilocation=popup') > -1; - }; - - UtilsService.prototype.saveObjToStorage = function (key, obj) { - var deferred = Q.defer(); - var storedObj = {}; - storedObj[key] = obj; - chrome.storage.local.set(storedObj, function () { - deferred.resolve(); - }); - return deferred.promise; - }; - - UtilsService.prototype.removeFromStorage = function (key) { - var deferred = Q.defer(); - chrome.storage.local.remove(key, function () { - deferred.resolve(); - }); - return deferred.promise; - }; - - UtilsService.prototype.getObjFromStorage = function (key) { - var deferred = Q.defer(); - chrome.storage.local.get(key, function (obj) { - if (obj && obj[key]) { - deferred.resolve(obj[key]); - } - else { - deferred.resolve(null); - } - }); - return deferred.promise; - }; - - function validIpAddress(ipString) { - var 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); - } -}