|
| 1 | +--- |
| 2 | +title: 'Chrome Extension Using MERN' |
| 3 | +sidebar_label: Chrome Extension Using MERN |
| 4 | +authors: [khushi-kalra] |
| 5 | +tags: [chrome extension, web dev, React, Express, MongoDB, Node, UI] |
| 6 | +date: 2024-06-13 23:23:23 |
| 7 | +hide_table_of_contents: true |
| 8 | +--- |
| 9 | + |
| 10 | +# Chrome Extension Using MERN |
| 11 | + |
| 12 | +Creating a Chrome extension can seem like a daunting task, especially when you're trying to combine it with technologies like ReactJS and MongoDB. When I first set out to build my extension, I found it challenging to find a perfect YouTube tutorial or blog post that covered everything I needed. So, I turned to StackOverflow and other resources to piece together my project. |
| 13 | + |
| 14 | +You can always take help from my github repository: https://github.com/abckhush/Basic-Chrome-Extension |
| 15 | + |
| 16 | +Here's a step-by-step guide based on my experience: |
| 17 | + |
| 18 | +## Creating Frontend of the Extension |
| 19 | + |
| 20 | +### Step 1: Create a React App |
| 21 | +First, you'll need to set up a basic React application. You can do this using Create React App: |
| 22 | + |
| 23 | +```bash |
| 24 | +npx create-react-app my-chrome-extension |
| 25 | +cd my-chrome-extension |
| 26 | +``` |
| 27 | + |
| 28 | +### Step 2: Change the Manifest JSON File |
| 29 | +The manifest.json file is crucial for Chrome extensions as it contains metadata about your extension. Update the manifest.json file in the public folder with the following content: |
| 30 | + |
| 31 | +```json |
| 32 | +{ |
| 33 | + "manifest_version":3, |
| 34 | + "name":"Chrome Extension", |
| 35 | + "version":"1.0.0", |
| 36 | + "description":"My First Chrome Extension Using MERN", |
| 37 | + "action": { |
| 38 | + "default_popup": "index.html", |
| 39 | + "default_title": "Open" |
| 40 | + }, |
| 41 | + "permissions":[ |
| 42 | + "scripting" |
| 43 | + ] |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### Step 3: Add Height and Width |
| 48 | +To ensure your extension has the proper dimensions, update the index.css file in the src folder and add height and width: |
| 49 | + |
| 50 | +```css |
| 51 | +body { |
| 52 | + min-width: 300px; |
| 53 | + min-height: 200px; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### Check Point |
| 58 | +To check if you have followed all the steps properly. You can go try to run the extension in browser. |
| 59 | +1. Run `npm build` in the terminal. |
| 60 | +2. Open Chrome and go to chrome://extensions/. |
| 61 | +3. Enable "Developer mode" in the top right corner. |
| 62 | +4. Click "Load unpacked" and select the build folder from your React app. |
| 63 | +5. See if can see the default React page in the height and width you gave. |
| 64 | + |
| 65 | +### Step 4: Change Rendering to MemoryRouter |
| 66 | +This is the most crucial step. BrowserRouter is not supported for the Chrome Extensions, which is default in React applications. We are going to change that too MemoryRouter. |
| 67 | +1. Install React Router: |
| 68 | + |
| 69 | +```bash |
| 70 | +npm install react-router-dom |
| 71 | +``` |
| 72 | + |
| 73 | +2. Update index.js to include routes: |
| 74 | + |
| 75 | +```jsx |
| 76 | +import React from 'react'; |
| 77 | +import ReactDOM from 'react-dom'; |
| 78 | +import App from './App'; |
| 79 | + |
| 80 | +import { MemoryRouter as Router } from 'react-router-dom'; |
| 81 | + |
| 82 | +ReactDOM.render( |
| 83 | + <React.StrictMode> |
| 84 | + <Router> |
| 85 | + <App /> |
| 86 | + </Router> |
| 87 | + </React.StrictMode>, |
| 88 | + document.querySelector('#root') |
| 89 | +); |
| 90 | +``` |
| 91 | + |
| 92 | +### Step 5: Adding Routing |
| 93 | +1. We will make a "Components" folder in src and a Home.jsx. |
| 94 | +```jsx |
| 95 | +import React from 'react'; |
| 96 | + |
| 97 | +function Home() { |
| 98 | + return ( |
| 99 | + <div> |
| 100 | + <h1>Welcome to My Home Page</h1> |
| 101 | + <p>This is a simple home page.</p> |
| 102 | + </div> |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +export default Home; |
| 107 | +``` |
| 108 | + |
| 109 | +2. We will update our App.js as: |
| 110 | +```js |
| 111 | +import React from 'react'; |
| 112 | +import { Routes, Route } from 'react-router-dom'; |
| 113 | +import Home from './Components/Home.jsx'; |
| 114 | + |
| 115 | +function App() { |
| 116 | + return ( |
| 117 | + <div> |
| 118 | + <Routes> |
| 119 | + <Route path="/" element={<Home />} /> |
| 120 | + </Routes> |
| 121 | + </div> |
| 122 | + ); |
| 123 | +} |
| 124 | + |
| 125 | +export default App; |
| 126 | +``` |
| 127 | +**Note: You can run "npm build" again and reload the build folder to see the changes made.** |
| 128 | + |
| 129 | +## Adding Backend to the Extension |
| 130 | + |
| 131 | +### Step 1: Create a Backend Folder |
| 132 | +In your project root, create a new folder called backend: |
| 133 | + |
| 134 | +```bash |
| 135 | +mkdir backend |
| 136 | +cd backend |
| 137 | +``` |
| 138 | + |
| 139 | +### Step 2: Add server.js |
| 140 | +Create a server.js file in the backend folder: |
| 141 | + |
| 142 | +```javascript |
| 143 | +const express = require('express'); |
| 144 | +const mongoose = require('mongoose'); |
| 145 | +const cors = require('cors'); |
| 146 | + |
| 147 | +const app = express(); |
| 148 | + |
| 149 | +app.use(cors()); |
| 150 | +app.use(express.json()); |
| 151 | + |
| 152 | +mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }) |
| 153 | + .then(() => console.log('MongoDB connected')) |
| 154 | + .catch(err => console.log(err)); |
| 155 | + |
| 156 | +app.get('/', (req, res) => { |
| 157 | + res.send('Hello, world!'); |
| 158 | +}); |
| 159 | + |
| 160 | +const PORT = process.env.PORT || 5000; |
| 161 | +app.listen(PORT, () => { |
| 162 | + console.log(`Server is running on port ${PORT}`); |
| 163 | +}); |
| 164 | +``` |
| 165 | +### Step 3: Add a .env file |
| 166 | + |
| 167 | +```env |
| 168 | +MONGO_URI= your_mongodb_connection_string |
| 169 | +PORT= 5000 |
| 170 | +``` |
| 171 | + |
| 172 | +Building a Chrome extension with ReactJS and MongoDB was a learning experience filled with challenges and triumphs. While finding the perfect tutorial was tough, the process of solving problems using StackOverflow and other resources was incredibly rewarding. I hope this guide helps you in your journey to create your own Chrome extension. |
| 173 | + |
| 174 | +Feel free to connect on github, and happy coding! |
0 commit comments