Troubleshoot Browser Screenshot API Permission Errors
In this article
This guide walks you through diagnosing and fixing permission errors when using the browser's Screenshot API (like getDisplayMedia() or html2canvas with cross-origin images). You'll learn to identify the exact cause, adjust browser settings, and handle user denial gracefully.
What You'll Need
- A modern browser (Chrome, Firefox, Edge, or Safari) with developer tools open.
- Access to the website or web app where the screenshot feature is failing.
- Basic familiarity with the browser's console (F12 or right-click → Inspect).
Step-by-Step
- Reproduce the error and capture the console message. Open the browser's developer console (F12) and trigger the screenshot. Look for a red error like
NotAllowedError,SecurityError, orPermissionDeniedError. Note the exact wording — it usually points to the root cause. - Check the browser's permission state for the site. Click the padlock icon next to the URL in the address bar. Look for "Screen Capture" or "Screen Recording" permissions. If it says "Blocked" or "Ask", change it to "Allow" or "Ask" and reload the page.
- Verify the page is served over HTTPS (or localhost). The Screenshot API (especially
getDisplayMedia()) requires a secure context. If the URL starts withhttp://(excepthttp://localhost), the browser will block it. Switch to HTTPS or use a local server for testing. - Test in an incognito/private window. Extensions or cached permissions can interfere. Open a new incognito window, navigate to the site, and try again. If it works there, the issue is likely an extension or a stale permission setting.
- Check for iframe or cross-origin issues. If your screenshot code runs inside an iframe, the parent page must grant the
allow="display-capture"attribute. Also, if you're capturing a canvas that includes cross-origin images, those images must havecrossOrigin="anonymous"set, and the server must send CORS headers. Otherwise, the canvas becomes "tainted" and the API throws aSecurityError. - Review your code for missing or incorrect constraints. When calling
getDisplayMedia(), you must pass avideo: trueconstraint. Some browsers also requireaudio: falseif you don't need sound. Example:navigator.mediaDevices.getDisplayMedia({ video: true, audio: false }). - Handle user denial gracefully. The API requires explicit user consent. If the user clicks "Cancel" or "Deny", the promise rejects with
NotAllowedError. Add acatchblock to show a friendly message like "Screen capture was denied. Please allow it in your browser settings." - Test with a different browser. Permission handling varies. If the issue persists in Chrome but works in Firefox, the problem is browser-specific. Check the browser's support and known issues for the API you're using.
- Clear site data and reset permissions. Go to browser settings → Privacy and Security → Site Settings → View permissions and data stored across sites. Find your site, click the trash icon to clear all stored data, then reload and try again.
- Update your browser. Older versions may have bugs or incomplete implementations. Ensure you're on the latest stable release.
Common Mistakes
- Forgetting to use
navigator.mediaDevices.getDisplayMedia()— some developers mistakenly usegetUserMedia()which captures a camera, not the screen. - Calling the API before the page is fully loaded — permission prompts may not appear if the page is still loading.
- Not checking if the browser supports the API (e.g., Safari uses
getDisplayMediaonly from version 13+). - Assuming the permission is permanent — browsers often reset permissions after a site is closed or after a certain time.
- Ignoring the
crossOriginattribute on images — this causes a tainted canvas and aSecurityErroreven if permissions are fine.
Tips
- Use
navigator.permissions.query({ name: 'display-capture' })to check the current permission state before calling the API. This can help you show a custom prompt. - Always wrap the API call in a
try...catchblock and log the error object to the console. The error'snameproperty (e.g.,NotAllowedError) is more reliable than the message. - For debugging, add a temporary
console.logto see if the promise resolves or rejects, and inspect the stream tracks. - If you're using a library like
html2canvas, remember it cannot capture cross-origin images without CORS headers — you may need to proxy them. - Test on a live server (even a local one) rather than opening the HTML file directly from disk — some APIs are blocked in
file://contexts.
Troubleshooting
- Error:
NotAllowedError— User denied the prompt or the permission is blocked. Check the site's permission settings and try again. If you're in an iframe, ensure theallowattribute is set. - Error:
SecurityError— The page is not in a secure context, or the canvas is tainted. Switch to HTTPS, or fix CORS headers on external images. - No prompt appears at all — The API might not be supported, or the page is in an insecure context. Check the console for a different error, and verify the page is served over HTTPS.
- Works in one browser but not another — Check browser-specific permission settings. For example, Firefox may require you to select a specific window or screen every time, while Chrome remembers your choice.
- Permission resets after reload — Some browsers, like Safari, may not persist screen capture permissions. You may need to re-request each time, which is expected behavior.
- Error:
TypeError: Cannot read properties of undefined (reading 'getDisplayMedia')— The API is not available in that browser. Use a feature detection check likeif (navigator.mediaDevices?.getDisplayMedia).
Frequently Asked Questions
Why does the browser ask for permission every time?
Browsers treat screen capture as a sensitive permission. Many require explicit consent on each call, especially if the user hasn't chosen "Always allow" (which some browsers don't offer for this API). This is a security feature.
Can I bypass the permission prompt?
No. The Screenshot API is designed to require user consent. Attempting to bypass it would violate browser security policies and is not possible with standard web APIs.
Does the API work on mobile browsers?
Most mobile browsers do not support getDisplayMedia() for capturing the entire screen. Some support capturing a tab or a specific element, but support is limited. Check the browser's compatibility table.
What is a tainted canvas and how do I avoid it?
A canvas becomes tainted when you draw an image from a different origin without CORS headers. Once tainted, any attempt to read its pixels (including screenshots) throws a SecurityError. Avoid this by setting crossOrigin="anonymous" on the image element and ensuring the server sends Access-Control-Allow-Origin headers.
How do I capture a specific element instead of the whole screen?
You can use getDisplayMedia() with the preferCurrentTab hint (Chrome) or use a library like html2canvas for DOM-based capture. For getDisplayMedia, you cannot select an element directly — the user chooses the window or tab.
Why does the screenshot come out black or empty?
This often happens when capturing a tab that has hardware acceleration disabled or when the screen is locked. Also, if you're capturing a video element, ensure it's playing. Try enabling hardware acceleration in browser settings.