├── .gitignore ├── public ├── zomato16.png ├── zomato24.png ├── zomato32.png ├── zomato48.png ├── zomato128.png ├── loading-buffering.gif ├── popup.html └── manifest.json ├── readme-pics ├── z1.png ├── z2.png └── z3.png ├── src ├── background.ts ├── __tests__ │ └── sum.ts ├── content_script.tsx ├── options.tsx ├── popup.scss ├── popup.tsx └── api.ts ├── jest.config.js ├── webpack ├── webpack.prod.js ├── webpack.dev.js └── webpack.common.js ├── .vscode ├── settings.json └── tasks.json ├── tsconfig.json ├── LICENSE ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | npm-debug.log 2 | node_modules/ 3 | dist/ 4 | tmp/ 5 | package-lock.json -------------------------------------------------------------------------------- /public/zomato16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpgupt/zomato-spending-calculator/HEAD/public/zomato16.png -------------------------------------------------------------------------------- /public/zomato24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpgupt/zomato-spending-calculator/HEAD/public/zomato24.png -------------------------------------------------------------------------------- /public/zomato32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpgupt/zomato-spending-calculator/HEAD/public/zomato32.png -------------------------------------------------------------------------------- /public/zomato48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpgupt/zomato-spending-calculator/HEAD/public/zomato48.png -------------------------------------------------------------------------------- /readme-pics/z1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpgupt/zomato-spending-calculator/HEAD/readme-pics/z1.png -------------------------------------------------------------------------------- /readme-pics/z2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpgupt/zomato-spending-calculator/HEAD/readme-pics/z2.png -------------------------------------------------------------------------------- /readme-pics/z3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpgupt/zomato-spending-calculator/HEAD/readme-pics/z3.png -------------------------------------------------------------------------------- /public/zomato128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpgupt/zomato-spending-calculator/HEAD/public/zomato128.png -------------------------------------------------------------------------------- /public/loading-buffering.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpgupt/zomato-spending-calculator/HEAD/public/loading-buffering.gif -------------------------------------------------------------------------------- /src/background.ts: -------------------------------------------------------------------------------- 1 | // function polling() { 2 | // // console.log("polling"); 3 | // setTimeout(polling, 1000 * 30); 4 | // } 5 | 6 | // polling(); 7 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "roots": [ 3 | "src" 4 | ], 5 | "transform": { 6 | "^.+\\.ts$": "ts-jest" 7 | }, 8 | }; 9 | -------------------------------------------------------------------------------- /webpack/webpack.prod.js: -------------------------------------------------------------------------------- 1 | const { merge } = require('webpack-merge'); 2 | const common = require('./webpack.common.js'); 3 | 4 | module.exports = merge(common, { 5 | mode: 'production' 6 | }); -------------------------------------------------------------------------------- /webpack/webpack.dev.js: -------------------------------------------------------------------------------- 1 | const { merge } = require('webpack-merge'); 2 | const common = require('./webpack.common.js'); 3 | 4 | module.exports = merge(common, { 5 | devtool: 'inline-source-map', 6 | mode: 'development' 7 | }); -------------------------------------------------------------------------------- /src/__tests__/sum.ts: -------------------------------------------------------------------------------- 1 | // import { sum } from "../sum"; 2 | 3 | // test("1 + 1 = 2", () => { 4 | // expect(sum(1, 1)).toBe(2); 5 | // }); 6 | 7 | // test("1 + 2 != 2", () => { 8 | // expect(sum(1, 2)).not.toBe(2); 9 | // }); 10 | -------------------------------------------------------------------------------- /public/popup.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Zomato Spending Calculator - Popup 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsdk": "./node_modules/typescript/lib", 3 | "files.eol": "\n", 4 | "json.schemas": [ 5 | { 6 | "fileMatch": [ 7 | "/manifest.json" 8 | ], 9 | "url": "http://json.schemastore.org/chrome-manifest" 10 | } 11 | ] 12 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "strict": true, 4 | "module": "commonjs", 5 | "target": "es6", 6 | "esModuleInterop": true, 7 | "sourceMap": false, 8 | "rootDir": "src", 9 | "outDir": "dist/js", 10 | "noEmitOnError": true, 11 | "jsx": "react", 12 | "typeRoots": [ "node_modules/@types" ] 13 | } 14 | } -------------------------------------------------------------------------------- /src/content_script.tsx: -------------------------------------------------------------------------------- 1 | chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { 2 | if (msg.type === 'getAuthStatus') { 3 | let aTags = document.getElementsByTagName('a'); 4 | const loginSearchText = 'Log in'; 5 | const signupSearchText = 'Sign up'; 6 | const aTagsArray = Array.from(aTags); 7 | const loginTag = aTagsArray.filter(tag => tag.innerText.includes(loginSearchText)); 8 | const signupTag = aTagsArray.filter(tag => tag.innerText.includes(signupSearchText)); 9 | if (loginTag.length > 0 && signupTag.length > 0) { 10 | sendResponse({ 11 | isLoggedIn: false 12 | }); 13 | } 14 | else { 15 | sendResponse({ 16 | isLoggedIn: true 17 | }); 18 | } 19 | } 20 | }); 21 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "command": "npm", 6 | "tasks": [ 7 | { 8 | "label": "install", 9 | "type": "shell", 10 | "command": "npm", 11 | "args": ["install"] 12 | }, 13 | { 14 | "label": "update", 15 | "type": "shell", 16 | "command": "npm", 17 | "args": ["update"] 18 | }, 19 | { 20 | "label": "test", 21 | "type": "shell", 22 | "command": "npm", 23 | "args": ["run", "test"] 24 | }, 25 | { 26 | "label": "build", 27 | "type": "shell", 28 | "group": "build", 29 | "command": "npm", 30 | "args": ["run", "watch"] 31 | } 32 | ] 33 | } -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "manifest_version": 3, 3 | "name": "zomato-spending-calculator", 4 | "description": "View total amount (in rupees) you have spent till now while ordering from Zomato App", 5 | "version": "1.1.5", 6 | "action": { 7 | "default_icon": { 8 | "16": "zomato16.png", 9 | "24": "zomato24.png", 10 | "32": "zomato32.png", 11 | "48": "zomato48.png", 12 | "128": "zomato128.png" 13 | }, 14 | "default_popup": "popup.html" 15 | }, 16 | "icons": { 17 | "16": "zomato16.png", 18 | "24": "zomato24.png", 19 | "32": "zomato32.png", 20 | "48": "zomato48.png", 21 | "128": "zomato128.png" 22 | }, 23 | "content_scripts": [ 24 | { 25 | "matches": [ 26 | "*://*.zomato.com/*" 27 | ], 28 | "js": [ 29 | "js/vendor.js", 30 | "js/content_script.js" 31 | ] 32 | } 33 | ], 34 | "background": { 35 | "service_worker": "js/background.js" 36 | }, 37 | "permissions": [ 38 | "storage", 39 | "cookies" 40 | ], 41 | "host_permissions": [ 42 | "*://*.zomato.com/*" 43 | ] 44 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Harsh Prakash Gupta 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zomato-spending-calculator", 3 | "version": "1.1.5", 4 | "description": "View total amount (rupees) you have spent till now while ordering from Zomato App", 5 | "main": "index.js", 6 | "scripts": { 7 | "watch": "webpack --config webpack/webpack.dev.js --watch", 8 | "build": "webpack --config webpack/webpack.prod.js", 9 | "clean": "rimraf dist", 10 | "test": "npx jest", 11 | "style": "prettier --write \"src/**/*.{ts,tsx}\"" 12 | }, 13 | "author": "", 14 | "license": "MIT", 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/harshnitk17/zomato-spending-calculator.git" 18 | }, 19 | "dependencies": { 20 | "react": "^17.0.1", 21 | "react-dom": "^17.0.1" 22 | }, 23 | "devDependencies": { 24 | "@types/chrome": "0.0.158", 25 | "@types/jest": "^27.0.2", 26 | "@types/react": "^17.0.0", 27 | "@types/react-dom": "^17.0.0", 28 | "copy-webpack-plugin": "^9.0.1", 29 | "css-loader": "^6.7.1", 30 | "file-loader": "^6.2.0", 31 | "glob": "^7.1.6", 32 | "html-loader": "^3.1.0", 33 | "jest": "^27.2.1", 34 | "node-sass": "^7.0.1", 35 | "prettier": "^2.2.1", 36 | "rimraf": "^3.0.2 ", 37 | "sass-loader": "^12.6.0", 38 | "style-loader": "^3.3.1", 39 | "ts-jest": "^27.0.5", 40 | "ts-loader": "^8.0.0", 41 | "typescript": "^4.4.3 ", 42 | "webpack": "^5.0.0", 43 | "webpack-cli": "^4.0.0", 44 | "webpack-merge": "^5.0.0" 45 | } 46 | } -------------------------------------------------------------------------------- /src/options.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | 3 | export const Options = () => { 4 | const [dateOption, setDateOption] = useState(); 5 | const [status, setStatus] = useState(); 6 | 7 | useEffect(() => { 8 | // Restores select box and checkbox state using the preferences 9 | // stored in chrome.storage. 10 | chrome.storage.sync.get( 11 | { 12 | dateOption: "For All Time", 13 | monthLimit: 0, 14 | }, 15 | (items) => { 16 | setDateOption(items.dateOption); 17 | } 18 | ); 19 | }, []); 20 | 21 | const saveOptions = () => { 22 | // Saves options to chrome.storage.sync. 23 | chrome.storage.sync.set( 24 | { 25 | dateOption: dateOption 26 | }, 27 | () => { 28 | // Update status to let user know options were saved. 29 | setStatus("Options saved. Open extension again to see changes."); 30 | const id = setTimeout(() => { 31 | setStatus(""); 32 | window.close(); 33 | }, 2000); 34 | return () => clearTimeout(id); 35 | } 36 | ); 37 | }; 38 | 39 | return ( 40 | <> 41 |
42 |
43 | Calculate Spending For: 51 |
52 |
{status}
53 | 54 |
55 | 56 | ); 57 | }; 58 | 59 | -------------------------------------------------------------------------------- /webpack/webpack.common.js: -------------------------------------------------------------------------------- 1 | const webpack = require("webpack"); 2 | const path = require("path"); 3 | const CopyPlugin = require("copy-webpack-plugin"); 4 | const srcDir = path.join(__dirname, "..", "src"); 5 | 6 | module.exports = { 7 | entry: { 8 | popup: path.join(srcDir, 'popup.tsx'), 9 | options: path.join(srcDir, 'options.tsx'), 10 | background: path.join(srcDir, 'background.ts'), 11 | content_script: path.join(srcDir, 'content_script.tsx'), 12 | api: path.join(srcDir, 'api.ts'), 13 | }, 14 | output: { 15 | path: path.join(__dirname, "../dist/js"), 16 | filename: "[name].js", 17 | }, 18 | optimization: { 19 | splitChunks: { 20 | name: "vendor", 21 | chunks(chunk) { 22 | return chunk.name !== 'background'; 23 | } 24 | }, 25 | }, 26 | module: { 27 | rules: [ 28 | { 29 | test: /\.tsx?$/, 30 | use: "ts-loader", 31 | exclude: /node_modules/, 32 | }, 33 | { 34 | test: /\.scss$/, 35 | use: [{ 36 | loader: "style-loader" 37 | }, { 38 | loader: "css-loader" 39 | }, { 40 | loader: "sass-loader" 41 | }] 42 | }, 43 | { 44 | test: /\.(gif|png|jpe?g)$/, 45 | use: [ 46 | { 47 | loader: 'file-loader', 48 | options: { 49 | name: '[name].[ext]', 50 | outputPath: 'assets/', 51 | esModule: false, 52 | } 53 | } 54 | ] 55 | }, 56 | { 57 | test: /\.html$/, 58 | use: [ 59 | 'html-loader' 60 | ] 61 | } 62 | ], 63 | }, 64 | resolve: { 65 | extensions: [".ts", ".tsx", ".js"], 66 | }, 67 | plugins: [ 68 | new CopyPlugin({ 69 | patterns: [{ from: ".", to: "../", context: "public" }], 70 | options: {}, 71 | }), 72 | ], 73 | }; 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Zomato Spending Calculator - Chrome Extension 2 | 3 | Chrome Extension for calculating the total amount of money(in rupees) spent while ordering from Zomato App/Website. Extension has options for you to select for what time period you want to calculate the total amount of money spent. (e.g. This Month, This Year or the whole history). This extension may currently only work for Indian Zomato users. 4 | 5 | ## User Manual 6 | 7 | * This extension only works when the extension is being used with Zomato's Website Homepage Open and the user already should be signed in. Thus to use this extension, you have to first open Zomato's Homepage (https://www.zomato.com/) and then click on the extension icon in the top right corner of the page. You also have to sign in to your Zomato account before using this extension. 8 | * Once you have signed in, you can use the extension to calculate the total amount of money spent in the selected time period. You change time period by selecting appropriate option. 9 | 10 | ### Screenshots 11 | 12 | * When user is on different page than Zomato's Homepage, the extension will not work.
13 | ![Screenshot of Extension in different page than Zomato's Homepage](readme-pics/z1.png)
14 | * When user is on Zomato's Homepage, but not signed in , the extension will not work.
15 | ![Screenshot of Extension when user is not signed in](readme-pics/z2.png)
16 | * When user is on Zomato's Homepage and signed in, the extension will work.
17 | ![Screenshot of Extension when user is signed in](readme-pics/z3.png)
18 | 19 | # Development Setup 20 | 21 | ## Prerequisites 22 | 23 | * [node + npm](https://nodejs.org/) (Current Version) 24 | 25 | ## Option 26 | 27 | * [Visual Studio Code](https://code.visualstudio.com/) 28 | 29 | ## Includes the following 30 | 31 | * TypeScript 32 | * Webpack 33 | * React 34 | * Jest 35 | 36 | ## Setup 37 | 38 | ``` 39 | npm install 40 | ``` 41 | 42 | ## Import as Visual Studio Code project 43 | 44 | ... 45 | 46 | ## Build 47 | 48 | ``` 49 | npm run build 50 | ``` 51 | 52 | ## Build in watch mode 53 | 54 | ### terminal 55 | 56 | ``` 57 | npm run watch 58 | ``` 59 | 60 | ### Visual Studio Code 61 | 62 | Run watch mode. 63 | 64 | type `Ctrl + Shift + B` 65 | 66 | ## Load extension to chrome 67 | 68 | Load `dist` directory 69 | 70 | ## Test 71 | `npx jest` or `npm run test` 72 | -------------------------------------------------------------------------------- /src/popup.scss: -------------------------------------------------------------------------------- 1 | body { 2 | background: rgb(231, 149, 149); 3 | color: #000; 4 | min-height: 100px; 5 | min-width: 150px; 6 | 7 | .popup-body { 8 | .popup-header { 9 | text-align: center; 10 | padding: 5px; 11 | text-transform: capitalize; 12 | font-weight: bold; 13 | font-size: 1em; 14 | } 15 | 16 | .option-button { 17 | margin: 5px 5px; 18 | padding: 5px; 19 | } 20 | 21 | .option-button>button { 22 | padding: 5px; 23 | border: 1px solid #000; 24 | border-radius: 5px; 25 | font-size: 0.8em; 26 | cursor: pointer; 27 | 28 | &:hover { 29 | background: rgb(218, 184, 184); 30 | color: #000; 31 | } 32 | } 33 | 34 | .option-body { 35 | margin: 5px 5px; 36 | font-size: 0.9em; 37 | 38 | .date-option>select { 39 | border-radius: 1px solid #000; 40 | padding: 5px; 41 | margin-bottom: 5px; 42 | } 43 | 44 | .month-limit-option>input { 45 | border-radius: 1px solid #000; 46 | padding: 5px; 47 | margin-bottom: 5px; 48 | } 49 | 50 | .save-option { 51 | margin: 5px auto; 52 | text-align: center; 53 | font-weight: bold; 54 | border-radius: 1px solid #000; 55 | cursor: pointer; 56 | } 57 | } 58 | 59 | .info-body { 60 | margin: 5px 5px; 61 | font-size: 0.9em; 62 | 63 | .webpage-info { 64 | background-color: rgb(172, 177, 109); 65 | color: #000; 66 | padding: 5px; 67 | text-align: left; 68 | border-radius: 3px; 69 | margin-bottom: 5px; 70 | } 71 | 72 | .auth-info { 73 | background-color: rgb(105, 167, 185); 74 | color: #000; 75 | padding: 5px; 76 | text-align: left; 77 | border-radius: 3px; 78 | margin-bottom: 5px; 79 | } 80 | 81 | .error { 82 | background-color: rgb(248, 27, 27); 83 | color: white; 84 | padding: 5px; 85 | text-align: left; 86 | border-radius: 3px; 87 | margin-bottom: 5px; 88 | } 89 | 90 | .amount-info { 91 | background-color: rgb(113, 207, 187); 92 | color: #000; 93 | padding: 5px; 94 | text-align: left; 95 | border-radius: 3px; 96 | margin-bottom: 5px; 97 | } 98 | 99 | .webpage-redirect { 100 | background-color: rgb(214, 172, 108); 101 | color: #000; 102 | padding: 5px; 103 | text-align: left; 104 | border-radius: 3px; 105 | margin-bottom: 5px; 106 | } 107 | } 108 | 109 | } 110 | } -------------------------------------------------------------------------------- /src/popup.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | import ReactDOM from "react-dom"; 3 | import { makeApiCalls } from "./api"; 4 | import { Options } from "./options"; 5 | import "./popup.scss"; 6 | const spinner = require('../public/loading-buffering.gif'); 7 | 8 | interface costStruct { 9 | [key: string]: number; 10 | } 11 | 12 | const Popup = () => { 13 | const [currentURL, setCurrentURL] = useState(); 14 | const [isZomatoHomeOpen, setIsZomatoHomeOpen] = useState(false); 15 | const [isSignedIn, setIsSignedIn] = useState(false); 16 | const [totalCost, setTotalCost] = useState({}); 17 | const [isLoading, setIsLoading] = useState(false); 18 | const [isOptionsOpen, setIsOptionsOpen] = useState(false); 19 | const [isError, setIsError] = useState(false); 20 | 21 | useEffect(() => { 22 | chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { 23 | setCurrentURL(tabs[0].url); 24 | }); 25 | }, []); 26 | 27 | useEffect(() => { 28 | if (currentURL) { 29 | const url = new URL(currentURL); 30 | if (url.hostname === "www.zomato.com") { 31 | if (url.pathname === "/") { 32 | setIsZomatoHomeOpen(true); 33 | chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { 34 | const currentTab = tabs[0]; 35 | if (currentTab.id) { 36 | chrome.tabs.sendMessage(currentTab.id, { type: "getAuthStatus" }, (response) => { 37 | setIsSignedIn(response.isLoggedIn); 38 | }); 39 | getCookies(); 40 | } 41 | 42 | }); 43 | } 44 | } 45 | } 46 | 47 | }, [currentURL]); 48 | 49 | const getCookies = () => { 50 | chrome.cookies.getAll({ url: "https://www.zomato.com" }, async (cookies) => { 51 | setIsLoading(true); 52 | try { 53 | const results = await makeApiCalls(cookies); 54 | setTotalCost(results); 55 | setIsLoading(false); 56 | setIsError(false); 57 | } 58 | catch (err) { 59 | setIsLoading(false); 60 | setIsError(true); 61 | } 62 | }); 63 | } 64 | 65 | return ( 66 | <> 67 |
68 |
69 | Zomato Spending Calculator 70 |
71 |
72 | 73 |
74 | {isOptionsOpen && } 75 | {isZomatoHomeOpen ? (isSignedIn ? (
76 |

Zomato Home is open

77 |

You are currently Signed In to Zomato Website

78 | {isError &&

Error while fetching data

} 79 |

Total Amount Spent : 80 | {isLoading ? <>loading... (Fetching Data....) : renderAmount(totalCost)} 81 |

82 |
) : ( 83 |
84 |

Zomato Home is open

85 |

You are not signed in , please sign in to Zomato to continue.

86 |
) 87 | ) : ( 88 |
89 |

Zomato Homepage is not open

90 |

Open www.zomato.com on your browser, then use this extension

91 |
92 | )} 93 |
94 | 95 | ); 96 | }; 97 | 98 | ReactDOM.render( 99 | 100 | 101 | , 102 | document.getElementById("root") 103 | ); 104 | 105 | const renderAmount = (amount: costStruct) => { 106 | if (Object.keys(amount).length == 1) { 107 | return <> 108 | {Object.keys(amount)[0]} {amount[Object.keys(amount)[0]]} 109 | ; 110 | } 111 | else { 112 | return <> 113 |

Amount Spent in different Currencies

114 | {Object.keys(amount).map((key) => { 115 | return

{key} {amount[key]}

116 | })} 117 | } 118 | }; 119 | -------------------------------------------------------------------------------- /src/api.ts: -------------------------------------------------------------------------------- 1 | interface costStruct { 2 | [key: string]: number 3 | } 4 | 5 | export const makeApiCalls = async (cookies: chrome.cookies.Cookie[]) => { 6 | try { 7 | const cookieMap: { [name: string]: string } = {}; 8 | cookies.forEach(cookie => { 9 | cookieMap[cookie.name] = cookie.value; 10 | }); 11 | const myHeaders = new Headers(); 12 | myHeaders.append("authority", "www.zomato.com"); 13 | myHeaders.append("sec-ch-ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\""); 14 | myHeaders.append("x-zomato-csrft", cookieMap?.csrf); 15 | myHeaders.append("sec-ch-ua-mobile", "?1"); 16 | myHeaders.append("user-agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Mobile Safari/537.36"); 17 | myHeaders.append("sec-ch-ua-platform", "\"Android\""); 18 | myHeaders.append("accept", "*/*"); 19 | myHeaders.append("sec-fetch-site", "same-origin"); 20 | myHeaders.append("sec-fetch-mode", "cors"); 21 | myHeaders.append("sec-fetch-dest", "empty"); 22 | myHeaders.append("accept-language", "en-US,en;q=0.9"); 23 | myHeaders.append("cookie", `fbcity=${cookieMap?.fbcity}; fre=${cookieMap?.fre}; rd=${cookieMap?.rd}; zl=${cookieMap.zl}; fbtrack=${cookieMap?.fbtrack}; _ga=${cookieMap?._ga}; _gid=${cookieMap?._gid}; _gcl_au=${cookieMap?._gcl_au}; _fbp=${cookieMap?._fbp}; G_ENABLED_IDPS=${cookieMap?.G_ENABLED_IDPS}; zhli=${cookieMap?.zhli}; g_state=${cookieMap?.g_state}; ltv=${cookieMap?.ltv}; lty=${cookieMap?.lty}; locus=${cookieMap?.locus}; squeeze=${cookieMap?.squeeze}; orange=${cookieMap?.orange}; csrf=${cookieMap?.csrf}; PHPSESSID=${cookieMap?.PHPSESSID}; AWSALBTG=${cookieMap?.AWSALBTG}; AWSALBTGCORS=${cookieMap?.AWSALBTGCORS}; fre=${cookieMap?.fre}; rd=${cookieMap?.rd}; AWSALBTG=${cookieMap?.AWSALBTG}; AWSALBTGCORS=${cookieMap?.AWSALBTGCORS}`); 24 | 25 | const requestOptions: RequestInit = { 26 | method: 'GET', 27 | headers: myHeaders, 28 | redirect: 'follow' 29 | }; 30 | 31 | const results: costStruct = {}; 32 | const stopDate = await getStopDate(); 33 | 34 | let stop = false; 35 | 36 | for (let index = 0; ; index++) { 37 | // const response = await fetch(`https://www.zomato.com/webroutes/user/orders?page=${page}`, requestOptions); 38 | if (stop) { 39 | break; 40 | } 41 | if (index > 1000) { 42 | break; 43 | } 44 | const pages = []; let count = 1; 45 | while (count <= 10) { 46 | pages.push(index * 10 + count); 47 | count++; 48 | } 49 | 50 | const batchData = await Promise.all(pages.map(async (page): Promise<[boolean,costStruct]>=> { 51 | let total: costStruct = {}; 52 | const response = await fetchWrrapper(`https://www.zomato.com/webroutes/user/orders?page=${page}`, requestOptions); 53 | 54 | if (response === "") { 55 | return [false, {}]; 56 | } 57 | 58 | const data = JSON.parse(response)?.entities?.ORDER; 59 | if (JSON.stringify(data) === JSON.stringify([])) { 60 | stop = true; 61 | return [false, total]; 62 | } 63 | 64 | for (const key in data) { 65 | if (stopDate.getTime() > new Date().getTime()) { 66 | if (data[key].paymentStatus) { 67 | const [currency, amount] = separateCurrencyFromCost(data[key].totalCost); 68 | total[currency] = (total[currency] || 0) + parseFloat(amount); 69 | } 70 | } 71 | else { 72 | const value = data[key]; 73 | const orderDate = new Date(value.orderDate.split("at")[0].trim()); 74 | if (orderDate.getTime() < stopDate.getTime()) { 75 | stop = true; 76 | } 77 | else { 78 | if (data[key].paymentStatus) { 79 | const [currency, amount] = separateCurrencyFromCost(data[key].totalCost); 80 | total[currency] = (total[currency] || 0) + parseFloat(amount); 81 | } 82 | } 83 | } 84 | } 85 | 86 | return [true, total]; 87 | })); 88 | 89 | batchData.forEach(data => { 90 | const [status, total] = data; 91 | if (status) { 92 | for (const key in total) { 93 | results[key] = (results[key] || 0) + total[key]; 94 | } 95 | } 96 | }); 97 | } 98 | return results; 99 | } 100 | catch (e) { 101 | console.log(e); 102 | throw new Error("Error while making api calls to zomato"); 103 | } 104 | } 105 | 106 | const getStopDate = async () => { 107 | let dateOption: string; 108 | const items = await chrome.storage.sync.get("dateOption"); 109 | dateOption = items.dateOption; 110 | let stopDate: Date; 111 | const presentDay = new Date(); 112 | 113 | switch (dateOption) { 114 | case "For All Time": 115 | stopDate = new Date(presentDay.getFullYear(), presentDay.getMonth(), presentDay.getDate() + 1); 116 | break; 117 | case "This Month": 118 | stopDate = new Date(presentDay.getFullYear(), presentDay.getMonth(), 1); 119 | break; 120 | case "This Year": 121 | stopDate = new Date(presentDay.getFullYear(), 0, 1); 122 | break; 123 | default: 124 | stopDate = new Date(presentDay.getFullYear(), presentDay.getMonth(), presentDay.getDate() + 1); 125 | } 126 | 127 | return stopDate; 128 | } 129 | 130 | const fetchWrrapper = async (url: string, options: RequestInit) => { 131 | return fetch(url, options).then(response => { 132 | return response.text(); 133 | }).catch(_ => { 134 | return ""; 135 | }); 136 | } 137 | 138 | const separateCurrencyFromCost = (cost: string) => { 139 | const firstDigitIndex = cost.search(/\d/); 140 | const currency = cost.substring(0, firstDigitIndex).trim(); 141 | const amount = cost.substring(firstDigitIndex).trim(); 142 | return [currency, amount]; 143 | }; --------------------------------------------------------------------------------