Michael Spector
Michael Spector

Reputation: 37019

How to capture a screenshot of a single HTML element in Chrome extension?

I know there's a captureVisibleTab, but how do I cut the resulted screenshot of a tab so only a single HTML element is left?

Upvotes: 9

Views: 10487

Answers (1)

user4017309
user4017309

Reputation:

For this you need onMessage, sendMessage, and captureVisibleTab. The onMessage is a method of chrome.runtime, sendMessage, and captureVisibleTab are both methods of tabs.

Manifest

In the manifest file you must to add the tabs, and <all_urls> permissions to your manifest file. This will not work without the permissions.

"permissions": [
    "tabs",
    "<all_urls>"
],

Content Script

In your content script file you need to add the following. This allows you to communicate with your background page to take a screenshot of your active tab.

chrome.runtime.sendMessage({ chromeAction: "screenshot" }, function (imageString) {
    console.log(imageString);
});

Background Script/Page

Here is where the magic happens.

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    if (request.chromeAction === "screenshot") {
        createScreenshot(function (dataURL) {
            createImage(dataURL);
        });
        return true;
    }
});

// here we create a new image
function createImage(dataURL) {
    // create a canvas
    var canvas = createCanvas(500, 500);
    // get the context of your canvas
    var context = canvas.getContext('2d');
    // create a new image object
    var croppedImage = new Image();

    croppedImage.onload = function() {
        // this is where you manipulate the screenshot (cropping)
        // parameter 1: source image (screenshot)
        // parameter 2: source image x coordinate
        // parameter 3: source image y coordinate
        // parameter 4: source image width
        // parameter 5: source image height
        // parameter 6: destination x coordinate
        // parameter 7: destination y coordinate
        // parameter 8: destination width
        // parameter 9: destination height
        context.drawImage(croppedImage, 10, 10, 300, 300, 0, 0, 250, 250);

        // canvas.toDataURL() contains your cropped image
        console.log(canvas.toDataURL());
    };
    croppedImage.src = dataURL; // screenshot (full image)
}

// creates a canvas element
function createCanvas(canvasWidth, canvasHeight) {
    var canvas = document.createElement("canvas");

    // size of canvas in pixels
    canvas.width = canvasWidth;
    canvas.height = canvasHeight;
    return canvas;
}

// calling the captureVisibleTab method takes a screenhot
function createScreenshot(callback) {
    // you can have two image formats (jpeg and png)
    // for jpeg use { format: "jpeg", quality: 100 } (you can adjust the jpeg image quality from 0-100) 
    // for png use { format: "png" }
    chrome.tabs.captureVisibleTab(null, { format: "png" }, callback);
}

Cropping

For a better understanding of the drawImage canvas method read the full documentation.

Upvotes: 6

Related Questions