Administrivia
Today: What is a Chrome Extension, and how do you write one?
Note: We will not be testing on Chrome Extensions, but they are very useful to learn as web programmers!
A program to extend the Chrome browser, often to personalize the user's browsing experience
There are many different types of Chrome Extensions you can write (new tab overrides, bookmark customizations, CSS overriding on certain websites, content control, etc.)
Today we'll cover the most important components using a New Tab Override and Popup extension as examples!
manifest.json
(required)
HTML/CSS/JS files for the interface/behavior of the extension
manifest.json
File (Required)mainifest.json
Example{
"manifest_version" : 2,
"name" : "My Extension!",
"version" : "0.1",
"description" : "My extension description",
"icons" : {
"16" : "icon16.png",
"48" : "icon48.png",
"128" : "icon128.png"
}
}
This type of Chrome Extension is one of the easiest to get started with. It doesn't require content or background scripts, and you can use pretty much anything you've learned in this class to add as the New Tab page!
Great personalized holiday gifts :)
{
...
"chrome_url_overrides" : {
"newtab" : "your-html-page.html"
},
...
}
Note: As you're trying different types of extensions, remember that this tab does not usually need other JS files other than the standard JS you add to the HTML
manifest.json
: Doggy Tabs!{
"name": "Doggy Tabs!",
"version": "0.1",
"manifest_version": 2,
"description" : "Now you have an excuse to open more tabs!",
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
},
"chrome_url_overrides" : {
"newtab": "doggy-tab.html"
}
}
You can find the code here (please do not distribute as your own work and cite the original author if you extend it)
Once you have your project folder with your manifest created, go to chrome://extensions in your Chrome browser (yes, you need Chrome).
Make sure you have Developer Mode on, and click Load Unpacked
More information here.
You will be prompted to select the folder containing your extension, and if you have everything set up correctly, you should see your new tab program!
Make sure you have Developer Mode on
Go to Developer Dashboard
Click "Add New Item" to add your project folder (need to submit as .zip)
From here, there is a page with some options you'll need to provide. Then click the publish button and voila!
You can find a full guide on getting your Developer Account and first Chrome Extension published here.
{
...
"browser_action" : {
"defualt_title" : "Popup Extension Title",
"default_popup" : popup.html"
},
...
}
This is sufficient to tell the browser to listen to the "browser action" event that fires when a user clicks the extension icon in the browser toolbar, which opens the specified HTML page
As with New Tab Overrides, you don't need background.js or content.js if you just want your popup page to appear when clicked and be self-contained (you'll need a content script to also interact with the current tab DOM)
{
"manifest_version" : 2,
"name" : "Hello World Popup Example",
"version" : "0.1",
"browser_action": {
"default_title": "Hello World!",
"default_popup": "popup.html"
}
}
Starter code (will show a basic popup if you add as local Extension)
One of the most important things to understand when writing Chrome Extensions is the difference between Background Scripts and Content Scripts
Defined in manifest.json with:
{
...
"content_scripts" : [{
"matches" : [""],
"js" : ["content.js"],
"css" : ["custom-styles.css"]
}],
"permissions" : ["activeTab"],
...
}
Under Chrome's Developer guidelines, only give permissions where needed
More information here
Defined in manifest.json with:
{
...
"background" : {
"scripts" : ["background.js"],
"persistant" : "false"
},
...
}
More information here
Note that these types of extensions often make use of one of the many Chrome extension APIs. Whenever you use one (e.g. the "activeTab") you'll need to add a "permissions" property to the manifest.json
Refer to the Chrome Developer Overview for more information and best practices!