Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-2033 Update GBE account state in response to checkout-web events #555

Merged
merged 16 commits into from Jun 15, 2020
Merged
Changes from all commits
Commits
File filter
Filter file types
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -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/
@@ -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();
@@ -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"
]
}
}
@@ -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);
@@ -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`],
ProTip! Use n and p to navigate between commits in a pull request.