How to Inject Javascript to a Site From Chrome Extension

Link to original article (recommended): https://gabriellazcano.com/blog/how-to-inject-javascript-to-a-site-from-chrome-extension/

Introduction

I was watching a Twitch streamer struggle trying to do this. So I thought it might help someone o…


This content originally appeared on DEV Community and was authored by Gabriel Lazcano

Link to original article (recommended): https://gabriellazcano.com/blog/how-to-inject-javascript-to-a-site-from-chrome-extension/

Introduction

I was watching a Twitch streamer struggle trying to do this. So I thought it might help someone out there.

You can inject javascript code into any site with a chrome extension, with this you can do many things. Like creating new elements and adding them to the DOM or managing events of certain elements, what you can do in your application you can do it when injecting it.

Manifest version 3

If you want your script to run on a set of pages you have defined, the manifest for your chrome extension which is needed for it to work (you can have a look a the documentation), needs to have some additional elements. content_scripts and host_permissions

Both the matches and host_permissions should specify match patterns. In this example inject.js only runs whenever the site is google.com, and you have permissions in all the urls.

{
    "name": "inject",
    "version": "1.0",
    "manifest_version": 3,
    "content_scripts": [
        {
            "matches": ["*://*.google.com/*"],
            "js": ["inject.js"],
        }
    ],
    "host_permissions": ["<all_urls>"],
}

This is an example of the code that can be injected. You can add event listeners, get and add elements from the DOM as I mentioned before.

function init() {
    const el = document.createElement("input")
    el.setAttribute("type", "checkbox")
    document.body.appendChild(el)
    el.addEventListener("click", (event) => {
        console.log(event.target.checked)
    })
}

init()


This content originally appeared on DEV Community and was authored by Gabriel Lazcano


Print Share Comment Cite Upload Translate Updates
APA

Gabriel Lazcano | Sciencx (2021-07-12T23:23:04+00:00) How to Inject Javascript to a Site From Chrome Extension. Retrieved from https://www.scien.cx/2021/07/12/how-to-inject-javascript-to-a-site-from-chrome-extension/

MLA
" » How to Inject Javascript to a Site From Chrome Extension." Gabriel Lazcano | Sciencx - Monday July 12, 2021, https://www.scien.cx/2021/07/12/how-to-inject-javascript-to-a-site-from-chrome-extension/
HARVARD
Gabriel Lazcano | Sciencx Monday July 12, 2021 » How to Inject Javascript to a Site From Chrome Extension., viewed ,<https://www.scien.cx/2021/07/12/how-to-inject-javascript-to-a-site-from-chrome-extension/>
VANCOUVER
Gabriel Lazcano | Sciencx - » How to Inject Javascript to a Site From Chrome Extension. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/12/how-to-inject-javascript-to-a-site-from-chrome-extension/
CHICAGO
" » How to Inject Javascript to a Site From Chrome Extension." Gabriel Lazcano | Sciencx - Accessed . https://www.scien.cx/2021/07/12/how-to-inject-javascript-to-a-site-from-chrome-extension/
IEEE
" » How to Inject Javascript to a Site From Chrome Extension." Gabriel Lazcano | Sciencx [Online]. Available: https://www.scien.cx/2021/07/12/how-to-inject-javascript-to-a-site-from-chrome-extension/. [Accessed: ]
rf:citation
» How to Inject Javascript to a Site From Chrome Extension | Gabriel Lazcano | Sciencx | https://www.scien.cx/2021/07/12/how-to-inject-javascript-to-a-site-from-chrome-extension/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.