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?
Browser window only Ensure that your browser is that “active window” by clicking anywhere in the browser window. Press Alt + Print Screen (may also say Prnt Scrn , or another variation). This takes a screenshot of the browser and copies it to the clipboard. Paste the image into a ticket or email by pressing Ctrl + V .
Movavi ScreenShot is a screenshot Chrome extension that lets you grab full and partial screen captures on Chrome. It's one of the best screenshot add-ons that you can find in the Google Web store. To make a screengrab and save it at high quality is easier than you might think.
The Chrome Shortcut for Taking a Full-Page Screenshot These keyboard shortcuts will open Chrome's developer menu. Just type "screenshot" and you'll see the option appear to "capture full size screenshot." Simply select this and Chrome will automatically save a full-page screenshot to your Downloads folder!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With