Nov 16, 2021 . 5 min read
Recently, I got in touch with the chrome extension and decided to learn more about it. After reading some docs, I was amazed by how powerful chrome’s API provides as well as how simple it can be in building an extension using it.
In this article, I will be showing you how to create a chrome extension that can change your Google homepage background dynamically and will remain persistent.
Note that this article assumes some knowledge in HTML, JavaScript, and CSS as well as Linux-based operating system. For window users, please find similar commands that correspond to their Linux counterparts.
What are Chrome extensions? Chrome extensions are software programs, built on web technologies (such as HTML, CSS, and JavaScript) that enable users to customize the Chrome browsing experience.
What can extensions do? Extensions are powerful tools that add features and functions to a browser.
However, extensions can only use either page actions or browser actions but not both.
Page actions are for actions that are only relevant to particular pages. It is seen in the chrome’s address bar.
Browser actions are by default enabled on all tabs/URLs. Similarly, it is seen in the chrome’s address bar.
The extension we will be building in this article is a simple example that I have built. It allows users to dynamically change their chrome’s background image and remain persistent. Here is a gif of the extension:
Create a new directory
mkdir chromebg
Create a new manifest file
Access the folder and create a manifest.json file
cd chromebg
touch manifest.json
The manifest.json file contains information about the extension’s name, description, permissions, version number, etc. Note that every extension requires this manifest.json file
In this project, we will require these 5 permissions.
Connecting to chrome
Every time you make changes to the code, you should press the reload button that is in the card of your extension in chrome://extensions
Create Popup.html
This is essentially the UI of our popup. We will focus on 3 things, the input, the clear button as well as the submit button
touch.html
popup.css is not shown here. You are free to design your own popup! (If you are interested in the popup.css, visit here)
Create Popup.js
touch popup.js
popup.js contains the logic for the popup to communicate with the content script as well as chrome’s storage (see below for implementation of content script).
Create Content Script
touch content.js
content.js contains the logic of communicating with the webpage as well as chrome’s storage. Also, it listens for action from popup.js in setting a new background, clearing of the background image, and changes of URL via background script. (see below for implementation of background script)
chrome.runtime.onMessage.addListener: Fires when a message is sent from either an extension process (by runtime.sendMessage) or a content script (by tabs.sendMessage).
Note that there is some repetition of code for ease of demonstration
Create Background Script
touch background.js
background.js contains the logic of sending messages to the content script whenever the URL changes.
For more information regarding the declarative content, please visit https://stackoverflow.com/questions/22511854/chrome-extension-that-acts-only-when-clicked-on-certain-webpages
Now go to chrome://extensions in your browser, click reload.
Go to https://www.google.com and click on your extension icon and insert an image URL for your background.
Voila!!!
That’s it! Our extension is now complete.
Now that we are done with our extension and is ready to be published, you will first need to create a chrome developer’s account. Please visit chrome’s developer guide for more information.
Check the repo for this project:
https://github.com/seanjyjy/chrome-bg
Visit here to download the extension.
All in all, in this article, we’ve learnt the basics of Chrome extensions by creating an extension using only HTML, JavaScript, and CSS. Hope that you find this article useful and let me know what you think in the comments!
View the Medium article here