Creating a ScreenCapture Chrome Extension using JS and HTML2Canvas Tutorial

In this tutorial, you will learn how to create a Screencapture Chrome Extension using JavaScript and HTML2Canvas Library. This tutorial aims to provide IT/CS students and new programmers with a reference on how they can manipulate the different site pages by creating a browser extension. Here, a step-by-step tutorial with snippets will be provided. The source code zip file of the chrome extension that can screenshot the site page that I created is also provided and free to download.

Creating the Screencapture Chrome Extension

Step 1: Creating the manifest file

First, we will need to create the manifest.json file of our chrome extension script. This is a JSON file that contains the information of our extension and the scripts that will be loaded when browsing the sites.

  1. {
  2. "manifest_version": 3,
  3.  
  4. "name": "Simple Screencapture Extension",
  5. "description": "This extension allows you to Screencapture the site.",
  6. "version": "1.0",
  7. "icons": {
  8. "16": "screencapture-icon.png",
  9. "48": "screencapture-icon.png",
  10. "128": "screencapture-icon.png"
  11. },
  12. "action": {
  13. "default_icon": "screencapture-icon.png",
  14. "default_popup": "popup.html"
  15. },
  16. "permissions": ["tabs", "scripting"],
  17. "content_scripts": [{
  18. "matches": [
  19. "<all_urls>"
  20. ],
  21. "js": ["page_content_script.js", "html2canvas.min.js"]
  22. }]
  23. }

Step 2: Creating the Popup Interface

Next, we will create an HTML file that contains the elements of the extension's popup interface. The interface contains an icon, headings, text, and a button. Save the file as popup.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <title>Simple Screen Capture Extension</title>
  4. body {
  5. width: 250px;
  6. padding: 1.5em 1em;
  7. background: #000;
  8. box-shadow: 0px 0px 7px #fff;
  9. margin: 2px;
  10. }
  11. .text-center{
  12. text-align: center;
  13. }
  14. .img-holder{
  15. display: flex;
  16. width: 100%;
  17. align-items: center;
  18. justify-content: center;
  19. }
  20. .img-holder>div{
  21. width: 65px;
  22. height: 65px;
  23. }
  24. .img-holder>div>img{
  25. width: 100%;
  26. height: 100%;
  27. object-fit: cover;
  28. object-position: center center;
  29. }
  30. .text-muted{
  31. color:#d0d0d0;
  32. }
  33. </style>
  34. </head>
  35. <div class="img-holder">
  36. <div>
  37. <img src="./screencapture-icon.png" alt="ScreenCapture">
  38. </div>
  39. </div>
  40. <h3 class="text-center" style="color:#fff">ScreenCapture</h3>
  41. <div class="text-center text-muted"><small><i>Capture the site page by clicking the button below</i></small></div>
  42. <br>
  43. <div class="text-center">
  44. <button class="btn-capture" type="button" id="capturePage">Capture</button>
  45. </div>
  46. <script src="capture.js"></script>
  47.  
  48. </body>
  49.  
  50. </html>

Step 3: Creating the Extension's Script

Next, create a new JavaScript file and save it as capture.js. This JS file contains the script that will send a message to the page/tab client when the button "Capture" has been clicked. It will send a message that will trigger the screenshot script on the page/tab client.

  1. // Capture Button Event Listener
  2. document.querySelector("#capturePage").addEventListener("click", function(){
  3. let tab_params = {
  4. active: true,
  5. currentWindow: true
  6. }
  7. chrome.tabs.query(tab_params, capture)
  8.  
  9. })
  10.  
  11. // Send Message to the Active Page to trigger screen capturing
  12. function capture(e) {
  13. chrome.tabs.sendMessage(e[0].id, {action:'capture'})
  14. }

Step 4: Creating the Extension's Content Scripts

Next, we will create the extension's content JavaScript file. This JS file will be loaded on the browsed site. It contains the scripts that listen to incoming messages from the extension script. If the button "Capture" has been clicked, this file will receive the message to trigger the screen-capturing script. Save this file as page_content_script.js.

  1. console.log("Screen Capture Extension Script has loaded")
  2. // Listen to the messages sent by the background script
  3. chrome.runtime.onMessage.addListener(ScreenCaptureExtCB)
  4.  
  5. // Screen Capture Extension's Callback [Receiving message]
  6. function ScreenCaptureExtCB(msg) {
  7. console.log(msg)
  8. if(msg.action == "capture"){
  9. html2canvas(document.body).then(canvas => {
  10. var blob = canvas.toBlob((blob) => {
  11. url = window.URL.createObjectURL(blob)
  12. window.open(url)
  13. }, "image/png")
  14. });
  15. }
  16. }
  17.  

Step 5: Download the Libraries Files

Lastly, download and save the HTML2Canvas and jQuery Libraries files into your source code directory. please make sure that the filenames are exactly the same on the script that loads the libraries. For the HTML2Canvas, the file is loaded at the manifest.json file. The jQuery library is loaded in the popup.html file. Please download also the icon/image that you want to use for the extension.

Here are the following links to download the library files

How to Upload the Extension Source Code in Chrome Browser

Step 1: Go to Chrome's Extensions Page

To upload the extension into your chrome browser, open the browser and browse chrome://extensions

 

Chrome's Extensions

Step 2: Enable Developer Mode

To enable the Developer Mode, toggle/click the Developer Mode toggle located at the upper right of the page.

 

Chrome's Extensions - developer mode

Step 3: Load Unpack

Next, load the ScreenCapture Extension source code that we created. To do that, click the Load Unpacked button located at the upper left of the page.

 

Chrome's Extensions - Load Unpacked

 

Chrome's Extensions - Loaded

That's you can now check the ScreenCapture Extension in the extensions menu of the Chrome Browser. The extensions menu is located beside the address bar.

 

Chrome's Extensions - Extension Menu

That's it! You can now test ScreenCapture Extension in your browser. To test it, browse any site and click the Capture button on the extensions popup interface. The captured image will be shown in a new tab of the browser.

Snapshots

Here's the snapshot of the ScreenCapture Extension popup interface.

 

ScreenCapture Extension

That's it! That's the end of this tutorial. I hope this ScreenCapture Chrome Extension Tutorial will help you with what you are looking for and that you'll find this useful for your current and future projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)

Add new comment