From 2dc074449e0bb8185d798c306054f8d7739363bc Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Fri, 1 May 2020 14:19:41 -0400 Subject: [PATCH 01/11] Add checkout web to matches for account_pages content script in manifest --- manifest.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index c3e8075bd..b61a24f8f 100644 --- a/manifest.json +++ b/manifest.json @@ -51,7 +51,9 @@ ], "matches": [ "https://account.ghostery.com/*", - "https://account.ghosterystage.com/*" + "https://account.ghosterystage.com/*", + "https://checkout.ghostery.com/*", + "https://checkout.ghosterystage.com/*", ], "run_at": "document_start" }, From c93a42ff708e6cfc8187bd68de3dcfef7072f09d Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Wed, 6 May 2020 13:39:07 -0400 Subject: [PATCH 02/11] Create separate checkout_pages content script using account_pages script as template. Add basic DEV manifest flag support --- app/content-scripts/account_pages.js | 5 ++- app/content-scripts/checkout_pages.js | 52 +++++++++++++++++++++++++++ manifest.json | 16 +++++++-- src/background.js | 25 +++++++++++++ src/classes/Globals.js | 22 ++++++++---- webpack.config.js | 1 + 6 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 app/content-scripts/checkout_pages.js diff --git a/app/content-scripts/account_pages.js b/app/content-scripts/account_pages.js index 32a0a0465..22e057cb7 100644 --- a/app/content-scripts/account_pages.js +++ b/app/content-scripts/account_pages.js @@ -1,8 +1,7 @@ /** - * Ghostery Account & ExtensionWeb Events + * Ghostery Account Events * - * This file connects the extension to all ExtensionWeb and Account - * pages (extension, account, signon) + * This file connects the extension to all Account pages * * Ghostery Browser Extension * https://www.ghostery.com/ diff --git a/app/content-scripts/checkout_pages.js b/app/content-scripts/checkout_pages.js new file mode 100644 index 000000000..c4d0c86a8 --- /dev/null +++ b/app/content-scripts/checkout_pages.js @@ -0,0 +1,52 @@ +/** + * Ghostery Checkout Events + * + * This file connects the extension to all Checkout pages + * + * Ghostery Browser Extension + * https://www.ghostery.com/ + * + * Copyright 2019 Ghostery, Inc. All rights reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0 + */ +/** + * @namespace CheckoutPagesContentScript + */ +import msgModule from './utils/msg'; + +const msg = msgModule('checkout_pages'); +const { sendMessage } = msg; +/** + * Use to call init to initialize functionality + * @var {Object} initialized to an object with init method as its property + */ +const CheckoutPagesContentScript = (function(window) { + /** + * Initialize functionality of this script. + * @memberOf CheckoutPagesContentScript + * @package + */ + const _listeners = [ + 'checkoutPage.buyPlus', + 'checkoutPage.ping', + ]; + const _initialize = function() { + _listeners.forEach(name => window.addEventListener(name, () => sendMessage(name))); + }; + + return { + /** + * Initialize functionality of this script. + * @memberOf CheckoutPagesContentScript + * @public + */ + init() { + _initialize(); + } + }; +}(window, document)); + +CheckoutPagesContentScript.init(); diff --git a/manifest.json b/manifest.json index b61a24f8f..127c531e2 100644 --- a/manifest.json +++ b/manifest.json @@ -9,6 +9,7 @@ "description": "__MSG_short_description__", "debug": true, "log": true, + "dev": true, "icons": { "16": "app/images/icon16.png", "48": "app/images/icon48.png", @@ -51,12 +52,21 @@ ], "matches": [ "https://account.ghostery.com/*", - "https://account.ghosterystage.com/*", - "https://checkout.ghostery.com/*", - "https://checkout.ghosterystage.com/*", + "https://account.ghosterystage.com/*" ], "run_at": "document_start" }, + { + "all_frames": false, + "js": [ + "dist/checkout_pages.js" + ], + "matches": [ + "https://checkout.ghostery.com/*", + "https://checkout.ghosterystage.com/*" + ], + "run_at": "document_start" + }, { "all_frames": true, "js": [ diff --git a/src/background.js b/src/background.js index e4fbc0e53..b6d93e4c5 100644 --- a/src/background.js +++ b/src/background.js @@ -317,6 +317,27 @@ function handleAccountPages(name, callback) { } } +/** + * Handle messages sent from app/js/checkout_pages.js content script. + * @memberOf Background + * + * @param {string} name message name + * @param {string} tab_url tab url + */ +function handleCheckoutPages(name, callback) { + switch (name) { + case 'checkoutPage.buyPlus': + // Update account info + console.error('background#handleCheckoutPages received "checkoutPage.buyPlus"'); + return true; + case 'checkoutPage.ping': + console.error('ping received from Checkout Web'); + return true; + default: + return false; + } +} + /** * Handle messages sent from dist/ghostery_dot_com.js content script. * @memberOf Background @@ -672,6 +693,10 @@ function onMessageHandler(request, sender, callback) { // Account pages return handleAccountPages(name, callback); } + if (origin === 'checkout_pages') { + // Checkout pages + return handleCheckoutPages(name, callback); + } if (origin === 'purplebox') { // Purplebox script events return handlePurplebox(name, message, tab_id, callback); diff --git a/src/classes/Globals.js b/src/classes/Globals.js index 0d8894f93..bd8777a1f 100644 --- a/src/classes/Globals.js +++ b/src/classes/Globals.js @@ -27,6 +27,7 @@ class Globals { // environment variables this.DEBUG = manifest.debug || false; this.LOG = this.DEBUG && manifest.log; + this.DEV = manifest.dev || false; this.EXTENSION_NAME = manifest.name || 'Ghostery'; this.EXTENSION_VERSION = manifest.version_name || manifest.version; // Firefox does not support "version_name" this.BROWSER_INFO = { @@ -51,17 +52,26 @@ class Globals { // domains this.GHOSTERY_ROOT_DOMAIN = `${this.DEBUG ? 'ghosterystage' : 'ghostery'}.com`; this.GHOSTERY_BASE_URL = `https://${this.GHOSTERY_ROOT_DOMAIN}`; - this.ACCOUNT_BASE_URL = `https://account.${this.GHOSTERY_ROOT_DOMAIN}`; - this.CHECKOUT_BASE_URL = `https://checkout.${this.GHOSTERY_ROOT_DOMAIN}`; this.METRICS_BASE_URL = `https://${this.DEBUG ? 'staging-d' : 'd'}.ghostery.com`; this.CMP_BASE_URL = `https://${this.DEBUG ? 'staging-cmp-cdn' : 'cmp-cdn'}.ghostery.com`; this.CDN_BASE_URL = `https://${this.DEBUG ? 'staging-cdn' : 'cdn'}.ghostery.com`; this.APPS_BASE_URL = `https://${this.DEBUG ? 'staging-apps' : 'apps'}.ghostery.com`; this.GCACHE_BASE_URL = `https://${this.DEBUG ? 'staging-gcache' : 'gcache'}.ghostery.com`; - this.AUTH_SERVER = `https://consumerapi.${this.GHOSTERY_ROOT_DOMAIN}`; - this.ACCOUNT_SERVER = `https://accountapi.${this.GHOSTERY_ROOT_DOMAIN}`; - this.COOKIE_DOMAIN = `.${this.GHOSTERY_ROOT_DOMAIN}`; - this.COOKIE_URL = this.GHOSTERY_BASE_URL; + if (this.DEV) { + this.ACCOUNT_BASE_URL = 'http://localhost:3001'; + this.CHECKOUT_BASE_URL = 'http://localhost:3002'; + this.AUTH_SERVER = 'http://localhost:8080'; + this.ACCOUNT_SERVER = 'http://localhost:8081'; + this.COOKIE_DOMAIN = 'localhost'; + this.COOKIE_URL = `http://${this.COOKIE_DOMAIN}`; + } else { + this.ACCOUNT_BASE_URL = `https://account.${this.GHOSTERY_ROOT_DOMAIN}`; + this.CHECKOUT_BASE_URL = `https://checkout.${this.GHOSTERY_ROOT_DOMAIN}`; + this.AUTH_SERVER = `https://consumerapi.${this.GHOSTERY_ROOT_DOMAIN}`; + this.ACCOUNT_SERVER = `https://accountapi.${this.GHOSTERY_ROOT_DOMAIN}`; + this.COOKIE_DOMAIN = `.${this.GHOSTERY_ROOT_DOMAIN}`; + this.COOKIE_URL = this.GHOSTERY_BASE_URL; + } // extension IDs this.GHOSTERY_TAB_CHROME_PRODUCTION_ID = 'plmapebanmikcofllaaddgeocahboejc'; diff --git a/webpack.config.js b/webpack.config.js index 17d53f522..6a379a862 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -47,6 +47,7 @@ module.exports = { account_pages: [`${CONTENT_SCRIPTS_DIR}/account_pages.js`], background: [`${SRC_DIR}/background.js`], blocked_redirect: [`${CONTENT_SCRIPTS_DIR}/blocked_redirect.js`], + checkout_pages: [`${CONTENT_SCRIPTS_DIR}/checkout_pages.js`], click_to_play: [`${CONTENT_SCRIPTS_DIR}/click_to_play.js`], content_script_bundle: [`${CONTENT_SCRIPTS_DIR}/content_script_bundle.js`], ghostery_dot_com: [`${CONTENT_SCRIPTS_DIR}/ghostery_dot_com.js`], From 5d53c82c95e298a13581c97e295201d173287a30 Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Mon, 11 May 2020 18:07:36 -0400 Subject: [PATCH 03/11] Listen for additional events in checkout_pages content script. Flesh out handling of these events in background#handleCheckoutPages --- app/content-scripts/checkout_pages.js | 5 ++++- manifest.json | 6 ++++-- src/background.js | 27 ++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/content-scripts/checkout_pages.js b/app/content-scripts/checkout_pages.js index c4d0c86a8..45551ac19 100644 --- a/app/content-scripts/checkout_pages.js +++ b/app/content-scripts/checkout_pages.js @@ -30,8 +30,11 @@ const CheckoutPagesContentScript = (function(window) { * @package */ const _listeners = [ + 'checkoutPages.buyInsights', 'checkoutPage.buyPlus', - 'checkoutPage.ping', + 'checkoutPage.buyPremium', + 'checkoutPage.login', + 'checkoutPage.register', ]; const _initialize = function() { _listeners.forEach(name => window.addEventListener(name, () => sendMessage(name))); diff --git a/manifest.json b/manifest.json index 127c531e2..ef4fb3d68 100644 --- a/manifest.json +++ b/manifest.json @@ -52,7 +52,8 @@ ], "matches": [ "https://account.ghostery.com/*", - "https://account.ghosterystage.com/*" + "https://account.ghosterystage.com/*", + "http://localhost:3001/*" ], "run_at": "document_start" }, @@ -63,7 +64,8 @@ ], "matches": [ "https://checkout.ghostery.com/*", - "https://checkout.ghosterystage.com/*" + "https://checkout.ghosterystage.com/*", + "http://localhost:3002/*" ], "run_at": "document_start" }, diff --git a/src/background.js b/src/background.js index b6d93e4c5..7b060e937 100644 --- a/src/background.js +++ b/src/background.js @@ -325,16 +325,33 @@ function handleAccountPages(name, callback) { * @param {string} tab_url tab url */ function handleCheckoutPages(name, callback) { + console.error('checkoutPage event:'); + console.error(name); + switch (name) { + case 'checkoutPage.buyInsights': case 'checkoutPage.buyPlus': - // Update account info - console.error('background#handleCheckoutPages received "checkoutPage.buyPlus"'); + case 'checkoutPage.buyPremium': + account.getUser() + .then(account.getUserSubscriptionData) + .catch((err) => { + log('handleCheckoutPages error', err); + }); return true; - case 'checkoutPage.ping': - console.error('ping received from Checkout Web'); + case 'checkoutPage.login': + account.getUser() + .then(account.getUserSettings) + // account.getUserSettings will reject if user email is not validated + .catch(err => log('handleCheckoutPages error', err)) + .then(account.getUserSubscriptionData) + // The user may not be a subscriber + .catch(err => log('handleCheckoutPages error', err)); + return true; + case 'checkoutPage.register': + account.getUser(); return true; default: - return false; + break; } } From 4ffce227dc33dd9f34ce90bd4200f99f4970f884 Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Mon, 18 May 2020 12:00:55 -0400 Subject: [PATCH 04/11] Remove debug statement --- src/background.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/background.js b/src/background.js index 7b060e937..a8c08edef 100644 --- a/src/background.js +++ b/src/background.js @@ -322,12 +322,8 @@ function handleAccountPages(name, callback) { * @memberOf Background * * @param {string} name message name - * @param {string} tab_url tab url */ -function handleCheckoutPages(name, callback) { - console.error('checkoutPage event:'); - console.error(name); - +function handleCheckoutPages(name) { switch (name) { case 'checkoutPage.buyInsights': case 'checkoutPage.buyPlus': From cbb53bff951278d91af137fcfe851c4ae474cb08 Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Wed, 20 May 2020 21:12:56 -0400 Subject: [PATCH 05/11] Update manifest --- manifest.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/manifest.json b/manifest.json index ef4fb3d68..15271f242 100644 --- a/manifest.json +++ b/manifest.json @@ -7,9 +7,6 @@ "version_name": "8.5.0", "default_locale": "en", "description": "__MSG_short_description__", - "debug": true, - "log": true, - "dev": true, "icons": { "16": "app/images/icon16.png", "48": "app/images/icon48.png", From 3bf68466c78f9dbca5c3f9df1557a77f2271533e Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Wed, 20 May 2020 21:50:31 -0400 Subject: [PATCH 06/11] Remove changes used for local development --- manifest.json | 6 ++---- src/classes/Globals.js | 22 ++++++---------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/manifest.json b/manifest.json index 15271f242..aa38e1545 100644 --- a/manifest.json +++ b/manifest.json @@ -49,8 +49,7 @@ ], "matches": [ "https://account.ghostery.com/*", - "https://account.ghosterystage.com/*", - "http://localhost:3001/*" + "https://account.ghosterystage.com/*" ], "run_at": "document_start" }, @@ -61,8 +60,7 @@ ], "matches": [ "https://checkout.ghostery.com/*", - "https://checkout.ghosterystage.com/*", - "http://localhost:3002/*" + "https://checkout.ghosterystage.com/*" ], "run_at": "document_start" }, diff --git a/src/classes/Globals.js b/src/classes/Globals.js index bd8777a1f..0d8894f93 100644 --- a/src/classes/Globals.js +++ b/src/classes/Globals.js @@ -27,7 +27,6 @@ class Globals { // environment variables this.DEBUG = manifest.debug || false; this.LOG = this.DEBUG && manifest.log; - this.DEV = manifest.dev || false; this.EXTENSION_NAME = manifest.name || 'Ghostery'; this.EXTENSION_VERSION = manifest.version_name || manifest.version; // Firefox does not support "version_name" this.BROWSER_INFO = { @@ -52,26 +51,17 @@ class Globals { // domains this.GHOSTERY_ROOT_DOMAIN = `${this.DEBUG ? 'ghosterystage' : 'ghostery'}.com`; this.GHOSTERY_BASE_URL = `https://${this.GHOSTERY_ROOT_DOMAIN}`; + this.ACCOUNT_BASE_URL = `https://account.${this.GHOSTERY_ROOT_DOMAIN}`; + this.CHECKOUT_BASE_URL = `https://checkout.${this.GHOSTERY_ROOT_DOMAIN}`; this.METRICS_BASE_URL = `https://${this.DEBUG ? 'staging-d' : 'd'}.ghostery.com`; this.CMP_BASE_URL = `https://${this.DEBUG ? 'staging-cmp-cdn' : 'cmp-cdn'}.ghostery.com`; this.CDN_BASE_URL = `https://${this.DEBUG ? 'staging-cdn' : 'cdn'}.ghostery.com`; this.APPS_BASE_URL = `https://${this.DEBUG ? 'staging-apps' : 'apps'}.ghostery.com`; this.GCACHE_BASE_URL = `https://${this.DEBUG ? 'staging-gcache' : 'gcache'}.ghostery.com`; - if (this.DEV) { - this.ACCOUNT_BASE_URL = 'http://localhost:3001'; - this.CHECKOUT_BASE_URL = 'http://localhost:3002'; - this.AUTH_SERVER = 'http://localhost:8080'; - this.ACCOUNT_SERVER = 'http://localhost:8081'; - this.COOKIE_DOMAIN = 'localhost'; - this.COOKIE_URL = `http://${this.COOKIE_DOMAIN}`; - } else { - this.ACCOUNT_BASE_URL = `https://account.${this.GHOSTERY_ROOT_DOMAIN}`; - this.CHECKOUT_BASE_URL = `https://checkout.${this.GHOSTERY_ROOT_DOMAIN}`; - this.AUTH_SERVER = `https://consumerapi.${this.GHOSTERY_ROOT_DOMAIN}`; - this.ACCOUNT_SERVER = `https://accountapi.${this.GHOSTERY_ROOT_DOMAIN}`; - this.COOKIE_DOMAIN = `.${this.GHOSTERY_ROOT_DOMAIN}`; - this.COOKIE_URL = this.GHOSTERY_BASE_URL; - } + this.AUTH_SERVER = `https://consumerapi.${this.GHOSTERY_ROOT_DOMAIN}`; + this.ACCOUNT_SERVER = `https://accountapi.${this.GHOSTERY_ROOT_DOMAIN}`; + this.COOKIE_DOMAIN = `.${this.GHOSTERY_ROOT_DOMAIN}`; + this.COOKIE_URL = this.GHOSTERY_BASE_URL; // extension IDs this.GHOSTERY_TAB_CHROME_PRODUCTION_ID = 'plmapebanmikcofllaaddgeocahboejc'; From 9033585ffb6c52316c295dddb366dbc24ce055f7 Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Wed, 20 May 2020 21:52:42 -0400 Subject: [PATCH 07/11] Fix typo --- app/content-scripts/checkout_pages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/content-scripts/checkout_pages.js b/app/content-scripts/checkout_pages.js index 45551ac19..dcdd4b6f5 100644 --- a/app/content-scripts/checkout_pages.js +++ b/app/content-scripts/checkout_pages.js @@ -30,7 +30,7 @@ const CheckoutPagesContentScript = (function(window) { * @package */ const _listeners = [ - 'checkoutPages.buyInsights', + 'checkoutPage.buyInsights', 'checkoutPage.buyPlus', 'checkoutPage.buyPremium', 'checkoutPage.login', From 56f0709493733de3b0c1cfb2e064873fc24f6065 Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Wed, 3 Jun 2020 20:43:51 -0400 Subject: [PATCH 08/11] Refactor account and promo modal message handling code in background to make it easier to read debug and test --- src/background.js | 382 +++++++++++++++++++++++++++------------------- 1 file changed, 222 insertions(+), 160 deletions(-) diff --git a/src/background.js b/src/background.js index 2fbb7472d..74a589ce3 100644 --- a/src/background.js +++ b/src/background.js @@ -781,16 +781,6 @@ function onMessageHandler(request, sender, callback) { callback(); return false; } - if (name === 'account.getTheme') { - if (conf.current_theme !== 'default') { - account.getTheme(conf.current_theme).then(() => { - callback(conf.account.themeData[conf.current_theme]); - }); - return true; - } - callback(); - return false; - } if (name === 'getCliqzModuleData') { // panel-android only utils.getActiveTab((tab) => { sendCliqzModuleCounts(tab.id, tab.pageHost, callback); @@ -804,144 +794,6 @@ function onMessageHandler(request, sender, callback) { }); return true; } - if (name === 'account.login') { - metrics.ping('sign_in'); - const { email, password } = message; - account.login(email, password) - .then((response) => { - if (!response.hasOwnProperty('errors')) { - metrics.ping('sign_in_success'); - } - callback(response); - }) - .catch((err) => { - log('LOGIN ERROR', err); - callback({ errors: _getJSONAPIErrorsObject(err) }); - }); - return true; - } - if (name === 'account.register') { - const { - email, confirmEmail, password, firstName, lastName - } = message; - account.register(email, confirmEmail, password, firstName, lastName) - .then((response) => { - if (!response.hasOwnProperty('errors')) { - metrics.ping('create_account_success'); - } - callback(response); - }) - .catch((err) => { - callback({ errors: [err] }); - log('REGISTER ERROR', err); - }); - return true; - } - if (name === 'account.logout') { - account.logout() - .then((response) => { - callback(response); - }) - .catch((err) => { - log('LOGOUT ERROR', err); - callback(err); - }); - return true; - } - if (name === 'account.getUserSettings') { - account.getUserSettings() - .then((settings) => { - callback(settings); - }) - .catch((err) => { - log('Error getting user settings:', err); - callback({ errors: _getJSONAPIErrorsObject(err) }); - }); - return true; - } - if (name === 'account.getUserSubscriptionData') { - account.getUserSubscriptionData() - .then((subscriptions) => { - // Return highest tier subscription from array - const premiumSubscription = subscriptions.find(subscription => subscription.productName.includes('Ghostery Premium')); - if (premiumSubscription) { - callback({ subscriptionData: premiumSubscription }); - return; - } - - const plusSubscription = subscriptions.find(subscription => subscription.productName.includes('Ghostery Plus')); - if (plusSubscription) { - callback({ subscriptionData: plusSubscription }); - return; - } - - callback({}); - }) - .catch((err) => { - log('Error getting user subscription data:', err); - callback({ errors: _getJSONAPIErrorsObject(err) }); - }); - return true; - } - if (name === 'account.openSubscriptionPage') { - utils.openNewTab({ url: `${globals.ACCOUNT_BASE_URL}/subscription`, become_active: true }); - return false; - } - if (name === 'account.openCheckoutPage') { - let url = `${globals.CHECKOUT_BASE_URL}/plus`; - const { utm } = message || null; - if (utm) { - url += `?utm_source=${utm.utm_source}&utm_campaign=${utm.utm_campaign}`; - } - utils.openNewTab({ url, become_active: true }); - return false; - } - if (name === 'account.openSupportPage') { - metrics.ping('priority_support_submit'); - const subscriber = account.hasScopesUnverified(['subscriptions:plus']); - const tabUrl = subscriber ? `${globals.ACCOUNT_BASE_URL}/support` : 'https://www.ghostery.com/support/'; - utils.openNewTab({ url: tabUrl, become_active: true }); - return false; - } - if (name === 'account.resetPassword') { - const { email } = message; - account.resetPassword(email) - .then((success) => { - callback(success); - }) - .catch((err) => { - callback({ errors: _getJSONAPIErrorsObject(err) }); - log('RESET PASSWORD ERROR', err); - }); - return true; - } - if (name === 'account.getUser') { - account.getUser(message) - .then((user) => { - if (user) { - user.plusAccess = account.hasScopesUnverified(['subscriptions:plus']) - || account.hasScopesUnverified(['subscriptions:premium']); - user.premiumAccess = account.hasScopesUnverified(['subscriptions:premium']); - } - callback({ user }); - }) - .catch((err) => { - callback({ errors: _getJSONAPIErrorsObject(err) }); - log('FETCH USER ERROR', err); - }); - return true; - } - if (name === 'account.sendValidateAccountEmail') { - account.sendValidateAccountEmail() - .then((success) => { - callback(success); - }) - .catch((err) => { - callback({ errors: _getJSONAPIErrorsObject(err) }); - log('sendValidateAccountEmail error', err); - }); - return true; - } if (name === 'update_database') { checkLibraryVersion().then((result) => { callback(result); @@ -1018,22 +870,232 @@ function onMessageHandler(request, sender, callback) { }); return true; } - if (name === 'promoModals.sawPremiumPromo') { - promoModals.recordPremiumPromoSighting(); - return false; + + if (name && name.startsWith('account.')) { + return handleAccountMessage(name.split('.')[1], message, callback); } - if (name === 'promoModals.sawInsightsPromo') { - promoModals.recordInsightsPromoSighting(); - return false; + + if (name && name.startsWith('promoModals.')) { + return handlePromoModalMessage(name.split('.')[1]); } - if (name === 'promoModals.sawPlusPromo') { - promoModals.recordPlusPromoSighting(); - return false; +} + +function hamGetTheme(callback) { + if (conf.current_theme !== 'default') { + account.getTheme(conf.current_theme) + .then(() => { + callback(conf.account.themeData[conf.current_theme]); + }); + return true; } - if (name === 'promoModals.turnOffPromos') { - promoModals.turnOffPromos(); - return false; + callback(); + return false; +} + +function hamGetUser(payload, callback) { + account.getUser(payload) + .then((user) => { + if (user) { + user.plusAccess = ( + account.hasScopesUnverified(['subscriptions:plus']) + || account.hasScopesUnverified(['subscriptions:premium']) + ); + user.premiumAccess = account.hasScopesUnverified(['subscriptions:premium']); + } + callback({ user }); + }) + .catch((err) => { + callback({ errors: _getJSONAPIErrorsObject(err) }); + log('FETCH USER ERROR', err); + }); + return true; +} + +function hamGetUserSettings(callback) { + account.getUserSettings() + .then((settings) => { + callback(settings); + }) + .catch((err) => { + log('Error getting user settings:', err); + callback({ errors: _getJSONAPIErrorsObject(err) }); + }); + return true; +} + +function hamGetUserSubscriptionData(callback) { + account.getUserSubscriptionData() + .then((subscriptions) => { + // Return highest tier subscription from array + const premiumSubscription = subscriptions.find(subscription => subscription.productName.includes('Ghostery Premium')); + if (premiumSubscription) { + callback({ subscriptionData: premiumSubscription }); + return; + } + + const plusSubscription = subscriptions.find(subscription => subscription.productName.includes('Ghostery Plus')); + if (plusSubscription) { + callback({ subscriptionData: plusSubscription }); + return; + } + + callback({}); + }) + .catch((err) => { + log('Error getting user subscription data:', err); + callback({ errors: _getJSONAPIErrorsObject(err) }); + }); + return true; +} + +function hamLogin(msgData, callback) { + const { email, password } = msgData; + + account.login(email, password) + .then((response) => { + if (!response.hasOwnProperty('errors')) { + metrics.ping('sign_in_success'); + } + callback(response); + }) + .catch((err) => { + log('LOGIN ERROR', err); + callback({ errors: _getJSONAPIErrorsObject(err) }); + }); + + metrics.ping('sign_in'); + + return true; +} + +function hamLogout(callback) { + account.logout() + .then((response) => { + callback(response); + }) + .catch((err) => { + log('LOGOUT ERROR', err); + callback(err); + }); + return true; +} + +function hamOpenCheckoutPage(utm) { + let url = `${globals.CHECKOUT_BASE_URL}/plus`; + if (utm) { + const { utm_source, utm_campaign } = utm; + url += `?utm_source=${utm_source}&utm_campaign=${utm_campaign}`; } + utils.openNewTab({ url, become_active: true }); + return false; +} + +function hamOpenSubscriptionPage() { + utils.openNewTab({ url: `${globals.ACCOUNT_BASE_URL}/subscription`, become_active: true }); + return false; +} + +function hamOpenSupportPage() { + const subscriber = account.hasScopesUnverified(['subscriptions:plus']); + const tabUrl = subscriber ? `${globals.ACCOUNT_BASE_URL}/support` : 'https://www.ghostery.com/support/'; + utils.openNewTab({ url: tabUrl, become_active: true }); + + metrics.ping('priority_support_submit'); + + return false; +} + +function hamRegister(userDetails, callback) { + const { + email, + confirmEmail, + password, + firstName, + lastName, + } = userDetails; + + account.register(email, confirmEmail, password, firstName, lastName) + .then((response) => { + if (!response.hasOwnProperty('errors')) { + metrics.ping('create_account_success'); + } + callback(response); + }) + .catch((err) => { + callback({ errors: [err] }); + log('REGISTER ERROR', err); + }); + + return true; +} + +function hamResetPassword(email, callback) { + // TODO check args + account.resetPassword(email) + .then((success) => { + callback(success); + }) + .catch((err) => { + callback({ errors: _getJSONAPIErrorsObject(err) }); + log('RESET PASSWORD ERROR', err); + }); + return true; +} + +function hamValidateAccountEmail(callback) { + account.sendValidateAccountEmail() + .then((success) => { + callback(success); + }) + .catch((err) => { + callback({ errors: _getJSONAPIErrorsObject(err) }); + log('sendValidateAccountEmail error', err); + }); + + return true; +} + +/** + * Handle messages related to user accounts + * @memberOf Background + * + * @param {string} name the name of the message sent by the calling script + * @param {Object} payload the data of the message sent by the calling script + * @param {function} callback function to call (at most once) when you have a response + * @return {boolean} denotes async (true) or sync (false) + */ +function handleAccountMessage(name, payload, callback) { + // TODO improve arg names : ? msgName, msgBody ? + switch (name) { + case 'getTheme': return hamGetTheme(callback); + case 'getUser': return hamGetUser(payload, callback); + case 'getUserSettings': return hamGetUserSettings(callback); + case 'getUserSubscriptionData': return hamGetUserSubscriptionData(callback); + case 'login': return hamLogin(payload, callback); + case 'logout': return hamLogout(callback); + case 'openCheckoutPage': return hamOpenCheckoutPage(payload.utm || null); + case 'openSubscriptionPage': return hamOpenSubscriptionPage(); + case 'openSupportPage': return hamOpenSupportPage(); + case 'register': return hamRegister(payload, callback); + case 'resetPassword': return hamResetPassword(payload.email, callback); + case 'sendValidateAccountEmail': return hamValidateAccountEmail(callback); + default: return false; + } +} + +/** + * Handle messages related to promo modals + * @memberOf Background + * + * @param {string} name the message name + * @return {boolean} denotes async (true) or sync (false) + */ +function handlePromoModalMessage(name) { + if (name === 'sawPremiumPromo') promoModals.recordPremiumPromoSighting(); + else if (name === 'sawInsightsPromo') promoModals.recordInsightsPromoSighting(); + else if (name === 'sawPlusPromos') promoModals.recordPlusPromoSighting(); + else if (name === 'turnOffPromos') promoModals.turnOffPromos(); + return false; } /** From 7bd3dec7c6d0d8bd2377cadfc764a1b473712400 Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Wed, 3 Jun 2020 20:45:55 -0400 Subject: [PATCH 09/11] Add some TODO comments in background --- src/background.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/background.js b/src/background.js index 74a589ce3..00886c995 100644 --- a/src/background.js +++ b/src/background.js @@ -880,6 +880,7 @@ function onMessageHandler(request, sender, callback) { } } +// TODO add function description function hamGetTheme(callback) { if (conf.current_theme !== 'default') { account.getTheme(conf.current_theme) @@ -892,6 +893,7 @@ function hamGetTheme(callback) { return false; } +// TODO add function description function hamGetUser(payload, callback) { account.getUser(payload) .then((user) => { @@ -911,6 +913,7 @@ function hamGetUser(payload, callback) { return true; } +// TODO add function description function hamGetUserSettings(callback) { account.getUserSettings() .then((settings) => { @@ -923,6 +926,7 @@ function hamGetUserSettings(callback) { return true; } +// TODO add function description function hamGetUserSubscriptionData(callback) { account.getUserSubscriptionData() .then((subscriptions) => { @@ -948,6 +952,7 @@ function hamGetUserSubscriptionData(callback) { return true; } +// TODO add function description function hamLogin(msgData, callback) { const { email, password } = msgData; @@ -968,6 +973,7 @@ function hamLogin(msgData, callback) { return true; } +// TODO add function description function hamLogout(callback) { account.logout() .then((response) => { @@ -980,6 +986,7 @@ function hamLogout(callback) { return true; } +// TODO add function description function hamOpenCheckoutPage(utm) { let url = `${globals.CHECKOUT_BASE_URL}/plus`; if (utm) { @@ -990,11 +997,13 @@ function hamOpenCheckoutPage(utm) { return false; } +// TODO add function description function hamOpenSubscriptionPage() { utils.openNewTab({ url: `${globals.ACCOUNT_BASE_URL}/subscription`, become_active: true }); return false; } +// TODO add function description function hamOpenSupportPage() { const subscriber = account.hasScopesUnverified(['subscriptions:plus']); const tabUrl = subscriber ? `${globals.ACCOUNT_BASE_URL}/support` : 'https://www.ghostery.com/support/'; @@ -1005,6 +1014,7 @@ function hamOpenSupportPage() { return false; } +// TODO add function description function hamRegister(userDetails, callback) { const { email, @@ -1029,6 +1039,7 @@ function hamRegister(userDetails, callback) { return true; } +// TODO add function description function hamResetPassword(email, callback) { // TODO check args account.resetPassword(email) @@ -1042,6 +1053,7 @@ function hamResetPassword(email, callback) { return true; } +// TODO add function description function hamValidateAccountEmail(callback) { account.sendValidateAccountEmail() .then((success) => { From cf1ff703d308d089e1039a32d2d921f578a2d56f Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Tue, 9 Jun 2020 00:33:39 -0400 Subject: [PATCH 10/11] Reverting changes to background.js accidentally made to wrong branch --- src/background.js | 394 +++++++++++++++++++--------------------------- 1 file changed, 160 insertions(+), 234 deletions(-) diff --git a/src/background.js b/src/background.js index 00886c995..2fbb7472d 100644 --- a/src/background.js +++ b/src/background.js @@ -781,6 +781,16 @@ function onMessageHandler(request, sender, callback) { callback(); return false; } + if (name === 'account.getTheme') { + if (conf.current_theme !== 'default') { + account.getTheme(conf.current_theme).then(() => { + callback(conf.account.themeData[conf.current_theme]); + }); + return true; + } + callback(); + return false; + } if (name === 'getCliqzModuleData') { // panel-android only utils.getActiveTab((tab) => { sendCliqzModuleCounts(tab.id, tab.pageHost, callback); @@ -794,6 +804,144 @@ function onMessageHandler(request, sender, callback) { }); return true; } + if (name === 'account.login') { + metrics.ping('sign_in'); + const { email, password } = message; + account.login(email, password) + .then((response) => { + if (!response.hasOwnProperty('errors')) { + metrics.ping('sign_in_success'); + } + callback(response); + }) + .catch((err) => { + log('LOGIN ERROR', err); + callback({ errors: _getJSONAPIErrorsObject(err) }); + }); + return true; + } + if (name === 'account.register') { + const { + email, confirmEmail, password, firstName, lastName + } = message; + account.register(email, confirmEmail, password, firstName, lastName) + .then((response) => { + if (!response.hasOwnProperty('errors')) { + metrics.ping('create_account_success'); + } + callback(response); + }) + .catch((err) => { + callback({ errors: [err] }); + log('REGISTER ERROR', err); + }); + return true; + } + if (name === 'account.logout') { + account.logout() + .then((response) => { + callback(response); + }) + .catch((err) => { + log('LOGOUT ERROR', err); + callback(err); + }); + return true; + } + if (name === 'account.getUserSettings') { + account.getUserSettings() + .then((settings) => { + callback(settings); + }) + .catch((err) => { + log('Error getting user settings:', err); + callback({ errors: _getJSONAPIErrorsObject(err) }); + }); + return true; + } + if (name === 'account.getUserSubscriptionData') { + account.getUserSubscriptionData() + .then((subscriptions) => { + // Return highest tier subscription from array + const premiumSubscription = subscriptions.find(subscription => subscription.productName.includes('Ghostery Premium')); + if (premiumSubscription) { + callback({ subscriptionData: premiumSubscription }); + return; + } + + const plusSubscription = subscriptions.find(subscription => subscription.productName.includes('Ghostery Plus')); + if (plusSubscription) { + callback({ subscriptionData: plusSubscription }); + return; + } + + callback({}); + }) + .catch((err) => { + log('Error getting user subscription data:', err); + callback({ errors: _getJSONAPIErrorsObject(err) }); + }); + return true; + } + if (name === 'account.openSubscriptionPage') { + utils.openNewTab({ url: `${globals.ACCOUNT_BASE_URL}/subscription`, become_active: true }); + return false; + } + if (name === 'account.openCheckoutPage') { + let url = `${globals.CHECKOUT_BASE_URL}/plus`; + const { utm } = message || null; + if (utm) { + url += `?utm_source=${utm.utm_source}&utm_campaign=${utm.utm_campaign}`; + } + utils.openNewTab({ url, become_active: true }); + return false; + } + if (name === 'account.openSupportPage') { + metrics.ping('priority_support_submit'); + const subscriber = account.hasScopesUnverified(['subscriptions:plus']); + const tabUrl = subscriber ? `${globals.ACCOUNT_BASE_URL}/support` : 'https://www.ghostery.com/support/'; + utils.openNewTab({ url: tabUrl, become_active: true }); + return false; + } + if (name === 'account.resetPassword') { + const { email } = message; + account.resetPassword(email) + .then((success) => { + callback(success); + }) + .catch((err) => { + callback({ errors: _getJSONAPIErrorsObject(err) }); + log('RESET PASSWORD ERROR', err); + }); + return true; + } + if (name === 'account.getUser') { + account.getUser(message) + .then((user) => { + if (user) { + user.plusAccess = account.hasScopesUnverified(['subscriptions:plus']) + || account.hasScopesUnverified(['subscriptions:premium']); + user.premiumAccess = account.hasScopesUnverified(['subscriptions:premium']); + } + callback({ user }); + }) + .catch((err) => { + callback({ errors: _getJSONAPIErrorsObject(err) }); + log('FETCH USER ERROR', err); + }); + return true; + } + if (name === 'account.sendValidateAccountEmail') { + account.sendValidateAccountEmail() + .then((success) => { + callback(success); + }) + .catch((err) => { + callback({ errors: _getJSONAPIErrorsObject(err) }); + log('sendValidateAccountEmail error', err); + }); + return true; + } if (name === 'update_database') { checkLibraryVersion().then((result) => { callback(result); @@ -870,246 +1018,24 @@ function onMessageHandler(request, sender, callback) { }); return true; } - - if (name && name.startsWith('account.')) { - return handleAccountMessage(name.split('.')[1], message, callback); - } - - if (name && name.startsWith('promoModals.')) { - return handlePromoModalMessage(name.split('.')[1]); + if (name === 'promoModals.sawPremiumPromo') { + promoModals.recordPremiumPromoSighting(); + return false; } -} - -// TODO add function description -function hamGetTheme(callback) { - if (conf.current_theme !== 'default') { - account.getTheme(conf.current_theme) - .then(() => { - callback(conf.account.themeData[conf.current_theme]); - }); - return true; + if (name === 'promoModals.sawInsightsPromo') { + promoModals.recordInsightsPromoSighting(); + return false; } - callback(); - return false; -} - -// TODO add function description -function hamGetUser(payload, callback) { - account.getUser(payload) - .then((user) => { - if (user) { - user.plusAccess = ( - account.hasScopesUnverified(['subscriptions:plus']) - || account.hasScopesUnverified(['subscriptions:premium']) - ); - user.premiumAccess = account.hasScopesUnverified(['subscriptions:premium']); - } - callback({ user }); - }) - .catch((err) => { - callback({ errors: _getJSONAPIErrorsObject(err) }); - log('FETCH USER ERROR', err); - }); - return true; -} - -// TODO add function description -function hamGetUserSettings(callback) { - account.getUserSettings() - .then((settings) => { - callback(settings); - }) - .catch((err) => { - log('Error getting user settings:', err); - callback({ errors: _getJSONAPIErrorsObject(err) }); - }); - return true; -} - -// TODO add function description -function hamGetUserSubscriptionData(callback) { - account.getUserSubscriptionData() - .then((subscriptions) => { - // Return highest tier subscription from array - const premiumSubscription = subscriptions.find(subscription => subscription.productName.includes('Ghostery Premium')); - if (premiumSubscription) { - callback({ subscriptionData: premiumSubscription }); - return; - } - - const plusSubscription = subscriptions.find(subscription => subscription.productName.includes('Ghostery Plus')); - if (plusSubscription) { - callback({ subscriptionData: plusSubscription }); - return; - } - - callback({}); - }) - .catch((err) => { - log('Error getting user subscription data:', err); - callback({ errors: _getJSONAPIErrorsObject(err) }); - }); - return true; -} - -// TODO add function description -function hamLogin(msgData, callback) { - const { email, password } = msgData; - - account.login(email, password) - .then((response) => { - if (!response.hasOwnProperty('errors')) { - metrics.ping('sign_in_success'); - } - callback(response); - }) - .catch((err) => { - log('LOGIN ERROR', err); - callback({ errors: _getJSONAPIErrorsObject(err) }); - }); - - metrics.ping('sign_in'); - - return true; -} - -// TODO add function description -function hamLogout(callback) { - account.logout() - .then((response) => { - callback(response); - }) - .catch((err) => { - log('LOGOUT ERROR', err); - callback(err); - }); - return true; -} - -// TODO add function description -function hamOpenCheckoutPage(utm) { - let url = `${globals.CHECKOUT_BASE_URL}/plus`; - if (utm) { - const { utm_source, utm_campaign } = utm; - url += `?utm_source=${utm_source}&utm_campaign=${utm_campaign}`; + if (name === 'promoModals.sawPlusPromo') { + promoModals.recordPlusPromoSighting(); + return false; } - utils.openNewTab({ url, become_active: true }); - return false; -} - -// TODO add function description -function hamOpenSubscriptionPage() { - utils.openNewTab({ url: `${globals.ACCOUNT_BASE_URL}/subscription`, become_active: true }); - return false; -} - -// TODO add function description -function hamOpenSupportPage() { - const subscriber = account.hasScopesUnverified(['subscriptions:plus']); - const tabUrl = subscriber ? `${globals.ACCOUNT_BASE_URL}/support` : 'https://www.ghostery.com/support/'; - utils.openNewTab({ url: tabUrl, become_active: true }); - - metrics.ping('priority_support_submit'); - - return false; -} - -// TODO add function description -function hamRegister(userDetails, callback) { - const { - email, - confirmEmail, - password, - firstName, - lastName, - } = userDetails; - - account.register(email, confirmEmail, password, firstName, lastName) - .then((response) => { - if (!response.hasOwnProperty('errors')) { - metrics.ping('create_account_success'); - } - callback(response); - }) - .catch((err) => { - callback({ errors: [err] }); - log('REGISTER ERROR', err); - }); - - return true; -} - -// TODO add function description -function hamResetPassword(email, callback) { - // TODO check args - account.resetPassword(email) - .then((success) => { - callback(success); - }) - .catch((err) => { - callback({ errors: _getJSONAPIErrorsObject(err) }); - log('RESET PASSWORD ERROR', err); - }); - return true; -} - -// TODO add function description -function hamValidateAccountEmail(callback) { - account.sendValidateAccountEmail() - .then((success) => { - callback(success); - }) - .catch((err) => { - callback({ errors: _getJSONAPIErrorsObject(err) }); - log('sendValidateAccountEmail error', err); - }); - - return true; -} - -/** - * Handle messages related to user accounts - * @memberOf Background - * - * @param {string} name the name of the message sent by the calling script - * @param {Object} payload the data of the message sent by the calling script - * @param {function} callback function to call (at most once) when you have a response - * @return {boolean} denotes async (true) or sync (false) - */ -function handleAccountMessage(name, payload, callback) { - // TODO improve arg names : ? msgName, msgBody ? - switch (name) { - case 'getTheme': return hamGetTheme(callback); - case 'getUser': return hamGetUser(payload, callback); - case 'getUserSettings': return hamGetUserSettings(callback); - case 'getUserSubscriptionData': return hamGetUserSubscriptionData(callback); - case 'login': return hamLogin(payload, callback); - case 'logout': return hamLogout(callback); - case 'openCheckoutPage': return hamOpenCheckoutPage(payload.utm || null); - case 'openSubscriptionPage': return hamOpenSubscriptionPage(); - case 'openSupportPage': return hamOpenSupportPage(); - case 'register': return hamRegister(payload, callback); - case 'resetPassword': return hamResetPassword(payload.email, callback); - case 'sendValidateAccountEmail': return hamValidateAccountEmail(callback); - default: return false; + if (name === 'promoModals.turnOffPromos') { + promoModals.turnOffPromos(); + return false; } } -/** - * Handle messages related to promo modals - * @memberOf Background - * - * @param {string} name the message name - * @return {boolean} denotes async (true) or sync (false) - */ -function handlePromoModalMessage(name) { - if (name === 'sawPremiumPromo') promoModals.recordPremiumPromoSighting(); - else if (name === 'sawInsightsPromo') promoModals.recordInsightsPromoSighting(); - else if (name === 'sawPlusPromos') promoModals.recordPlusPromoSighting(); - else if (name === 'turnOffPromos') promoModals.turnOffPromos(); - return false; -} - /** * Initialize Dispatcher Events. * All Conf properties trigger a dispatcher pub event From 966db467bdec319c0666fdd488da017a2c29162c Mon Sep 17 00:00:00 2001 From: wlycdgr Date: Mon, 15 Jun 2020 14:10:15 -0400 Subject: [PATCH 11/11] Appease linter --- src/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/background.js b/src/background.js index 00838105a..fb8292e65 100644 --- a/src/background.js +++ b/src/background.js @@ -349,7 +349,7 @@ function handleCheckoutPages(name) { account.getUser(); return true; default: - break; + return false; } }