User Agents

Web browsers and everyday web tech, explained

Capture Full Screen in Chrome Extension: A Complete Guide

· 6 min read · Extensions

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Capturing the full screen in a Chrome extension is achieved by utilizing the chrome.tabs.captureVisibleTab API for the current viewport and programmatically scrolling the page to stitch multiple captures together, or by using the chrome.debugger API to invoke the Page.captureScreenshot command. Because Chrome extensions cannot natively take a single "full-page" screenshot of a scrolling document in one call, developers must implement a loop that captures the visible area, scrolls down by the height of the viewport, and repeats the process until the bottom of the document is reached. This process requires specific permissions in the manifest file and a coordinated effort between the background service worker and a content script.

What You'll Need

  • A basic understanding of JavaScript, HTML, and CSS.
  • The Chrome browser installed on your machine.
  • A text editor (such as Visual Studio Code) for writing your manifest and script files.
  • The activeTab or permission declared in your manifest.json.
  • Knowledge of the Chrome Extension Manifest V3 architecture.

Step-by-Step

  1. Configure the manifest.json file: Start by creating your manifest.json. You must request the activeTab permission to access the current tab and the scripting permission to execute code within the page to handle scrolling. If you intend to use the debugger API for a more native feel, you must add the debugger permission. Ensure your manifest version is set to 3.
  2. Create the Background Service Worker: The background script acts as the orchestrator. It listens for a browser action (like clicking the extension icon) and initiates the capture sequence. Use chrome.action.onClicked.addListener to trigger the process. The background script is responsible for calling chrome.tabs.captureVisibleTab, which returns a data URL of the current visible area of the page.
  3. Implement the Content Script for Scrolling: Since the background script cannot interact with the DOM, you need a content script. This script will be injected into the page via chrome.scripting.executeScript. The content script should calculate the total height of the page using document.body.scrollHeight and the height of the current viewport using window.innerHeight.
  4. Develop the Capture Loop: Create a logic loop in your background script that communicates with the content script. The flow should be: 1) Background script tells content script to scroll to a specific position. 2) Content script scrolls and sends a confirmation message back. 3) Background script waits a few milliseconds for the page to render (to avoid capturing white gaps or loading elements). 4) Background script calls captureVisibleTab. 5) Repeat until the scroll position equals or exceeds the total page height.
  5. Handle Image Stitching with HTML5 Canvas: Once you have an array of data URLs (the screenshots), you need to combine them. Create an off-screen HTML5 Canvas element. Set the canvas width to the viewport width and the height to the total page height. Iterate through your captured images, drawing each one onto the canvas at the correct Y-coordinate offset (e.g., ctx.drawImage(img, 0, currentScrollY)).
  6. Export the Final Image: After all slices are drawn onto the canvas, use canvas.toDataURL('image/png') to convert the entire canvas into a single image file. You can then trigger a download using the chrome.downloads API or open the image in a new tab for the user to save manually.
  7. Clean Up and Reset: After the capture is complete, send a final message to the content script to scroll the page back to the top (position 0,0) so the user's browsing experience is not disrupted.

Common Mistakes

One of the most frequent errors is failing to account for the "sticky" elements on a page, such as fixed navigation bars or floating headers. When you stitch images together, these fixed elements appear multiple times throughout the final screenshot, creating a repetitive and broken visual effect. To solve this, developers often use the content script to temporarily set position: absolute or display: none on elements with position: fixed before starting the capture loop.

Another common mistake is ignoring the rendering delay. Modern websites use lazy-loading for images and asynchronous rendering for components. If the extension scrolls and captures instantly, the resulting image will contain empty gray boxes or loading spinners. Implementing a small setTimeout or using a MutationObserver to ensure the page has settled before the capture call is essential for high-quality results.

Developers also often forget to handle high-DPI (Retina) displays. On these screens, window.devicePixelRatio is usually 2 or higher. If you set your canvas size based on CSS pixels but draw images that are captured at physical pixel resolution, the image will be cropped or misaligned. You must multiply your canvas dimensions by the device pixel ratio to ensure the image remains crisp and correctly scaled.

Tips

To improve performance, consider using the chrome.debugger API. By attaching to the tab and using the Page.captureScreenshot command with the captureBeyondViewport parameter set to true, you can sometimes bypass the manual scrolling loop entirely for certain types of pages. This provides a much faster and more seamless experience, though it triggers a warning bar at the top of the browser informing the user that the debugger is active.

For better memory management, avoid storing large arrays of base64 strings in the background script's memory. Instead, consider passing the images to a hidden background page or a dedicated options page where the canvas processing can happen without risking the service worker being terminated due to memory limits.

To make the extension more user-friendly, add a visual indicator (like a progress bar or a loading overlay) via the content script. This lets the user know that the extension is actively scrolling and capturing the page, preventing them from clicking away or closing the tab prematurely, which would crash the capture process.

Troubleshooting

If the captured image is coming out blank or white, check your permissions in the manifest.json. The activeTab permission is only granted when the user explicitly interacts with the extension. If you are trying to trigger the capture via an automated timer or an external event, you may need host permissions for the specific URLs you are targeting.

If the images are not stitching together correctly and there are gaps between the slices, check the scroll calculation. Some websites use a custom scroll container (a div with overflow: scroll) instead of the main window or body. In these cases, window.scrollY will always be 0. You must identify the specific scrollable element and manipulate its scrollTop property instead of the window's scroll position.

When encountering "Out of Memory" errors on very long pages (e.g., infinite scroll feeds), the canvas size may exceed the browser's maximum allowed dimensions. To fix this, instead of one giant canvas, you can save the slices as individual files or use a library that handles tiled image generation. Alternatively, you can implement a maximum height limit for the capture to prevent the browser from crashing.

Frequently Asked Questions

Does this method work on all websites?

It works on the vast majority of sites, but pages with complex iframes or those that use overflow: hidden on the body can be challenging. You may need to target specific containers to capture their content.

Will the captured image be high resolution?

Yes, provided you account for the devicePixelRatio. If you use the standard canvas dimensions without multiplying by the pixel ratio, the image may appear blurry on high-resolution screens.

Can I capture pages that require a login?

Yes, because the extension runs within the user's browser session, it has access to the page as the user sees it, including authenticated content, provided the content is rendered in the DOM.

Why does the browser show a "debugger" warning?

This happens if you use the chrome.debugger API. Chrome displays this warning for security reasons to ensure users know an external tool is controlling the browser's internal protocols.

Is it possible to capture only a specific element?

Yes. Instead of scrolling the whole page, you can use the content script to find the getBoundingClientRect() of a specific element and then only capture the area defined by those coordinates.