diff --git a/src/classes/ABTest.js b/src/classes/ABTest.js index 76b380256..33974463c 100644 --- a/src/classes/ABTest.js +++ b/src/classes/ABTest.js @@ -19,7 +19,12 @@ import globals from './Globals'; import { getJson } from '../utils/utils'; import { log } from '../utils/common'; -const { BROWSER_INFO, CMP_BASE_URL, EXTENSION_VERSION } = globals; +const { + BROWSER_INFO_READY, + BROWSER_INFO, + CMP_BASE_URL, + EXTENSION_VERSION +} = globals; /** Helper class for handling A/B tests. * @memberof BackgroundClasses @@ -54,33 +59,34 @@ class ABTest { fetch(irDebugOverride) { log('A/B Tests: fetching...'); - const URL = ABTest._buildURL(irDebugOverride); - - return getJson(URL).then((data) => { - if (data && Array.isArray(data)) { - log('A/B Tests: fetched', JSON.stringify(data)); - this._updateTests(data); - log('A/B Tests: tests updated to', this.getTests()); - } else { - log('A/B Tests: no tests found.'); - } - }).catch(() => { - log('A/B Tests: error fetching.'); - }); + return ABTest._buildURL(irDebugOverride) + .then(url => getJson(url)) + .then((data) => { + if (data && Array.isArray(data)) { + log('A/B Tests: fetched', JSON.stringify(data)); + this._updateTests(data); + log('A/B Tests: tests updated to', this.getTests()); + } else { + log('A/B Tests: no tests found.'); + } + }).catch(() => { + log('A/B Tests: error fetching.'); + }); } silentFetch(ir) { - const URL = ABTest._buildURL(ir); - - return getJson(URL).then((data) => { - if (data && Array.isArray(data)) { - this._updateTests(data); - } - return 'resolved'; - }).catch(() => 'rejected'); + return ABTest._buildURL(ir) + .then(url => getJson(url)) + .then((data) => { + if (data && Array.isArray(data)) { + this._updateTests(data); + } + return 'resolved'; + }).catch(() => 'rejected'); } - static _buildURL(ir) { + static async _buildURL(ir) { + await BROWSER_INFO_READY; return (`${CMP_BASE_URL}/abtestcheck ?os=${encodeURIComponent(BROWSER_INFO.os)} &install_date=${encodeURIComponent(conf.install_date)} diff --git a/src/classes/CMP.js b/src/classes/CMP.js index 88b683a30..35f5da09f 100644 --- a/src/classes/CMP.js +++ b/src/classes/CMP.js @@ -16,7 +16,12 @@ import globals from './Globals'; import { getJson } from '../utils/utils'; import { log } from '../utils/common'; -const { BROWSER_INFO, CMP_BASE_URL, EXTENSION_VERSION } = globals; +const { + BROWSER_INFO_READY, + BROWSER_INFO, + CMP_BASE_URL, + EXTENSION_VERSION +} = globals; /** * Class for handling notification and/or marketing campaigns. @@ -36,27 +41,26 @@ class CMP { return Promise.resolve(false); } - const URL = CMP._buildUrl(); - - return getJson(URL).then((data) => { - if (CMP._isNewData(data)) { - this._updateCampaigns(data); - return this.CMP_DATA; - } - // getJson() returned a 204, meaning no new campaigns available - log('No CMP data to fetch at this time'); - globals.SESSION.cmp_data = []; - return false; - }).catch((err) => { - log('Error in fetchCMPData', err); - return false; - }); + return CMP._buildUrl() + .then(url => getJson(url)) + .then((data) => { + if (CMP._isNewData(data)) { + this._updateCampaigns(data); + return this.CMP_DATA; + } + // getJson() returned a 204, meaning no new campaigns available + log('No CMP data to fetch at this time'); + globals.SESSION.cmp_data = []; + return false; + }).catch((err) => { + log('Error in fetchCMPData', err); + return false; + }); } debugFetch() { - const URL = CMP._buildUrl(); - - return getJson(URL) + return CMP._buildUrl() + .then(url => getJson(url)) .then((data) => { if (CMP._isNewData(data)) { this._updateCampaigns(data); @@ -86,7 +90,8 @@ class CMP { this.CMP_DATA = data.Campaigns; } - static _buildUrl() { + static async _buildUrl() { + await BROWSER_INFO_READY; return (`${CMP_BASE_URL}/check ?os=${encodeURIComponent(BROWSER_INFO.os)} &hw=${encodeURIComponent(conf.enable_human_web ? '1' : '0')}