This repository contains a sample Chrome extension demonstrating best practices for Manifest V3 extensions. It showcases key concepts and techniques for building robust and efficient Chrome extensions, with a focus on:
- Communication between background scripts and content scripts
- Secure handling of the extension ID
- Integration with third-party services (e.g., Mixpanel) without using their standard SDK
- Efficient storage management across extension components
chrome-extension-best-practices/
├── src/
│ ├── background.js
│ ├── content.js
│ ├── storage.js
│ └── styles.css
├── manifest.json
├── package.json
├── webpack.config.js
├── README.md
└── icons/
├── icon16.png
├── icon32.png
├── icon48.png
└── icon128.png
- Secure communication between background and content scripts
- Dynamic injection of the extension ID into content scripts
- Integration with Mixpanel using its HTTP API instead of the JavaScript SDK
- Centralized storage management
- Modular code structure for better maintainability
-
Clone this repository:
git clone https://github.com/dipankar/chrome-extension-best-practices.git
-
Navigate to the project directory:
cd chrome-extension-best-practices
-
Install dependencies:
npm install
-
Build the extension:
npm run build
-
Load the extension in Chrome:
- Open Chrome and navigate to
chrome://extensions
- Enable "Developer mode" in the top right corner
- Click "Load unpacked" and select the
dist
directory in your project folder
- Open Chrome and navigate to
After installation, the extension will be active on all web pages. It demonstrates:
- Tracking page views using Mixpanel
- Storing and retrieving the last visit timestamp
To see it in action:
- Open the Chrome Developer Tools (F12 or Ctrl+Shift+I)
- Navigate to the "Console" tab
- You should see logs indicating:
- The extension ID being set
- Mixpanel initialization
- Page view events being tracked
- Storage operations
The background script injects the extension ID into content scripts, allowing for secure communication between different parts of the extension.
// In background.js
function injectExtensionId(tabId) {
chrome.scripting.executeScript({
target: { tabId: tabId },
func: setExtensionId,
args: [chrome.runtime.id]
});
}
Content scripts use the injected extension ID to communicate with the background script.
// In storage.js
chrome.runtime.sendMessage(extensionId, {
action: "setStorageItem",
key: key,
value: value
}, response => {
// Handle response
});
The extension demonstrates how to integrate Mixpanel using its HTTP API instead of the JavaScript SDK.
// In background.js
async function trackMixpanelEvent(eventName, properties) {
// Implementation using fetch to send events to Mixpanel
}
A dedicated storage.js
module handles all storage operations, providing a clean interface for other parts of the extension.
// In storage.js
export async function setStorageItem(key, value) {
// Implementation
}
export async function getStorageItem(key) {
// Implementation
}
-
Security: Use the extension ID for message passing to ensure that only your extension can communicate with its components.
-
Modularity: Separate concerns into different files (e.g.,
background.js
,content.js
,storage.js
) for better organization and maintainability. -
Asynchronous Operations: Use async/await for cleaner asynchronous code, especially for storage and API operations.
-
Error Handling: Implement proper error handling and logging throughout the extension.
-
Performance: Minimize the use of storage and API calls by batching operations where possible.
-
Privacy: Be mindful of the data you collect and store. Only track necessary information and comply with privacy regulations.
-
Manifest V3 Compliance: Ensure all APIs and practices are compatible with Manifest V3 specifications.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License. See the LICENSE
file for details.
Dipankar Sarkar
For any questions or suggestions, please open an issue or contact the author directly.