Below is one practical way to build a Firefox WebExtension that can
• detect when the user is in Reader-Mode (about:reader pages)
• let the user highlight / right-click a word
• look that word up in an on-line dictionary API
• surface the definition in a neat little in-page tooltip (so the user never leaves Reader-Mode).
The walk-through contains:
- How Reader Mode works and what that means for extensions
- The overall extension flow
- A complete minimal code skeleton (manifest + background + content-script + CSS)
- Extra polish ideas / gotchas
Feel free to copy-paste & iterate.
- Understanding Reader-Mode from the add-on side
• When the user clicks the Reader-Mode icon, Firefox navigates from
https://site.example/article.html → about:reader?url=encodedOriginalURL
• Reader-Mode pages live under the about:reader* origin.
They are normal web documents as far as WebExtensions are concerned, so you can inject content scripts by listing about:reader* in matches (or just use <all_urls>).
• Because Reader-Mode rewrites the markup with Mozilla’s Readability library, DOM positions change, but the literal text content of the article is still present. Normal window.getSelection() therefore works fine.
- High-level flow
- Content script runs on about:reader*.
- User selects a word ⇒ right-click ⇒ context-menu item “Look up ‘word’”.
- Background script receives the click, fetches a definition from a dictionary API (e.g. https://api.dictionaryapi.dev or Oxford, Merriam-Webster, Wordnik, etc.).
- Background sends the definition back to the content script.
- Content script injects a tiny tooltip next to the selection; clicking outside closes it.
- Minimal working prototype
A) manifest.json
{
"manifest_version": 2,
"name": "Reader-Mode Dictionary",
"version": "1.0",
"description": "Look up definitions from Reader-Mode pages",
"permissions": [
"contextMenus",
"activeTab",
"https://api.dictionaryapi.dev/*"
],
"browser_specific_settings": {
"gecko": {
"strict_min_version": "91.0",
"id": "reader-dictionary@example.com"
}
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["about:reader*"],
"js": ["content.js"],
"css": ["tooltip.css"]
}
]
}
Notes
• manifest_version:2 still the only one Firefox fully supports; MV3 is coming but many APIs are still experimental.
• If you want it to work on non-Reader pages too, add "http://*/*", "https://*/*" to matches.
B) background.js
// Create the context-menu item once at start-up
browser.contextMenus.create({
id: "lookupDefinition",
title: "Look up \"%s\"",
contexts: ["selection"]
});
async function fetchDefinition(word) {
const url = `https://api.dictionaryapi.dev/api/v2/entries/en/${encodeURIComponent(word)}`;
const resp = await fetch(url);
if (!resp.ok) throw new Error("Word not found");
const json = await resp.json();
return json[0]?.meanings?.[0]?.definitions?.[0]?.definition || "No definition found.";
}
// Handle clicks on the context-menu
browser.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId !== "lookupDefinition") return;
const word = info.selectionText.trim();
try {
const definition = await fetchDefinition(word);
// send message to the content script in the same tab
browser.tabs.sendMessage(tab.id, {
type: "SHOW_DEFINITION",
word,
definition
});
} catch (err) {
browser.tabs.sendMessage(tab.id, {
type: "SHOW_DEFINITION",
word,
definition: "❌ " + err.message
});
}
});
C) content.js
// Listen for messages from background.js
browser.runtime.onMessage.addListener((msg) => {
if (msg.type === "SHOW_DEFINITION") {
injectTooltip(msg.word, msg.definition);
}
});
function injectTooltip(word, definition) {
removeExistingTooltip();
const sel = window.getSelection();
if (!sel.rangeCount) return;
const range = sel.getRangeAt(0).cloneRange();
const rect = range.getBoundingClientRect();
const tooltip = document.createElement("div");
tooltip.className = "rdict-tooltip";
tooltip.innerHTML = `
<strong>${word}</strong><br>
<span>${definition}</span>
`;
document.body.appendChild(tooltip);
// position near the selection
const scrollX = window.scrollX || document.documentElement.scrollLeft;
const scrollY = window.scrollY || document.documentElement.scrollTop;
tooltip.style.left = `${rect.left + scrollX}px`;
tooltip.style.top = `${rect.bottom + scrollY + 6}px`;
// auto-dismiss on click outside or ESC
setTimeout(() => {
document.addEventListener("click", dismiss);
document.addEventListener("keydown", escDismiss);
}, 0);
function dismiss() { removeExistingTooltip(); }
function escDismiss(e) { if (e.key === "Escape") removeExistingTooltip(); }
}
function removeExistingTooltip() {
const old = document.querySelector(".rdict-tooltip");
if (old) old.remove();
document.removeEventListener("click", removeExistingTooltip);
document.removeEventListener("keydown", removeExistingTooltip);
}
D) tooltip.css
.rdict-tooltip {
position: absolute;
max-width: 260px;
padding: 8px 10px;
z-index: 2147483647; /* above everything */
background: #fff;
color: #111;
border: 1px solid #ccc;
border-radius: 6px;
box-shadow: 0 3px 10px rgba(0,0,0,.2);
font-family: system-ui, sans-serif;
font-size: 14px;
line-height: 1.3;
}
.rdict-tooltip strong {
font-weight: 600;
}
Put those four files in a directory, open about:debugging, “This Firefox” → “Load Temporary Add-on…” → pick the manifest.
Go to any article, click the Reader icon, highlight a word, right-click → “Look up …” 🥳.
- Extras, polishing & caveats
• Better UX than a context-menu:
– Pop a tiny “🔍” button just above the selection (like Google Docs).
– Listen for double-click events and auto-lookup.
• Multiple dictionaries or thesaurus: add sub-menu items or an add-on options page.
• Offline lookup: bundle WordNet or a JSON wordlist and read from it with an IndexedDB. No network → still works.
• Reader-Mode detection: If you need to conditionally enable menus only on such pages, check tab.url.startsWith("about:reader") in the contextMenus.onShown handler.
• CORS / API keys: If your chosen dictionary requires an API key, keep it server-side or use browser.storage.local + options_ui for user-provided keys.
• MV3 migration: by the time MV3 stabilises in Firefox you will move background.js into a service-worker (background.service_worker in manifest v3) and change messaging to browser.runtime.sendMessage the same way. The rest (content-script, CSS) is unchanged.
• Accessibility: Add ARIA roles to the tooltip, announce via aria-live region so screen-reader users get the definition too.
• Permissions review: Mozilla AMO is picky—if you only inject into about:reader* and use one HTTPS API, reviewers are usually happy.
Happy hacking! Any part you’d like to expand on, just holler.