diff options
Diffstat (limited to 'ext/mixed/js')
-rw-r--r-- | ext/mixed/js/core.js | 83 |
1 files changed, 51 insertions, 32 deletions
diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 513d6211..8a8a2368 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -17,27 +17,11 @@ */ -// toIterable is required on Edge for cross-window origin objects. -function toIterable(value) { - if (typeof Symbol !== 'undefined' && typeof value[Symbol.iterator] !== 'undefined') { - return value; - } - - if (value !== null && typeof value === 'object') { - const length = value.length; - if (typeof length === 'number' && Number.isFinite(length)) { - const array = []; - for (let i = 0; i < length; ++i) { - array.push(value[i]); - } - return array; - } - } - - throw new Error('Could not convert to iterable'); -} +/* + * Extension information + */ -function extensionHasChrome() { +function _extensionHasChrome() { try { return typeof chrome === 'object' && chrome !== null; } catch (e) { @@ -45,7 +29,7 @@ function extensionHasChrome() { } } -function extensionHasBrowser() { +function _extensionHasBrowser() { try { return typeof browser === 'object' && browser !== null; } catch (e) { @@ -53,6 +37,21 @@ function extensionHasBrowser() { } } +const EXTENSION_IS_BROWSER_EDGE = ( + _extensionHasBrowser() && + (!_extensionHasChrome() || (typeof chrome.runtime === 'undefined' && typeof browser.runtime !== 'undefined')) +); + +if (EXTENSION_IS_BROWSER_EDGE) { + // Edge does not have chrome defined. + chrome = browser; +} + + +/* + * Error handling + */ + function errorToJson(error) { return { name: error.name, @@ -86,16 +85,40 @@ function logError(error, alert) { } } -const EXTENSION_IS_BROWSER_EDGE = ( - extensionHasBrowser() && - (!extensionHasChrome() || (typeof chrome.runtime === 'undefined' && typeof browser.runtime !== 'undefined')) -); -if (EXTENSION_IS_BROWSER_EDGE) { - // Edge does not have chrome defined. - chrome = browser; +/* + * Common helpers + */ + +function isObject(value) { + return typeof value === 'object' && value !== null && !Array.isArray(value); } +// toIterable is required on Edge for cross-window origin objects. +function toIterable(value) { + if (typeof Symbol !== 'undefined' && typeof value[Symbol.iterator] !== 'undefined') { + return value; + } + + if (value !== null && typeof value === 'object') { + const length = value.length; + if (typeof length === 'number' && Number.isFinite(length)) { + const array = []; + for (let i = 0; i < length; ++i) { + array.push(value[i]); + } + return array; + } + } + + throw new Error('Could not convert to iterable'); +} + + +/* + * Async utilities + */ + function promiseTimeout(delay, resolveValue) { if (delay <= 0) { return Promise.resolve(resolveValue); @@ -148,7 +171,3 @@ function stringReplaceAsync(str, regex, replacer) { parts.push(str.substring(index)); return Promise.all(parts).then(v => v.join('')); } - -function isObject(value) { - return typeof value === 'object' && value !== null && !Array.isArray(value); -} |