74

I have made a lot of search regarding taking pictures using JS but none seem to be useful. Some say using activeX controls, which doesn't suit my situation. I was hoping to take picture using JS and upload it a server.

Sam R.
  • 16,027
  • 12
  • 69
  • 122
Sridarshan
  • 1,268
  • 3
  • 13
  • 15

5 Answers5

92

Since you're using this in Chrome Extensions, the Tab API has a method called captureVisibleTab, which allows captures the visible area of the currently selected tab in the specified window.

To use that you just add "tabs" to your permissions manifest. And from your background page, or popup (or any other extension page), you just call that method like this:

chrome.tabs.captureVisibleTab(null, {}, function (image) {
   // You can add that image HTML5 canvas, or Element.
});

You can control the property by adding {quality: 50} and change the format too, all described within the docs mentioned above.

The beauty of HTML5, you can alter that image with HTML5 Canvas, you can manipulate, transform, modify, clip, anything you want, very easily!

Hope that is what your looking for! Happy New Years!

Doug
  • 14,387
  • 17
  • 74
  • 104
Mohamed Mansour
  • 39,445
  • 10
  • 116
  • 90
  • 2
    I have added permission for tabs, but when I use alert the 'image', I get 'undefined'. Have you got any idea why? – Ziyan Junaideen Nov 20 '13 at 07:55
  • Any way to make screenshot of not visible tab? – Flash Thunder Feb 09 '15 at 16:14
  • 1
    to take a screenshot, you need permission – Vlas Bashynskyi Aug 05 '15 at 06:40
  • 3
    Full-page screenshots are now available in Chrome 59. Do you know if this feature is accessible via the Extensions API? https://developers.google.com/web/updates/2017/04/devtools-release-notes#screenshots – skibulk Jul 04 '17 at 19:24
  • @skibulk I think in 59 that feature is done by scrolling down and stitching together visible screen areas anyway. But yeah it would be real nice to have it done for us :) instead of having to image process... – Jonathan Lin Jul 14 '17 at 05:12
  • @VlasBashynskyi what is the all_urls permission? is not listed here https://developer.chrome.com/extensions/declare_permissions – Enrique Mar 20 '18 at 16:49
  • @Enrique https://stackoverflow.com/questions/16096482/what-does-http-https-and-all-urls-mean-in-the-context-of-ch – Vlas Bashynskyi Mar 21 '18 at 11:46
31

I'm not sure if this was available when the original answer was given, but Google now has an example available that shows how to take screenshots:

http://developer.chrome.com/extensions/samples.html

Search for "Test Screenshot Extension" on this page.

UPDATE: Here's the new example using the desktopCapture API:

https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/apps/samples/desktop-capture

Todd Price
  • 2,650
  • 1
  • 18
  • 26
7

If you are looking for working example, I have created repo with extension which take screenshot of the entire web page. Take a look here: https://github.com/marcinwieprzkowicz/take-screenshot

4

Here is another approach that worked for me.
The requirements were as follows:
(a) capture a screenshot in a chrome extension
(b) the screenshot must have a transparent background
(c) the screenshot must be communicated to a different process (through HTTP)

In this section i will present a code fragment addressing requirement (b)
Useful references are:
chrome extensions debugger api
chrome devtools protocol debugger domain
You may want to start reading code from the last function attachToDebugger

function captureScreenshot(tabId) {

    logMsg(`{page}: captureScreenshot: status=aboutTo, tabId=${tabId}`);

    chrome.debugger.sendCommand(
        {tabId:tabId},
        "Page.captureScreenshot", 
        {format: "png", fromSurface: true},
        response => {
            if(chrome.runtime.lastError) {
                logMsg(`{back}: captureScreenshot: status=failed, tabId=${tabId}`);
            }
            else {
                var dataType = typeof(response.data);
                logMsg(`{back}: captureScreenshot: status=success, tabId=${tabId}, dataType=${dataType}`);
                saveScreenshotRemotely(response.data);
            }
        });

    logMsg(`{page}: captureScreenshot: status=commandSent, tabId=${tabId}`);
}

//---------------------------------------------------------------------------

function setColorlessBackground(tabId) {

    logMsg(`{back}: setColorlessBackground: status=aboutTo, tabId=${tabId}`);

    chrome.debugger.sendCommand(
        {tabId:tabId}, 
        "Emulation.setDefaultBackgroundColorOverride",
        {'color': {'r': 0, 'g': 0, 'b': 0, 'a': 0}},
        function () {
            logMsg(`{back}: setColorlessBackground: status=enabled, tabId=${tabId}`);
            captureScreenshot(tabId);
        });

    logMsg(`{back}: setColorlessBackground: status=commandSent, tabId=${tabId}`);
}

//---------------------------------------------------------------------------

function enableDTPage(tabId) {

    logMsg(`{back}: enableDTPage: status=aboutTo, tabId=${tabId}`);

    chrome.debugger.sendCommand(
        {tabId:tabId}, 
        "Page.enable", 
        {}, 
        function () {
            logMsg(`{back}: enableDTPage: status=enabled, tabId=${tabId}`);
            setColorlessBackground(tabId);
            /*
             * you can comment 
             * setColorlessBackground(tabId);
             * and invoke 
             * captureScreenshot(tabId);
             * directly if you are not interested in having a 
             * transparent background
             */
        });

    logMsg(`{back}: enableDTPage: status=commandSent, tabId=${tabId}`);
}

//---------------------------------------------------------------------------

function attachToDebugger(tabId) {
    chrome.debugger.attach(
        {tabId:tabId}, 
        g_devtools_protocol_version,
        () => {
            if (chrome.runtime.lastError) {
                alert(chrome.runtime.lastError.message);
                logMsg(`{back}: debugger attach failed: error=${chrome.runtime.lastError.message}`);
            }
            else {
                logMsg(`{back}: debugger attach success: tabId=${tabId}`);
                enableDTPage(tabId);
            }
        });
}
ksridhar
  • 189
  • 2
  • 9
  • This works way better than captureVisibleTab API provided your extension can attach the debugger. You can also launch Chrome with the `--silent-debugger-extension-api` to avoid the info bar stating the extension has started debugging. – Max Nov 23 '20 at 17:48
1

If you’re inside an enterprise, your IT might set the policy DisableScreenshots to true. You can check it by going into chrome://policy and search for this key.

YRabl
  • 41
  • 2