User Agents

Web browsers and everyday web tech, explained

How to Create Chrome Extensions from Scratch

· 5 min read · Extensions

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Creating a Chrome extension from scratch allows you to add custom functionality to the Google Chrome browser, such as modifying web pages, adding toolbar buttons, or automating repetitive tasks. With just basic knowledge of HTML, CSS, and JavaScript, you can build and test your own extension locally, then publish it to the Chrome Web Store. This guide walks you through setting up your first extension, structuring the required files, testing it, and avoiding common issues.

What You'll Need

  • A computer with Google Chrome installed
  • A text editor (e.g., VS Code, Sublime Text, or Notepad++)
  • Basic knowledge of HTML, CSS, and JavaScript
  • An understanding of JSON syntax for the manifest file

Step-by-Step

  1. Create a new folder on your computer for your extension. Name it something descriptive, like my-first-extension. This folder will hold all your extension files.
  2. Inside the folder, create a file named manifest.json. This file tells Chrome about your extension, including its name, version, and permissions. Start with this basic structure:
    {
      "manifest_version": 3,
      "name": "My First Extension",
      "version": "1.0",
      "description": "A simple extension to get started.",
      "action": {
        "default_popup": "popup.html",
        "default_icon": "icon.png"
      }
    }
    Note: Chrome extensions now use Manifest V3, so ensure the version is set to 3.
  3. Create a small icon image (16x16 or 32x32 pixels) and save it as icon.png in the same folder. This icon will appear in the Chrome toolbar.
  4. Create a file named popup.html in the same directory. This file defines the content that appears when a user clicks the extension icon. Add the following minimal HTML:
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <style>
        body { width: 200px; padding: 10px; font-family: sans-serif; }
        h1 { font-size: 16px; margin: 0; }
      </style>
    </head>
    <body>
      <h1>Hello, Chrome!</h1>
    </body>
    </html>
    This creates a simple popup with a greeting.
  5. To add interactivity, create a JavaScript file named popup.js and link it in popup.html just before the closing </body> tag:
    <script src="popup.js"></script>
    Then, in popup.js, add:
    document.addEventListener('DOMContentLoaded', function() {
      console.log('Popup loaded');
    });
    This logs a message when the popup opens.
  6. If your extension needs to run scripts on web pages (content scripts), add a content_scripts section to your manifest.json:
    "content_scripts": [
      {
        "matches": ["<all_urls>"],
        "js": ["content.js"]
      }
    ]
    Then create content.js in the same folder. For example:
    console.log('Content script running on:', window.location.href);
    This script will run on every page you visit.
  7. Open Chrome and go to chrome://extensions/. Enable Developer mode using the toggle in the top-right corner.
  8. Click "Load unpacked" and select your extension folder. Chrome will load your extension, and its icon will appear in the toolbar.
  9. Click the extension icon to see your popup. Open Chrome DevTools (right-click the popup and select "Inspect") to view console messages from popup.js or content.js.
  10. To test content scripts, visit any webpage and open DevTools (F12). Check the Console tab to see the log message from content.js.
  11. Once everything works, you can package your extension by clicking "Pack extension" on the chrome://extensions/ page. This creates a .crx file for distribution or submission to the Chrome Web Store.

Common Mistakes

  • Using Manifest V2 instead of V3: Chrome no longer supports new extensions using Manifest V2. Always set "manifest_version": 3.
  • Forgetting to enable Developer mode on the extensions page: Without it, the "Load unpacked" option won’t appear.
  • Incorrect file paths in the manifest: Ensure all referenced files (like popup.html or content.js) are spelled correctly and located in the extension folder.
  • Missing required fields: Every manifest must include manifest_version, name, and version.
  • Not reloading the extension after changes: After editing files, click "Reload" on the extension card in chrome://extensions/ to apply updates.

Tips

  • Use small icons: 16x16 or 32x32 pixels in PNG format work best for toolbar icons.
  • Start simple: Build a basic popup first, then add features like content scripts or background services.
  • Leverage Chrome’s API: Use chrome.storage for saving user data, or chrome.tabs to interact with browser tabs.
  • Validate your manifest: Use online JSON validators to catch syntax errors before loading.
  • Test across sites: If your content script uses <all_urls>, ensure it doesn’t break on secure or restricted pages.

Troubleshooting

If your extension doesn’t load, check the error message on the chrome://extensions/ page. Common issues include invalid JSON in the manifest (missing commas, quotes, or braces) or missing files. If the popup doesn’t open, verify the default_popup path in manifest.json. If content scripts don’t run, ensure the matches pattern is correct and the script file exists. If changes aren’t reflected, always click "Reload" after editing files. For permission-related errors, explicitly declare required permissions in the manifest, such as "permissions": ["activeTab"].

Frequently Asked Questions

Can I use jQuery in a Chrome extension?

Yes. Include jQuery by downloading it and adding it to your extension folder, then reference it in your HTML or manifest before your scripts.

How do I debug a Chrome extension?

Use Chrome DevTools: right-click the popup to inspect it, or open DevTools on a webpage to see content script logs. Background scripts can be inspected via the extension page.

Do Chrome extensions work in other browsers?

Some do. Edge, Opera, and Brave support Chrome extensions, but always test in the target browser.

Can I monetize my Chrome extension?

Yes, but not directly through the extension code. You can offer paid versions via external websites or use donation links.

How do I update a published extension?

Update the version number in manifest.json, make your changes, repack, and upload the new version to the Chrome Web Store.

Are there size limits for Chrome extensions?

Yes. The Chrome Web Store has a 100 MB limit for unpacked extensions, but most should be much smaller.