A dynamic Chrome Extension that performs real-time sentiment analysis on any webpage using the Hugging Face API and visually highlights text as positive, neutral, or negative with custom styling.
-
Configured via
manifest.json
:- Declares name, version, icons, and permissions (
activeTab
,storage
, etc.). - Registers
background.js
,content.js
, and optionalpopup.html
. - Enables injection of content scripts on any website.
- Declares name, version, icons, and permissions (
-
Files:
popup/popup.html
,popup/popup.js
-
Users can:
- Enable/disable sentiment highlighting.
- Customize highlight colors.
- Adjust highlight intensity.
-
Uses Chrome's local storage to save preferences.
-
File:
content.js
-
Injected into web pages:
- Traverses visible DOM text nodes.
- Sends chunks of text to Hugging Face API.
- Applies highlights based on sentiment results.
-
Uses models like
distilbert-base-uncased-finetuned-sst-2-english
. -
Maps labels:
LABEL_2
→ Positive ✅LABEL_1
→ Neutral ⚪LABEL_0
→ Negative ❌
-
Dynamically wraps text nodes with
<span class="sentiment-highlight">
. -
Applies inline styles or loads
highlight.css
with background color + opacity. -
Color mapping based on sentiment:
- Green: Positive
- Red: Negative
- Gray: Neutral
- File:
background.js
- Manages persistent extension state.
- Communicates between popup and content scripts.
- Stores/retrieves settings using Chrome’s storage API.
-
File:
highlight.css
-
Controls appearance:
- Background color
- Opacity
- Transition animations
- Popup → Background → Content: Send settings and commands.
- Content → Hugging Face API: Analyze text sentiment.
- Content → DOM: Apply highlight styles dynamically.
- Toggle switch in popup
- Settings saved and applied instantly
- Highlights removed if disabled
- API failures → Fallback to neutral or skipped text
- No text found → Skip
- Offline → Silent fail
Sentiment-Highlighter-Extension/
├── assets/ # Optional images/assets
├── icons/ # Extension icons
├── popup/
│ ├── popup.html # Popup UI
│ └── popup.js # Popup logic
├── background.js # Background script
├── content.js # Content script
├── highlight.css # Styling for highlights
├── manifest.json # Extension config
└── Project Flow.md # Technical documentation
Feature | Stack |
---|---|
Frontend UI | HTML, JS, Chrome APIs |
Sentiment Analysis API | Hugging Face Transformers API |
Communication | Chrome Runtime Messaging |
Extension Framework | Chrome Extension Manifest v3 |
State Persistence | Chrome Storage API |
- Clone the repo:
git clone https://github.com/vedantmpatil/Sentiment-Highlighter-Extension.git
- Replace the Hugging Face API key inside
content.js
:
headers: {
'Authorization': 'Bearer YOUR_HF_TOKEN',
...
}
- Load Extension into Chrome:
- Go to
chrome://extensions
- Enable Developer Mode
- Click Load unpacked and select the project folder
- Test on any website and toggle sentiment highlighting via the popup.
- Chrome Extension development (Manifest v3)
- DOM traversal and node manipulation
- Asynchronous JavaScript + API integration
- State persistence with Chrome Storage
- Real-time UX enhancements
Vedant M. Patil 💼 GitHub: vedantmpatil