Skip to content

Commit

Permalink
support apple store
Browse files Browse the repository at this point in the history
  • Loading branch information
JessNah committed May 22, 2021
1 parent 9779d1f commit 667900f
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 38 deletions.
5 changes: 5 additions & 0 deletions Extension/src/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ chrome.runtime.onInstalled.addListener(() => {
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(tab.url && tab.url.includes(SupportedSitesUrls[SupportedSites.INSTACART])){
setCurrentStore(SupportedSites.INSTACART);
} else if(tab.url && tab.url.includes(SupportedSitesUrls[SupportedSites.APPLESTORE])){
setCurrentStore(SupportedSites.APPLESTORE);
if(tab.url && tab.url.includes("apple.com/ca/shop/bag")){
chrome.tabs.sendMessage(tab.id, Messages.ENABLE_CHECKOUT_STATE);
}
}
});

Expand Down
5 changes: 3 additions & 2 deletions Extension/src/components/CheckoutDialog/CheckoutDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { Component } from 'react'
import "./CheckoutDialog.scss"
import { Tearsheet } from "@carbon/ibm-cloud-cognitive"
import { getInstaCart, getCartAverageRating } from "../../stores/getInstaCart"
import { getCartAverageRating, getCart } from "../../stores/storesCommon"
import { Accordion, AccordionItem, Tag, TagTypeName } from "carbon-components-react"

interface CheckoutDialogProps {
closeCheckoutDialog: () => void;
currentStore: string;
}

interface CheckoutDialogState {
Expand Down Expand Up @@ -58,7 +59,7 @@ class CheckoutDialog extends Component<CheckoutDialogProps, CheckoutDialogState>
}

render() {
const cart = getInstaCart();
const cart = getCart(this.props.currentStore);
const cartRank = getCartAverageRating(cart);
console.log(cart);
return (
Expand Down
4 changes: 3 additions & 1 deletion Extension/src/containers/CheckoutContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface CheckoutContainerProps {
isPermissionGranted: boolean;
setPermission: () => void;
endCheckoutSequence: () => void;
currentStore: string;
}

interface CheckoutContainerState {
Expand Down Expand Up @@ -55,7 +56,8 @@ class CheckoutContainer extends Component<CheckoutContainerProps, CheckoutContai
message={"Take2 is analyzing your cart...\n Thanks for trying your best to make smart choices."}
/>
: <CheckoutDialog
closeCheckoutDialog={() => {this.props.endCheckoutSequence()}}
currentStore={this.props.currentStore}
closeCheckoutDialog={() => {this.props.endCheckoutSequence()}}
/>
)
}
Expand Down
3 changes: 3 additions & 0 deletions Extension/src/contentScript/contentScript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class App extends Component<AppProps, AppState> {
}
}
return;
case SupportedSites.APPLESTORE:
return;
default:
return;
}
Expand Down Expand Up @@ -141,6 +143,7 @@ class App extends Component<AppProps, AppState> {
{ (this.state.checkoutState &&
this.state.startScenarioSquence) ?
<CheckoutContainer
currentStore={this.state.currentStore}
isPermissionGranted={this.state.isPermissionGranted}
setPermission={() => {this.setState({isPermissionGranted: true})}}
endCheckoutSequence={() => {this.setState({startScenarioSquence: false})}}
Expand Down
2 changes: 1 addition & 1 deletion Extension/src/static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"content_scripts": [
{
"matches": ["https://www.instacart.com/store/*"],
"matches": ["https://www.instacart.com/store/*", "https://www.apple.com/ca/shop/*"],
"js": ["contentScript.js"]
}
]
Expand Down
20 changes: 20 additions & 0 deletions Extension/src/stores/getAppleCart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { analyzeCart } from "./storesCommon"

export function getAppleCart() {
const _cartItems = document.getElementsByClassName("row rs-iteminfo");
let cart = [];
for(let i = 0; i < _cartItems.length; i++){
const priceItem = _cartItems[i].getElementsByClassName("rs-iteminfo-price");
const textItem = _cartItems[i].getElementsByClassName("rs-iteminfo-title");
const imageItem = _cartItems[i].getElementsByClassName("as-util-relatedlink")[0].childNodes[0];
let _item = {
id: textItem[0].childNodes[0].innerText,
name: textItem[0].childNodes[0].innerText,
price: priceItem[0].childNodes[0].childNodes[1].innerText,
image: imageItem
}
cart.push(_item);
}
cart = analyzeCart(cart);
return cart;
}
36 changes: 4 additions & 32 deletions Extension/src/stores/getInstaCart.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { analyzeCart } from "./storesCommon"

export function getInstaCart() {
// const _html = document.all[0].outerHTML;
const _cart = document.getElementsByClassName("cart-container-inner")[0];
const _cartItems = _cart.getElementsByClassName("clamped-name");
const _cartImages = _cart.getElementsByTagName("img");
const _cartItems = _cart ? _cart.getElementsByClassName("clamped-name") : [];
const _cartImages = _cart ? _cart.getElementsByTagName("img") : [];

let cart = [];
let imageIndex = 0;
Expand All @@ -21,34 +23,4 @@ export function getInstaCart() {
}
cart = analyzeCart(cart);
return cart;
}

export function analyzeCart(cart) {
for(let item of cart){
item.score = Math.floor(Math.random() * 10) + 1;
if(item.score > 10){
item.score = 10;
}
}
cart.sort(compare);
return cart;
}

function compare( a, b ) {
if ( a.score < b.score ){
return 1;
}
if ( a.score > b.score ){
return -1;
}
return 0;
}

export function getCartAverageRating(cart) {
let score = 0;
for(let item of cart){
score += item.score;
}
console.log(score);
return score/cart.length;
}
46 changes: 46 additions & 0 deletions Extension/src/stores/storesCommon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { getInstaCart } from "./getInstaCart"
import { getAppleCart } from "./getAppleCart"
import { SupportedSites } from "../utils/constants"

export function getCart(currentStore) {
switch(currentStore){
case SupportedSites.INSTACART:
return getInstaCart();
case SupportedSites.APPLESTORE:
return getAppleCart();
default:
return [];
}
}

export function analyzeCart(cart) {
for(let item of cart){
item.score = Math.floor(Math.random() * 10) + 1;
if(item.score > 10){
item.score = 10;
}
}
cart.sort(compare);
return cart;
}

export function compare( a, b ) {
if ( a.score < b.score ){
return 1;
}
if ( a.score > b.score ){
return -1;
}
return 0;
}

export function getCartAverageRating(cart) {
let score = 0;
for(let item of cart){
score += item.score;
}
if(cart.length <= 0 ){
return 0;
}
return score/cart.length;
}
6 changes: 4 additions & 2 deletions Extension/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ export enum Messages {
}

export const SupportedSites = {
INSTACART: "INSTACART"
INSTACART: "INSTACART",
APPLESTORE: "APPLESTORE"
}

export const SupportedSitesUrls = {
[SupportedSites.INSTACART]: "instacart.com"
[SupportedSites.INSTACART]: "instacart.com",
[SupportedSites.APPLESTORE]: "apple.com"
}

0 comments on commit 667900f

Please sign in to comment.