{"id":308141,"date":"2020-05-19T07:38:42","date_gmt":"2020-05-19T14:38:42","guid":{"rendered":"https:\/\/css-tricks.com\/?p=308141"},"modified":"2020-05-19T20:31:47","modified_gmt":"2020-05-20T03:31:47","slug":"how-to-build-a-chrome-extension","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/how-to-build-a-chrome-extension\/","title":{"rendered":"How to Build a Chrome Extension"},"content":{"rendered":"\n
I made a Chrome extension this weekend because I found I was doing the same task over and over and wanted to automate it. Plus, I\u2019m a nerd living through a pandemic, so I spend my pent-up energy building things. I’ve made a few Chrome Extensions over the years, hope this post helps you get going, too. Let\u2019s get started!<\/p>\n\n\n\n\n\n\n
The first step is creating a You might notice a few things- first: the names<\/strong> and descriptions<\/strong> can be anything you’d like.<\/p>\n\n\n\n The permissions<\/strong> depend on what the extension needs to do. We have A list of all of the permissions and what they mean can be found in Chrome’s extension docs<\/a>.<\/p>\n\n\n\n The There are things in Once the manifest, CSS and JavaScript files are ready, head over to manifest.json<\/code> file in a project folder. This serves a similar purpose to a
package.json<\/code>, it provides the Chrome Web Store with critical information about the project, including the name, version, the required permissions, and so forth. Here\u2019s an example:<\/p>\n\n\n\n
{\n \"manifest_version\": 2,\n \"name\": \"Sample Name\",\n \"version\": \"1.0.0\",\n \"description\": \"This is a sample description\",\n \"short_name\": \"Short Sample Name\",\n \"permissions\": [\"activeTab\", \"declarativeContent\", \"storage\", \"<all_urls>\"],\n \"content_scripts\": [\n {\n \"matches\": [\"<all_urls>\"],\n \"css\": [\"background.css\"],\n \"js\": [\"background.js\"]\n }\n ],\n \"browser_action\": {\n \"default_title\": \"Does a thing when you do a thing\",\n \"default_popup\": \"popup.html\",\n \"default_icon\": {\n \"16\": \"icons\/icon16.png\",\n \"32\": \"icons\/icon32.png\"\n }\n }\n}<\/code><\/pre>\n\n\n\n
[\"activeTab\", \"declarativeContent\", \"storage\", \"<all_urls>\"]<\/code> in this example because this particular extension needs information about the active tab, needs to change the page content, needs to access
localStorage<\/code>, and needs to be active on all sites. If it only needs it to be active on one site at a time, we can remove the last index of that array. <\/p>\n\n\n\n
\"content_scripts\": [\n {\n \"matches\": [\"<all_urls>\"],\n \"css\": [\"background.css\"],\n \"js\": [\"background.js\"]\n }\n],<\/code><\/pre>\n\n\n\n
content_scripts<\/code> section sets the sites where the extension should be active. If you want a single site, like Twitter for example, you would say
[\"https:\/\/twitter.com\/*\"]<\/code>. The CSS and JavaScript files are everything needed for extensions. For instance, my productive Twitter extension<\/a> uses these files to override Twitter\u2019s default appearance.<\/p>\n\n\n\n
\"browser_action\": {\n \"default_title\": \"Does a thing when you do a thing\",\n \"default_popup\": \"popup.html\",\n \"default_icon\": {\n \"16\": \"icons\/icon16.png\",\n \"32\": \"icons\/icon32.png\"\n }\n}<\/code><\/pre>\n\n\n\n
browser_action<\/code> that are also optional. For example, if the extension doesn\u2019t need a popup for its functionality, then both the
default_title<\/code> and
default_popup<\/code> can be removed. In that case, all that’s needed the icon for the extension. If the extension only works on some sites, then Chrome will grey out the icon when it\u2019s inactive.<\/p>\n\n\n
Debugging<\/h3>\n\n\n
chrome:\/\/extensions\/<\/code>from the browser’s address bar and enable developer mode. That activates the “Load unpacked” button to add the extension files. It’s also possible to toggle whether or not the developer version of the extension is active.<\/p>\n\n\n\n