|
| 1 | +--- |
| 2 | +title: 'Chrome Extension Using ReactJS and MongoDB' |
| 3 | +sidebar_label: Chrome Extension Using ReactJS and MongoDB |
| 4 | +authors: [khushi-kalra] |
| 5 | +tags: [chrome extension, ReactJS, MongoDB] |
| 6 | +date: 2024-06-13 23:23:23 |
| 7 | +hide_table_of_contents: true |
| 8 | +--- |
| 9 | + |
| 10 | +# Chrome Extension Using ReactJS and MongoDB |
| 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 | +Here's a step-by-step guide based on my experience: |
| 15 | + |
| 16 | +## Step 1: Create a React App |
| 17 | +First, you'll need to set up a basic React application. You can do this using Create React App: |
| 18 | +``bash |
| 19 | +npx create-react-app my-chrome-extension |
| 20 | +cd my-chrome-extension |
| 21 | +``` |
| 22 | +Step 2: Change the Manifest JSON File |
| 23 | +The manifest.json file is crucial for Chrome extensions as it contains metadata about your extension. Create a manifest.json file in the public folder with the following content: |
| 24 | +
|
| 25 | +```json |
| 26 | +{ |
| 27 | + "manifest_version": 3, |
| 28 | + "name": "My Chrome Extension", |
| 29 | + "version": "1.0", |
| 30 | + "description": "A simple Chrome extension built with React.", |
| 31 | + "action": { |
| 32 | + "default_popup": "index.html", |
| 33 | + "default_icon": "icon.png" |
| 34 | + }, |
| 35 | + "permissions": [] |
| 36 | +} |
| 37 | +``` |
| 38 | +Step 3: Add Height and Width to index.css |
| 39 | +To ensure your extension has the proper dimensions, update the index.css file in the src folder: |
| 40 | + |
| 41 | +```css |
| 42 | +html, body, #root { |
| 43 | + height: 600px; |
| 44 | + width: 400px; |
| 45 | + margin: 0; |
| 46 | + padding: 0; |
| 47 | +} |
| 48 | +``` |
| 49 | +Step 4: Change Rendering to BrowserRoute in App.js and Add Routes |
| 50 | +To manage different views in your extension, you can use React Router. Install React Router: |
| 51 | + |
| 52 | +```bash |
| 53 | +npm install react-router-dom |
| 54 | +``` |
| 55 | +Then, update App.js to include routes: |
| 56 | + |
| 57 | +```jsx |
| 58 | +import React from 'react'; |
| 59 | +import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'; |
| 60 | +import Home from './Home'; |
| 61 | +import About from './About'; |
| 62 | + |
| 63 | +function App() { |
| 64 | + return ( |
| 65 | + <Router> |
| 66 | + <Routes> |
| 67 | + <Route path="/" element={<Home />} /> |
| 68 | + <Route path="/about" element={<About />} /> |
| 69 | + </Routes> |
| 70 | + </Router> |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +export default App; |
| 75 | +``` |
| 76 | +Step 5: Run npm build and Load the Build Folder in Chrome Extensions |
| 77 | +Finally, build your React app: |
| 78 | + |
| 79 | +```bash |
| 80 | +npm run build |
| 81 | +``` |
| 82 | +Load the build folder as an unpacked extension in Chrome: |
| 83 | + |
| 84 | +Open Chrome and go to chrome://extensions/. |
| 85 | +Enable "Developer mode" in the top right corner. |
| 86 | +Click "Load unpacked" and select the build folder from your React app. |
| 87 | +Adding a Backend |
| 88 | +If you want to add a backend using Node.js and MongoDB, follow these steps: |
| 89 | + |
| 90 | +Step 1: Create a Backend Folder Within the App |
| 91 | +In your project root, create a new folder called backend: |
| 92 | + |
| 93 | +```bash |
| 94 | +mkdir backend |
| 95 | +cd backend |
| 96 | +``` |
| 97 | +Step 2: Add server.js |
| 98 | +Create a server.js file in the backend folder: |
| 99 | + |
| 100 | +```javascript |
| 101 | +const express = require('express'); |
| 102 | +const mongoose = require('mongoose'); |
| 103 | +const cors = require('cors'); |
| 104 | + |
| 105 | +const app = express(); |
| 106 | +const PORT = process.env.PORT || 5000; |
| 107 | + |
| 108 | +app.use(cors()); |
| 109 | +app.use(express.json()); |
| 110 | + |
| 111 | +mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }) |
| 112 | + .then(() => console.log('MongoDB connected')) |
| 113 | + .catch(err => console.log(err)); |
| 114 | + |
| 115 | +app.get('/', (req, res) => { |
| 116 | + res.send('Hello, world!'); |
| 117 | +}); |
| 118 | + |
| 119 | +app.listen(PORT, () => { |
| 120 | + console.log(`Server is running on port ${PORT}`); |
| 121 | +}); |
| 122 | +``` |
| 123 | +Add a .env file in the backend folder with your MongoDB connection string: |
| 124 | + |
| 125 | +```env |
| 126 | +MONGO_URI=your_mongodb_connection_string |
| 127 | +``` |
| 128 | + |
| 129 | +## Final Thoughts |
| 130 | +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. |
| 131 | + |
| 132 | +Feel free to leave any questions or comments below, and happy coding! |
0 commit comments