There are plenty of tutorials and guides on how to make a chome extension and there are hundeds for jQuery manipulation of the DOM. There are also a few about using jQuery in a chome extension, but only for the popup window. There seems to be a lack of using jQuery to actually do something on a page. This is something that too me a while to get my head around and a bit of googling to figure out, so now it's time to share.
Create a folder for the extension files. It's probably best to name it after your extension so you can find it later on.
In a plain text editor create 3 blank documents. You can save the files as manifest.json
, background.js
and background.html
.
Using your favorite image editing software create a png icon with a transparent background (20px x 20px should be ok for now). Chome should scale the icon for the different areas it shows. Later we can create different resolution icons and tell chrome to use them.
Get a copy of jQuery and add it to the folder.
In your folder you should now have the following files:
manifest.json
background.html
background.js
icon.png
jquery.min.js
Let's go over what each of the files will contain, and then what that does.
manifest.json
{
"name": "jQuery DOM",
"version": "1",
"description": "Manipulate the DOM when the page is done loading",
"background_page": "background.html",
"browser_action": {
"name": "Manipulate DOM",
"icons": ["icon.png"],
"default_icon": "icon.png"
},
"content_scripts": [
{
"js": ["jquery.min.js", "background.js"],
"matches": ["http://*/*", "https://*/*"]
}
]
}
The important part of the manifest.json is:
"content_scripts": [
{
"js": ["jquery.min.js", "background.js"],
"matches": ["http://*/*", "https://*/*"]
}
]
The js tells the extension to load jquery and then your custom js file (background.js
). The matches let you control what pages the extension can run on. In this case it is all http and https pages. You could restrict it to just one page.
icon.png
It's just a png image.
jquery.min.js
The minified version of the jQuery you need to use. Doesn't have to be minified, but we might as well.
background.html
Not always needed, but I have included it in this example. You can read more about it on google's extention page . Since I am not doing anything, mine is just an empty document.
<!doctype html>
<html>
<head>
<title>Background Page</title>
</head>
<body>
</body>
</html>
background.js
This is where we get to use our jQuery. There is no need for a document ready listener as the extension is actually loaded after the page so that page loading isn't affected.
$("body").append('Test');
Once any page loads it will append a pargraph with 'Test' as the text. Obvously we can do much more interesting and fun things, but that is for you and your jQuery magic to do.
Adding the extension to chrome
Now we have all the files and they all have the right content we need to add it to chrome.
- Bring up the extensions management page by clicking the spanner icon and choose
Tools > Extensions
- Tick the developer mode checkbox
- Click 'Load unpacked extension' and select your folder and click 'OK'. If all the files are valid and named correctly the icon should appear next to the address bar with the other extensions. Any changes you make will need to be reloaded by clicking reload in the extensions page for your plugin.
If you now refresh a webpage it should append a paragraph with 'Test'
Download the whole example extension and have a look to see how it works.
You can also clone the repo from github .
EDIT:
Creating a chrome extension - an update now follows up this article with a more up to date guide that addresses the issues that have been commented here.
Top comments (0)