Build a Firefox Screenshot Extension: Step-by-Step
In this article
This guide walks you through creating a Firefox extension that captures a screenshot of the current tab, saves it as a PNG, and lets the user download it. You'll use the WebExtensions API, which works across modern browsers, but we'll focus on Firefox specifics. By the end, you'll have a working extension with a toolbar button and a keyboard shortcut.
What You'll Need
- Firefox browser (any recent version, e.g., 115 or later)
- A text editor (VS Code, Notepad++, or even plain Notepad)
- Basic familiarity with HTML, CSS, and JavaScript
- About 30 minutes of time
Step-by-Step
-
Create the project folder. On your computer, create a new folder named
screenshot-extension. All extension files will live here. -
Create the manifest file. Inside the folder, create a file named
manifest.json. This is the heart of the extension. Open it and add the following:{ "manifest_version": 2, "name": "Tab Screenshot", "version": "1.0", "description": "Capture a screenshot of the current tab.", "browser_specific_settings": { "gecko": { "id": "tab-screenshot@example.com" } }, "permissions": ["activeTab", "downloads"], "browser_action": { "default_title": "Take Screenshot", "default_icon": { "48": "icon.svg" } }, "background": { "scripts": ["background.js"] }, "commands": { "take-screenshot": { "suggested_key": { "default": "Ctrl+Shift+S" }, "description": "Take a screenshot" } } }This manifest declares permissions for the active tab and for downloading files. The
browser_actionadds a toolbar button. Thecommandssection sets up a keyboard shortcut. -
Create the background script. Create a file named
background.jsin the same folder. This script handles the screenshot logic. Add the following code:function takeScreenshot() { browser.tabs.captureTab().then((dataUrl) => { const url = new URL(dataUrl); const filename = `screenshot-${Date.now()}.png`; browser.downloads.download({ url: dataUrl, filename: filename, saveAs: true }); }).catch((error) => { console.error(`Failed to capture tab: ${error}`); }); } browser.browserAction.onClicked.addListener(takeScreenshot); browser.commands.onCommand.addListener((command) => { if (command === "take-screenshot") { takeScreenshot(); } });This code captures the visible area of the active tab, converts it to a data URL, and triggers a download. The
saveAs: trueoption lets the user choose where to save the file. -
Create an icon. Create a file named
icon.svgwith a simple camera icon. You can use this minimal SVG:<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"> <rect width="48" height="48" fill="#4a4a4a"/> <circle cx="24" cy="24" r="10" fill="white"/> <rect x="16" y="14" width="16" height="4" fill="white"/> </svg>
This creates a simple gray square with a white circle and a small rectangle that resembles a camera lens.
-
Load the extension in Firefox. Open Firefox and type
about:debuggingin the address bar. Click This Firefox in the left sidebar, then click Load Temporary Add-on. Select themanifest.jsonfile from your project folder. The extension will appear in the toolbar with your icon. - Test the extension. Navigate to any webpage, then click the toolbar button or press Ctrl+Shift+S (on Windows/Linux) or Cmd+Shift+S (on macOS). A save dialog should appear with a PNG file name. Save it and open it to verify it captured the visible portion of the tab.
- Package for distribution (optional). To share your extension, you need to sign it. Go to addons.mozilla.org, create a developer account, and follow their submission guidelines. For personal use, the temporary loading method is sufficient.
Common Mistakes
- Forgetting the gecko ID: Firefox requires a unique ID in
browser_specific_settingsfor permanent installs. Without it, the extension may load temporarily but fail on restart. - Using
tabs.captureVisibleTabinstead ofcaptureTab: The older API requires theactiveTabpermission and may fail on some pages.captureTabis more reliable for the current tab. - Not handling the promise rejection: If the capture fails (e.g., on a restricted page like
about:addons), the error is silently ignored unless you catch it. Always add a.catch(). - Missing the icon file: If the manifest references an icon that doesn't exist, Firefox may show a generic icon or fail to load the extension.
Tips
- Capture full page: To capture the entire page (not just the visible area), use
browser.tabs.captureTab(tabId, { format: 'png', rect: { width: document.body.scrollWidth, height: document.body.scrollHeight } }). This requires injecting a content script to get the full dimensions. - Add a context menu: You can add a right-click menu item to capture screenshots. Use the
contextMenusAPI with themenuspermission. - Use PNG format: The default format is PNG, which is lossless and suitable for screenshots. If you need JPEG, specify
{ format: 'jpeg', quality: 0.8 }in the capture options. - Test on different pages: Some sites (like browser internal pages) block screenshots. Test on regular websites to ensure your code works.
Troubleshooting
- Extension doesn't load: Check the browser console (Ctrl+Shift+J) for errors. Common issues are invalid JSON in the manifest or missing files. Ensure the manifest is valid JSON (no trailing commas).
- Clicking the button does nothing: Open the Browser Console (Ctrl+Shift+J) and look for error messages. Make sure the background script is loaded and there are no syntax errors. Also verify that the
browser.browserAction.onClickedlistener is correctly registered. - Screenshot is blank or black: This often happens on pages with WebGL or canvas elements that are protected. Try a different page. Also, ensure the tab is fully loaded before capturing.
- Download doesn't start: The
downloadspermission is required. Check that you added it to the manifest. Also, Firefox may block downloads if the data URL is too large; for big screenshots, consider using a blob URL instead. - Keyboard shortcut doesn't work: The shortcut might conflict with another extension or Firefox's own shortcuts. Try a different combination, like Ctrl+Shift+Y. Also, ensure the command name matches exactly.
Frequently Asked Questions
Can I capture a specific region of the page?
Yes, you can pass a rect option to captureTab with left, top, width, and height values. You'd need to get the coordinates from a content script or user interaction.
Does this work on Firefox for Android?
Firefox for Android supports a subset of WebExtensions APIs. The captureTab API is not available on Android, so this extension would not work there.
How do I change the default filename?
In the downloads.download call, the filename parameter is a relative path within the Downloads folder. You can include subdirectories, e.g., screenshots/foo.png.
Can I upload the screenshot automatically instead of downloading?
Yes, you can use the fetch API to send the data URL to a server, but that requires a backend and additional permissions like host_permissions for the target URL. This guide focuses on local download.
Why do I need the activeTab permission?
The activeTab permission grants temporary access to the current tab when the user invokes the extension (e.g., by clicking the button). This is sufficient for captureTab and is more privacy-friendly than requesting all tabs.
Can I use this code in Chrome?
Most of it works in Chrome with minor changes: use chrome.tabs.captureVisibleTab instead of captureTab, and remove the browser_specific_settings block. Chrome also uses action instead of browser_action in Manifest V3.