User Agents

Web browsers and everyday web tech, explained

Capture Element Screenshot in Chrome Extension

· 5 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

To capture a screenshot of a specific element (like a button, image, or div) in a Chrome extension, you use the Chrome tabs.captureVisibleTab API to grab the whole visible tab, then draw that image onto a canvas and crop it to the element's bounding rectangle. This works in Manifest V3 extensions with the activeTab or tabs permission. You'll need basic knowledge of JavaScript, HTML canvas, and Chrome extension APIs.

What You'll Need

  • A Chrome extension project (manifest.json, background script, and popup or content script).
  • Manifest V3 (the current extension format).
  • Permission activeTab or tabs in your manifest.
  • Access to the target element from a content script (or the ability to inject one).
  • Basic familiarity with Promises and async/await.

Step-by-Step

  1. Set up your manifest.json. Declare "permissions": ["activeTab"] (or "tabs" if you need it on all pages). Also add a content script that runs on the pages where you want the screenshot, and a popup or action button to trigger the capture.
  2. Create a content script that can locate the target element. For example, if you want to capture a button with id #my-button, the content script can find it and return its bounding rectangle using getBoundingClientRect().
  3. In your popup or background script, send a message to the content script to get the element's position. Use chrome.tabs.sendMessage with a custom message like {type: "GET_ELEMENT_RECT", selector: "#my-button"}.
  4. In the content script, listen for that message, find the element, and respond with its rectangle: {x: rect.x, y: rect.y, width: rect.width, height: rect.height}. Make sure the element is in the viewport; if not, scroll it into view first with element.scrollIntoView().
  5. Capture the visible tab. In the background or popup script, call chrome.tabs.captureVisibleTab() to get a data URL of the current tab's visible area. This returns a PNG image as a base64 string.
  6. Create an offscreen canvas. In the popup or background, load the captured image into an Image object, then draw it onto a canvas of the same size as the captured image.
  7. Crop the canvas to the element's rectangle. Create a second canvas with dimensions equal to the element's width and height. Use ctx.drawImage(sourceCanvas, rect.x, rect.y, rect.width, rect.height, 0, 0, rect.width, rect.height) to copy just that region.
  8. Convert the cropped canvas to a data URL. Call canvas.toDataURL('image/png') to get the final screenshot as a base64 string.
  9. Use or download the screenshot. You can display it in the popup, send it to a server, or trigger a download using chrome.downloads.download() with the data URL as the URL.

Common Mistakes

  • Not waiting for the image to load. The Image object must fire its onload event before you can draw it onto the canvas. Use await new Promise(...) or the decode() method.
  • Using the wrong coordinates. getBoundingClientRect() returns viewport-relative coordinates, which are correct for captureVisibleTab because that captures the visible viewport. But if the page is scrolled, ensure you scroll the element into view first and recalculate after scrolling.
  • Canvas tainting. If the page has cross-origin images or CSS that draws from other origins, the canvas becomes tainted and toDataURL() throws a security error. You cannot avoid this without a CORS-enabled server.
  • Forgetting to set canvas dimensions. If you don't set the canvas width and height, it defaults to 300x150, and your crop will be clipped.
  • Using tabs.captureVisibleTab without permission. This API requires activeTab or tabs permission; otherwise it returns an error.

Tips

  • Use devicePixelRatio for high-DPI displays. Multiply the rectangle coordinates and dimensions by window.devicePixelRatio to get a sharp screenshot.
  • If the element is partially off-screen, scroll it fully into view before capturing. You can use element.scrollIntoView({block: 'center'}) to center it.
  • For elements that change on hover (like tooltips), you might need to simulate hover or wait for the state to settle before capturing.
  • Consider adding a small delay after scrolling to allow the page to repaint.
  • If you need to capture elements that are inside iframes, you'll need to handle cross-origin restrictions or use a different approach.

Troubleshooting

  • Error: "Cannot access a chrome:// URL" – The extension cannot capture built-in Chrome pages. The user must navigate to a regular web page.
  • Error: "Extension manifest invalid" – Check that your permissions are spelled correctly and that you're using Manifest V3 syntax (e.g., "manifest_version": 3).
  • Blank or black screenshot – This often happens if the tab is not fully loaded or if the element is not in the viewport. Ensure the tab is active and visible, and that the element is on-screen.
  • SecurityError when calling toDataURL() – The canvas is tainted. You cannot fix this for arbitrary web pages; you can only capture pages that don't have cross-origin resources.
  • Coordinates are off after scrolling – Re-query getBoundingClientRect() after scrolling, because the rectangle changes as the page moves.
  • Message not received – Make sure the content script is injected into the page. Check the console for errors and verify that the content script's matches pattern covers the URL.

Frequently Asked Questions

Do I need the tabs permission or is activeTab enough?

activeTab is sufficient if the user invokes the extension (e.g., clicks the action button) on the page. It grants temporary access to the active tab. If you need to capture tabs without user interaction, you need tabs permission.

Can I capture an element that is not currently visible on the screen?

No, captureVisibleTab only captures what is visible in the viewport. You must scroll the element into view first. There is no built-in way to capture off-screen content.

How do I get the element's position from the background script?

You send a message to the content script asking for the rectangle. The content script runs in the page's context and can access the DOM.

Why does my screenshot come out blurry on a 4K monitor?

Because the canvas uses CSS pixels. Multiply the coordinates and dimensions by window.devicePixelRatio (often 2 or 3) before cropping to get a sharp image.

Can I capture an element inside an iframe?

Yes, if the iframe is same-origin. You can access the iframe's document from the content script and get the element's position relative to the top window. For cross-origin iframes, you cannot access the DOM due to security restrictions.

Is there a way to capture a full-page element that is taller than the viewport?

Not with captureVisibleTab alone. You would need to scroll and stitch multiple captures together, which is complex. For a single element, it's better to scroll it fully into view and capture just that portion.