Browser
Browser
This tutorial
explains how you
can build a website
blocker extension
that will run on
multiple browsers.
W
eb browsers have become integral to our Tokens to websites and content creators. You can download
lives, and are used on a wide range of devices, Brave from https://brave.com/download/.
including desktops, laptops, tablets and Microsoft Edge: This is a Web browser developed by
smartphones. In 2019, an estimated 4.3 billion Microsoft. It was rebuilt as a Chromium based browser in
people used Web browsers. The most used browser today is 2019. It is also available for Windows, Android, iOS and
Google Chrome, with a 64 per cent global market share across MacOS. You can download Microsoft Edge from https://
all devices, followed by Apple Safari with 17 per cent. www.microsoft.com/en-us/edge.
the website they want to block to the list. When they try block.js: In this file we are going to intercept the outgoing
to visit that site, a message that states that the website is request from the browser. If the request contains one
blocked will be displayed, and access to it will be denied. If of the blocked websites from the list, we will reject the
the users then want to visit the site, they will have to unblock request, thus blocking that website.
it by deleting the website entry from the list of blocked style.css adds styles to the options.html page to
sites in the extension. make it look good.
We are going the use the following Web technologies to
build this extension: Creating the manifest file
HTML to create a table to view blocked sites, and buttons Open the manifest.json file and copy the following
to add and delete them JSON code:
CSS to style HTML
JavaScript (Chrome browser API) to write our logic {
“name”: “Website Blocker”,
Setting up the development environment “version”: “1.0.0”,
Let us now set up the file structure for our browser extension. “description”: “Blocks websites based on domain names”,
Create a new folder and give it a name, say ‘web-blocker’. “background”: { “scripts”: [“block.js”] },
Now, inside that folder, create the following empty files: “options_page”: “options.html”,
manifest.json “permissions”: [
options.html “webRequest”, “webRequestBlocking”,
script.js “http://*/*”, “https://*/*”,
block.js “storage”
style.css ],
You should end up with the directory structure shown in “manifest_version”: 2
Figure 1. }
manifest.json is a metadata file in JSON format. It
contains basic information about the extension, like its As you can see, this code is in JSON format. It consists
name, a description of it, the version number, which script of general information about the browser extension, such as
it should run on starting up, etc. its version, name, description, permissions required for the
options.html displays the UI of the extension, like the extension, and so on.
list of websites to be blocked, buttons to add and delete, The parameter description is as follows:
websites from the list, etc. manifest_version specifies the version of the manifest; the
script.js controls the interactions and the logic of the value must be 2
options.html file. name specifies the name of the extension
version specifies the version of the extension
description specifies a short description of the extension
background specifies that the script block.js should load as
soon as the user starts the browser
options_page specifies the UI page to be displayed for
extension options
permission specifies which browser APIs are to be
used. We are using webRequest, webRequestBlocking,
https://*/*, http://*/* and storage
} else {
urls = [];
}
});
addButton.addEventListener(‘click’, function () {
const url = document.getElementsByClassName(‘add-
website’)[0].value;
if (url.length > 0) {
noWebsiteMsg.style.display = “none”;
Figure 2: Website blocker user interface urls.push(url + “/*”);
chrome.storage.local.set({
This CSS will style the UI as shown in Figure 2. “websites”: JSON.stringify(urls)
To add JavaScript, open the script.js file and copy the }, function (value) {
following JavaScript code: console.log(value);
});
let urls = []; // Global variable to store URLs save();
loadList();
// Load init when the content is loaded. document.getElementsByClassName(‘add-website’)
window.addEventListener(‘DOMContentLoaded’, init); [0].value = “”;
window.addEventListener(‘DOMContentLoaded’, loadList); }
});
//Init Function - Initialize, load, addEventListeners }
function init() {
const addButton = document.getElementById(‘add’); function loadList() {
const deleteButton = document.getElementsByClassName(‘de const deleteButton = document.getElementsByClassName(‘de
lete’); lete’);
const noWebsiteMsg = document.getElementById(‘no-website- const tblNode = document.getElementsByTagName(‘body’)[0];
msg’); let tblRow = document.createElement(‘tr’);
chrome.storage.local.getBytesInUse([‘websites’], function let tblData = document.createElement(‘td’);
(bytes) { let tblButton = document.createElement(‘button’);
if (bytes) { let storedURLs = [];
chrome.storage.local.get(“websites”, function chrome.storage.local.getBytesInUse([‘websites’], function
(res) { (bytes) {
if (res != {}) { if (bytes) {
urls = JSON.parse(res.websites); chrome.storage.local.get(“websites”, function
if (urls == undefined || urls.length == (res) {
0) { if (res != {}) {
noWebsiteMsg.style.display = “”; storedURLs = JSON.parse(res.websites);
} else {
noWebsiteMsg.style.display = “none”; if (storedURLs != undefined || storedURLs
} != []) {
if (deleteButton) { for (let i = 0; i < storedURLs.
for (let i = 0; i < deleteButton. length; i++) {
length; i++) { tblButton.innerText = “-”;
deleteButton[i]. tblButton.setAttribute(‘class’,
addEventListener(‘click’, function (e) { ‘delete’);
deleteWebsite(e); tblRow.setAttribute(‘id’, “row” + i);
}); tblButton.setAttribute(‘id’, i);
} tblData.innerText =
} storedURLs[i];
} tblRow.appendChild(tblData);
}); tblRow.appendChild(tblButton);
webRequest.onBeforeRequest API and check if it matches the corner, if it hasn’t been checked.
list of websites that we have stored. If it does, we block the 3. Now, click on Load unpacked, which will open up the file-
request; else, we allow it. selection dialogue box, as shown in Figure 4.
4. Navigate to the directory where your extension files
Loading the extension are and select it. Another easy way is to simply drag
First, let us load the extension in the Chrome browser. and drop the extension folder onto chrome://extensions
1. Open up the Chrome menu by clicking the icon that’s to in your browser to load it. The extension will install/
the right of the address bar and select Extensions under load right away. If you make some errors while creating
the Tools menu or just enter chrome://extensions in the the extension, it will not load and will display an error
address bar before hitting Enter, as shown in Figure 3. message at the top of the page. You have to rectify the
2. Check the Developer mode checkbox in the top right-hand error and then try again.
the context menu. Then select Options, which will open the
options page. Here you can add the website you want to
block. As an example, I added https://www.facebook.com
to the list, as shown in Figure 6; so as soon as I try to visit
https://www.facebook.com, the request gets blocked and a
message is displayed, as shown in Figure 7. Now, the website
is blocked, unless I delete it from the list.
References
[1] https://www.google.com/chrome/
[2] https://developer.chrome.com/extensions/api_index
Figure 7: The extension blocking the request to a website