The Chrome Extension Engineer builds browser-native tools that augment web pages or provide standalone utilities. Manifest V3 (MV3) is the current standard - it replaces background pages with service workers, which terminate when idle and must persist state in chrome.storage, not memory. This is the biggest architectural change from MV2 and the source of most MV3 bugs.
Manifest V3 Structure
{
"manifest_version": 3,
"name": "My Extension",
"version": "1.0.0",
"description": "What the extension does.",
"permissions": [
"storage",
"activeTab"
// Only request what you need - Chrome Web Store reviewers check
// "tabs" - access to all tab metadata (justify this)
// "scripting" - inject scripts programmatically
// "host_permissions" - access to specific origins
],
"host_permissions": [
"https://api.example.com/*"
// Required for fetch() to external origins from service worker
],
"background": {
"service_worker": "background.js",
"type": "module"
},
"content_scripts": [{
"matches": ["https://*.github.com/*"],
"js": ["content.js"],
"run_at": "document_idle"
}],
"action": {
"default_popup": "popup.html",
"default_title": "Open extension"
},
"icons": { "16": "icon16.png", "48": "icon48.png", "128": "icon128.png" }
}
Message Passing Between Contexts
// Content scripts and service workers run in separate contexts
// They communicate via chrome.runtime.sendMessage / onMessage
// content.js - send to background
async function extractPageData() {
const response = await chrome.runtime.sendMessage({
type: 'FETCH_USER_DATA',
payload: { url: window.location.href },
});
console.log('Got from background:', response);
}
// background.js (service worker) - receive from content, respond
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'FETCH_USER_DATA') {
// Must call sendResponse synchronously OR return true to keep channel open
fetchFromApi(message.payload.url)
.then(data => sendResponse({ success: true, data }))
.catch(err => sendResponse({ success: false, error: err.message }));
return true; // Keep message channel open for async response
}
});
// Storage: use chrome.storage.local (not localStorage - unavailable in service worker)
await chrome.storage.local.set({ userSettings: { theme: 'dark' } });
const { userSettings } = await chrome.storage.local.get('userSettings');
Service Worker Persistence Pattern
// Service workers terminate after ~30s of inactivity
// Don't store state in variables - it's lost when SW terminates
// ✗ Bad: state in memory (lost on SW termination)
let currentUser = null;
// ✓ Good: always read from storage
async function getCurrentUser() {
const { user } = await chrome.storage.local.get('user');
return user;
}
// Keep the SW alive during long operations using chrome.alarms
chrome.alarms.create('keepAlive', { periodInMinutes: 0.4 });
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'keepAlive') { /* no-op, just keeps SW running */ }
});