• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a ScreenCapture Chrome Extension using JS and HTML2Canvas Tutorial

ffaez126711

Operational Technology Auditor
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
77
Likes
154
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. "name"

    :

    "Simple Screencapture Extension"

    ,
  4. "description"

    :

    "This extension allows you to Screencapture the site."

    ,
  5. "version"

    :

    "1.0"

    ,
  6. "icons"

    :

    {
  7. "16"

    :

    "screencapture-icon.png"

    ,
  8. "48"

    :

    "screencapture-icon.png"

    ,
  9. "128"

    :

    "screencapture-icon.png"
  10. }

    ,
  11. "action"

    :

    {
  12. "default_icon"

    :

    "screencapture-icon.png"

    ,
  13. "default_popup"

    :

    "popup.html"
  14. }

    ,
  15. "permissions"

    :

    [

    "tabs"

    ,

    "scripting"

    ]

    ,
  16. "content_scripts"

    :

    [

    {
  17. "matches"

    :

    [
  18. "<all_urls>"
  19. ]

    ,
  20. "js"

    :

    [

    "page_content_script.js"

    ,

    "html2canvas.min.js"

    ]
  21. }

    ]
  22. }

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. <head

    >
  4. <title

    >

    Simple Screen Capture Extension</

    title

    >
  5. <style

    >
  6. body {
  7. width: 250px;
  8. padding: 1.5em 1em;
  9. background: #000;
  10. box-shadow: 0px 0px 7px #fff;
  11. margin: 2px;
  12. }
  13. .text-center{
  14. text-align: center;
  15. }
  16. .img-holder{
  17. display: flex;
  18. width: 100%;
  19. align-items: center;
  20. justify-content: center;
  21. }
  22. .img-holder>div{
  23. width: 65px;
  24. height: 65px;
  25. }
  26. .img-holder>div>img{
  27. width: 100%;
  28. height: 100%;
  29. object-fit: cover;
  30. object-position: center center;
  31. }
  32. .text-muted{
  33. color:#d0d0d0;
  34. }
  35. </

    style

    >
  36. </

    head

    >
  37. <body

    >
  38. <div

    class

    =

    "img-holder"

    >
  39. <div

    >
  40. <img

    src

    =

    "./screencapture-icon.png"

    alt

    =

    "ScreenCapture"

    >
  41. </

    div

    >
  42. </

    div

    >
  43. <h3

    class

    =

    "text-center"

    style

    =

    "color:#fff"

    >

    ScreenCapture</

    h3

    >
  44. <div

    class

    =

    "text-center text-muted"

    ><small

    ><i

    >

    Capture the site page by clicking the button below</

    i

    ></

    small

    ></

    div

    >
  45. <br

    >
  46. <div

    class

    =

    "text-center"

    >
  47. <button

    class

    =

    "btn-capture"

    type

    =

    "button"

    id

    =

    "capturePage"

    >

    Capture</

    button

    >
  48. </

    div

    >
  49. <script

    src

    =

    "capture.js"

    ></

    script

    >

  50. </

    body

    >

  51. </

    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. // Send Message to the Active Page to trigger screen capturing
  10. function

    capture(

    e)

    {
  11. chrome.tabs

    .sendMessage

    (

    e[

    0

    ]

    .id

    ,

    {

    action:

    'capture'

    }

    )
  12. }

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. // Screen Capture Extension's Callback [Receiving message]
  5. function

    ScreenCaptureExtCB(

    msg)

    {
  6. console.log

    (

    msg)
  7. if

    (

    msg.action

    ==

    "capture"

    )

    {
  8. html2canvas(

    document.body

    )

    .then

    (

    canvas =>

    {
  9. var

    blob =

    canvas.toBlob

    (

    (

    blob)

    =>

    {
  10. url =

    window.URL

    .createObjectURL

    (

    blob)
  11. window.open

    (

    url)
  12. }

    ,

    "image/png"

    )
  13. }

    )

    ;
  14. }
  15. }


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

Step 2: Enable Developer Mode

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

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.

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.

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.

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 :)


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,736

324,744

Top