https://github.com/bluzi/chrome-extension-execute-on-website
Access JS on web pages directly through your Chrome extensions
https://github.com/bluzi/chrome-extension-execute-on-website
Last synced: 12 months ago
JSON representation
Access JS on web pages directly through your Chrome extensions
- Host: GitHub
- URL: https://github.com/bluzi/chrome-extension-execute-on-website
- Owner: bluzi
- Created: 2017-09-28T21:19:36.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-11-16T13:17:57.000Z (over 7 years ago)
- Last Synced: 2024-11-29T23:26:57.653Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/chrome-extension-execute-on-website
- Size: 8.79 KB
- Stars: 9
- Watchers: 4
- Forks: 11
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# chrome-extension-execute-on-website
It is a long name, but it will self-explanatory so, its worth
Or is it?...
## What is it?
It turns out that Chrome extensions does not have access to the JavaScript on a webpage, even if the extension is a content script. As a result, you can't access variables and content on the page itself.
It's disapointing. Anyway, there is a solution, you have to inject a script tag to the page and then execute whatever you want in this script. You may ask yourself what is the problem? It's ugly as f, that's the problem.
Well, luckily, I've got the solution!
This tiny library allows you to easily execute JavaScript code in a webpage from your Chrome extension.
## Usage
But how does it work? It is as simple as this:
```javascript
exec(() => {
console.log('This is the window of the current webpage:', window);
});
```
Nice, huh? I know!
Anyway, so that's more or less it. Best 500 bytes (unminified) your Chrome extension is going to get.
## Installation
### jsDelivr (Recommended)
I just love jsDelivr.
1. Download the script from the this --> [URL](https://cdn.jsdelivr.net/npm/chrome-extension-execute-on-website/execute-on-website.min.js) <--, and put it somewhere in your extension's folder:
2. Go to your manifest file, make sure you have `contentSettings` permission, like this:
```json
"permissions": [
"contentSettings"
]
```
Under `content_scripts` add a `js` array and insert the path to the library file as an item.
No worries, here's an example:
Assuming your script is in a folder called `js`:
```json
"content_scripts": [
{
"js": [
"js/execute-on-website.min.js",
"./inject.js"
]
}
]
```
It's as simple as that.
### npm
Navigate to your extension's folder, and run the following command: (After making sure you have Node installed)
```bash
npm i chrome-extension-execute-on-website
```
Good, now go to your `manifest.json`, make sure you have `contentSettings` permission:
```json
"permissions": [
"contentSettings"
]
```
Under `content_scripts` add `js` array and add the following path as an item:
`node_modules/chrome-extension-execute-on-website/execute-on-website.js`
And that should do the job. Here is an example:
```json
"content_scripts": [
{
"js": [
"node_modules/chrome-extension-execute-on-website/execute-on-website.js",
"./inject.js"
]
}
]
```
## Sample Manifest
Here is an example of a full manifest, with the library included:
```json
{
"name": "My Extension",
"version": "0.0.4",
"manifest_version": 2,
"description": "",
"homepage_url": "http://eliran.net",
"icons": {
"16": "icons/Lightning16.png",
"19": "icons/Lightning19.png",
"48": "icons/Lightning48.png",
"128": "icons/Lightning128.png"
},
"permissions": [
"contentSettings"
],
"content_scripts": [
{
"matches": [
"*://*/*"
],
"js": [
"js/execute-on-website.min.js",
"./inject.js"
]
}
]
}
```