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..dcdd4b6f5 --- /dev/null +++ b/app/content-scripts/checkout_pages.js @@ -0,0 +1,55 @@ +/** + * 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.buyInsights', + 'checkoutPage.buyPlus', + 'checkoutPage.buyPremium', + 'checkoutPage.login', + 'checkoutPage.register', + ]; + 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 271ef52db..cecac8abe 100644 --- a/manifest.json +++ b/manifest.json @@ -53,6 +53,17 @@ ], "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": [ @@ -103,4 +114,4 @@ "cliqz/offers-templates/checkout.html", "cliqz/offers-templates/control-center.html" ] -} \ No newline at end of file +} diff --git a/src/background.js b/src/background.js index bf9b25ecb..a7f34aa05 100644 --- a/src/background.js +++ b/src/background.js @@ -319,6 +319,40 @@ function handleAccountPages(name, callback) { } } +/** + * Handle messages sent from app/js/checkout_pages.js content script. + * @memberOf Background + * + * @param {string} name message name + */ +function handleCheckoutPages(name) { + switch (name) { + case 'checkoutPage.buyInsights': + case 'checkoutPage.buyPlus': + case 'checkoutPage.buyPremium': + account.getUser() + .then(account.getUserSubscriptionData) + .catch((err) => { + log('handleCheckoutPages error', err); + }); + return true; + 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; + } +} + /** * Handle messages sent from dist/ghostery_dot_com.js content script. * @memberOf Background @@ -677,6 +711,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/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`],