Lesson 01-Browser Extension Basics

Browser Extension Concepts and Use Cases

Browser extensions are small software programs designed to enhance browser functionality, providing additional tools and customization options. They can modify webpage content, add new features, offer privacy protection, block ads, manage passwords, and more, significantly improving the user’s internet browsing experience.

Introduction to Major Browser Extension Platforms

Google Chrome

  • Chrome extensions are developed using HTML, CSS, and JavaScript, with a Manifest file defining metadata and permissions.
  • Supports various extension types, including page action extensions, content scripts, background scripts, popups, and more.
  • The Chrome Web Store provides a platform for users to discover, install, and manage extensions.

Mozilla Firefox

  • Firefox extensions, also known as add-ons, use the WebExtensions API, which is highly compatible with Chrome extensions.
  • The Firefox Add-ons store allows users to find and install extensions.
  • Supports custom panels, content scripts, browser actions, and more.

Apple Safari

  • Safari extensions are developed using the Safari Extension Builder tool, based on HTML, CSS, and JavaScript.
  • The Safari Extension Gallery provides a platform for users to download and install extensions.
  • Supports browser actions, context menus, content scripts, and other features.

Microsoft Edge

  • Edge extensions are based on the same technical standards as Chrome, using a Manifest file to define extensions.
  • The Microsoft Store provides a platform for downloading and installing Edge extensions.
  • Supports page actions, content scripts, background scripts, and more.

Basic Architecture of Extensions

The architecture design of browser extensions is the foundation of their functionality. We will delve into the three core components of browser extensions: Background Scripts, Content Scripts, and Browser Actions, and analyze their working principles and development process through example code.

Background Scripts

Background scripts are continuously running parts of a browser extension, capable of performing tasks in the background even when the user is not interacting with the extension. This makes them ideal for handling scheduled tasks, listening for specific events, or communicating with other services.

1. Defining Background Scripts

In the manifest.json file, the entry file for background scripts is specified using the “background” field.

{
  "manifest_version": 3,
  "name": "My Extension",
  "version": "1.0",
  "background": {
    "service_worker": "background.js"
  }
}
JavaScript

2. Writing Background Scripts

// background.js
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === 'changeColor') {
    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
      chrome.tabs.sendMessage(tabs[0].id, { action: 'changeColor' });
    });
  }
});
JavaScript

Content Scripts

Content scripts run in the context of specific webpages, allowing access to and modification of the page’s DOM to enhance or alter its content.

1. Configuring Content Scripts

In the manifest.json file, content scripts and their target pages are defined using the “content_scripts” field.

{
  "manifest_version": 3,
  "name": "My Extension",
  "version": "1.0",
  "content_scripts": [
    {
      "matches": ["*://*.example.com/*"],
      "js": ["content.js"]
    }
  ]
}
JavaScript

2. Writing Content Scripts

// content.js
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === 'changeColor') {
    document.body.style.backgroundColor = '#ff0';
  }
});
JavaScript

Browser Actions

Browser actions allow users to trigger extension behaviors by clicking the extension icon in the browser toolbar, such as displaying a popup or executing specific functions.

1. Defining Browser Actions

In the manifest.json file, browser actions are defined using the "browser_action" or "action" field.

{
  "manifest_version": 3,
  "name": "My Extension",
  "version": "1.0",
  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "images/icon16.png",
      "48": "images/icon48.png",
      "128": "images/icon128.png"
    }
  }
}
JavaScript

2. Creating a Popup

<!-- popup.html -->
<!DOCTYPE html>
<html>
<head>
  <title>My Extension Popup</title>
</head>
<body>
  <button id="changeColorButton">Change Background Color</button>
  <script src="popup.js"></script>
</body>
</html>
HTML

3. Writing Popup Scripts

// popup.js
document.getElementById('changeColorButton').addEventListener('click', () => {
  chrome.runtime.sendMessage({ action: 'changeColor' });
});
JavaScript

Comprehensive Example

Suppose we are developing an extension that changes the background color of the current webpage when the user clicks the toolbar icon.

manifest.json

{
  "manifest_version": 3,
  "name": "Color Changer",
  "version": "1.0",
  "description": "Changes the background color of webpages you visit.",
  "permissions": ["activeTab"],
  "background": {
    "service_worker": "background.js"
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content.js"]
    }
  ],
  "action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "images/icon16.png",
      "48": "images/icon48.png",
      "128": "images/icon128.png"
    }
  }
}
JavaScript

background.js

chrome.action.onClicked.addListener((tab) => {
  chrome.tabs.sendMessage(tab.id, { action: 'changeColor' });
});
JavaScript

content.js

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action === 'changeColor') {
    document.body.style.backgroundColor = '#ff0';
  }
});
JavaScript

popup.html

<!DOCTYPE html>
<html>
<head>
  <title>Color Changer</title>
</head>
<body>
  <button id="changeColorButton">Change Background Color</button>
  <script src="popup.js"></script>
</body>
</html>
HTML

popup.js

document.getElementById('changeColorButton').addEventListener('click', () => {
  chrome.runtime.sendMessage({ action: 'changeColor' });
});
JavaScript
Share your love