Best AI Prompts for Chrome Extension Development with Claude Code
TL;DR
- Claude Code’s file-aware editing makes it uniquely suited for Chrome extension development, where multi-file coordination (manifest, background, content scripts, popup) is the norm.
- The most effective Claude Code prompts specify which files exist in the project and which file the generated code should be written to or modified.
- Claude Code’s ability to read and modify existing files means it can extend working code rather than generating isolated snippets.
- Service worker lifecycle management, message passing architecture, and permission handling are the three areas where Claude Code adds the most value.
- Use Claude Code’s project context to maintain consistency across all extension files, not just generate individual code blocks.
Chrome extension development is inherently multi-file: a manifest.json, at least one background script, one or more content scripts, a popup HTML/JS file, and potentially options pages. Claude Code’s project context awareness makes it significantly more effective for extension work than standalone AI tools, because it can see and modify all the files in your extension project simultaneously. This means it can maintain consistency across the manifest, background scripts, and content scripts in ways that isolated prompt-and-response tools cannot.
1. Project Initialization Prompt
Starting a new Chrome extension project correctly is harder than it looks because the manifest.json has many required fields and the file structure has specific conventions. Claude Code can scaffold a complete extension project.
Prompt for scaffolding a new Manifest V3 extension:
Scaffold a complete Chrome Extension project structure for Manifest V3. The extension is a "Page Highlighter" that lets users highlight text on any webpage and save highlights to a list, which syncs across their browser sessions using chrome.storage.
Create the following file structure:
1. **manifest.json** — Manifest V3 compliant, with:
- Proper structure for MV3 (manifest_version: 3)
- Browser action with a popup (popup.html + popup.js)
- Content script that runs on all URLs
- Background service worker (not persistent background page)
- Permissions: storage, activeTab
- Minimal, clean, readable formatting
2. **background.js** — Service worker that:
- Handles messages from content script via chrome.runtime.onMessage
- Saves highlights to chrome.storage.local under a "highlights" key
- On startup (chrome.runtime.onInstalled), initializes empty highlights array if not present
- Exports functions: saveHighlight(url, text, selection), getHighlights(url), deleteHighlight(id)
3. **content.js** — Content script that:
- Listens for mouseup events to capture text selection
- Shows a small floating "highlight" button near the selection
- On click, captures the selected text, selection coordinates, and page URL
- Sends the data to background.js via chrome.runtime.sendMessage
- Receives confirmation and visually marks the highlighted text on the page
4. **popup.html + popup.js** — A simple popup that:
- Lists all saved highlights for the current tab
- Allows clicking a highlight to scroll the page to that position
- Allows deleting highlights
- Shows the count of highlights saved
5. **styles.css** — Minimal styles for the popup and the in-page highlight marker (yellow background)
Use clean, well-commented code. Do not use any external libraries or frameworks. Ensure all chrome API calls are Manifest V3 compatible.
This scaffold gives you a working extension from day one. You can run it, observe the behavior, and then extend it piece by piece.
2. Advanced Message Passing Architecture
As extensions grow in complexity, the simple one-time message passing pattern often needs to be replaced with a more sophisticated architecture using ports and named channels.
Prompt for establishing robust message passing:
I have a Chrome Extension with the following files currently in my project: manifest.json, background.js, content.js, popup.js, and popup.html. The extension allows users to highlight text on pages and save their highlights.
I need to implement a more robust message passing architecture that handles:
1. **Bi-directional persistent communication**: The popup needs to receive real-time updates when new highlights are saved by the content script, without polling
2. **Named message channels**: Different types of messages (highlight save, highlight delete, settings update, tab change) should be distinguishable
3. **Service worker lifecycle resilience**: If the background service worker has been terminated by Chrome's idle timeout, message passing should automatically reconnect
Here is the current state of my background.js:
[PASTE CURRENT background.js]
Generate updated versions of background.js, content.js, and popup.js that implement:
- chrome.runtime.connect with named ports for popup-to-background communication
- Service worker keepalive strategy using chrome.alarms to ping the service worker and keep it alive during active popup sessions
- Message routing that dispatches to appropriate handlers based on message type
- Proper error handling that logs failures but does not crash the extension
This architectural improvement is the kind of multi-file refactoring that Claude Code handles well because it can read all the existing files and produce changes that are consistent across them.
3. Security Audit Prompt
Chrome extensions have a broad security attack surface because they run with elevated privileges. Claude Code can audit extension code for common security vulnerabilities.
Prompt for security vulnerability audit:
Audit the following Chrome extension code for security vulnerabilities. This is a Manifest V3 extension. Check for:
1. **Cross-Site Scripting (XSS) risks**: Are any DOM elements created using innerHTML or document.write with data from web pages or chrome.storage without sanitization?
2. **Injection risks**: Can a malicious webpage or API response inject content into the extension's pages?
3. **Permission overuse**: Are permissions granted that are not needed for the described functionality?
4. **Data validation**: Is data received from content scripts (via message passing) validated before use?
5. **URL handling**: Are URLs validated before being stored or navigated to?
6. **Storage exposure**: Could sensitive user data in chrome.storage.local be accessed by unauthorized sources?
Here is the code to audit:
[PASTE MANIFEST.JSON AND ALL JS FILES]
For each vulnerability found, provide:
- Severity (Critical, High, Medium, Low)
- Location in the code (file and line/function)
- Description of the vulnerability
- Specific fix with before/after code
- A note on what an attacker could do if this vulnerability were exploited
This audit prompt is valuable for security-sensitive extensions or before publishing to the Chrome Web Store, where review teams actively check for common vulnerabilities.
4. Service Worker Debugging Prompt
Debugging service worker issues is difficult because the service worker runs in an isolated context and logs are not persistent. Claude Code can help diagnose issues based on symptoms.
Prompt for debugging service worker failures:
Diagnose a Chrome Extension service worker that exhibits the following behavior:
[DESCRIBE THE SPECIFIC BEHAVIOR - e.g., "background.js runs once on extension load but stops responding to messages after 30 seconds" or "chrome.runtime.onInstalled listener fires but chrome.storage.local reads return empty even though data was previously saved"]
This is a Manifest V3 extension. The project has the following files:
[LIST FILES]
The service worker currently does the following on startup:
[DESCRIBE OR PASTE relevant portion of background.js]
Based on the symptoms described, what is the most likely cause and how would you fix it? Consider:
- Service worker idle/termination behavior (MV3 service workers can terminate after 30 seconds of inactivity)
- The difference between chrome.storage.session (cleared on service worker termination) and chrome.storage.local (persists)
- The difference between setTimeout and chrome.alarms in service workers
- The fact that chrome.runtime.Port connections do not automatically reconnect after service worker termination
Provide specific code changes with explanations.
5. Production Hardening Prompt
Before publishing an extension to the Chrome Web Store, specific hardening steps improve security, performance, and review pass rates.
Prompt for production hardening:
Review and harden the following Chrome extension code for production release to the Chrome Web Store. The extension has passed basic functionality testing but has not been reviewed for security, performance, or policy compliance.
Manifest V3 extension files:
[PASTE ALL FILES]
Apply the following hardening checklist:
1. **Content Security Policy**: Add a content_security_policy entry to manifest.json that restricts script sources to 'self' and prohibits inline scripts where possible
2. **Host permission minimization**: Check if host_permissions are specified with minimal scope (specific domains rather than broad patterns like "<all_urls>")
3. **Service worker efficiency**: Ensure the service worker has no memory leaks (no dangling event listeners, no variables that accumulate without bound)
4. **Error boundaries**: Add try/catch around all chrome.storage operations and chrome.runtime.sendMessage calls
5. **Manifest completeness**: Ensure all required icons are specified (16, 32, 48, 128 pixel sizes), manifest has a description, version follows semantic versioning
6. **Removal cleanup**: Implement chrome.runtime.setUninstallURL() to clean up user data when the extension is removed
7. **Logging strategy**: Add structured logging using console methods that can be reviewed in the service worker console, without exposing sensitive data
8. **Review against Web Store policies**: Check for compliance with the Chrome Web Store's restricted Use Policies (no embedding of remote code, no obfuscated code, no cryptocurrency mining)
FAQ
How does Claude Code’s multi-file awareness help with extension development? When you run /code in Claude Code, it can read all files in your extension project simultaneously. This means it can generate a change to background.js that accounts for the current state of content.js, and a change to manifest.json that accurately reflects the scripts defined in background.js. Without this awareness, AI tools generate code for one file that may conflict with another.
What is the most significant architectural difference Claude Code can help with in existing extensions? The transition from Manifest V2 to V3 is the most common significant refactoring need. Claude Code can read your existing manifest.json and background.js, identify V2-specific patterns (persistent background pages, chrome.extension APIs), and generate a V3-compatible architecture. This is substantially more accurate than providing the code without project context.
How do I handle the service worker lifecycle in prompts for Claude Code? Include explicit context about the service worker’s lifecycle: “Chrome may terminate this service worker after 30 seconds of inactivity. Any state that must survive termination should be stored in chrome.storage. Do not use closure variables for state that is needed across restarts.”
Can Claude Code help me pass the Chrome Web Store review process? Yes, particularly with the production hardening prompt that checks for the specific policy violations that cause review rejections (obfuscated code, excessive permissions, missing policy disclosures). Use the hardening prompt before your first submission and after any significant code changes.
How do I get Claude Code to generate code for a specific extension file without accidentally modifying others? Specify the filename explicitly in your prompt: “Generate only the content.js file” or “Modify only background.js, do not change other files.” Claude Code will ask for confirmation if your requested change requires modifications to other files, giving you control over the scope.
Conclusion
Claude Code’s project-level context awareness makes it the most effective AI tool for Chrome extension development. It can maintain consistency across multi-file extension projects in ways that single-prompt AI tools cannot, particularly for architectural decisions like message passing patterns and service worker lifecycle management.
Key Takeaways:
- Use project scaffolding prompts to generate a complete, working extension structure before building features on top.
- Claude Code’s multi-file editing enables architectural refactoring across background.js, content.js, and manifest.json simultaneously.
- Run security audits on extension code before publication, specifically checking for XSS risks, permission overuse, and injection vulnerabilities.
- Service worker lifecycle resilience (persistent connections, chrome.alarms keepalive) is an architectural decision that benefits from Claude Code’s ability to see the full project.
- Production hardening should precede Chrome Web Store submission.
Next Step: If you have an existing Chrome extension project, open it in Claude Code and run the security audit prompt. If you are starting a new extension, start with the scaffold prompt and build incrementally, running each generated file to confirm expected behavior before adding the next layer of complexity.