Chrome Extension Example: Working Code and What You Actually Learn From It
Photo by Firmbee.com on Pexels

10 min read

From the studio

We build apps people actually keep.

Tinkrd is a small indie studio making focused, high-quality apps for everyday life. See what we're working on.

View our apps

The fastest way to understand how Chrome extensions work is to build one, even a tiny one. A working chrome extension example teaches you more in an hour than a week of reading documentation. Here is a real, minimal example with honest notes on what trips people up.

What a Chrome Extension Is Made Of

A Chrome extension is a small web app that runs inside the Chrome browser and is made of a few plain files: a manifest, some JavaScript, and optionally HTML and CSS. That is it. No special runtime, no framework required. Chrome reads the manifest to understand what your extension does and what permissions it needs, then loads the rest of your files accordingly.

The manifest file is always named manifest.json and always lives at the root of your extension folder. Every extension has exactly one. The current version of the manifest format is Manifest V3, which Google now requires for all new extensions submitted to the Chrome Web Store.

Here is the minimum viable manifest.json for an extension that does something real:

{
  "manifest_version": 3,
  "name": "Word Counter",
  "version": "1.0",
  "description": "Counts words on the current page.",
  "action": {
    "default_popup": "popup.html",
    "default_icon": "icon.png"
  },
  "permissions": ["activeTab", "scripting"]
}

Those six fields are enough to get a working extension into Chrome. The action key tells Chrome to show a popup when the user clicks the extension icon. The permissions array tells Chrome your extension needs to read the active tab and run scripts on it.

A Real Chrome Extension Example: Word Counter

A word counter extension is an ideal first chrome extension example because it touches every major piece: a popup UI, a content script, and message passing between them. It is genuinely useful, and the scope is small enough to finish in one sitting.

The popup HTML

The popup is just an HTML file. Chrome renders it in a small window when the user clicks your icon.

<!DOCTYPE html>
<html>
  <head>
    <meta charset='UTF-8' />
    <style>
      body { font-family: sans-serif; padding: 12px; min-width: 180px; }
      #count { font-size: 2rem; font-weight: bold; }
    </style>
  </head>
  <body>
    <p>Words on this page:</p>
    <p id='count'>...</p>
    <script src='popup.js'></script>
  </body>
</html>

The popup JavaScript

The popup script asks Chrome to run a function on the active tab, then displays the result.

// popup.js
document.addEventListener('DOMContentLoaded', async () => {
  const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

  const results = await chrome.scripting.executeScript({
    target: { tabId: tab.id },
    func: countWords,
  });

  document.getElementById('count').textContent = results[0].result;
});

function countWords() {
  const text = document.body.innerText || '';
  return text.trim().split(/\s+/).filter(Boolean).length;
}

Notice that countWords is defined in popup.js but executed in the context of the web page. That is message passing in its simplest form. The function runs on the page, returns a value, and your popup displays it. No server involved.

Loading it in Chrome

  1. Create a folder with those three files: manifest.json, popup.html, popup.js.
  2. Add any 128×128 PNG named icon.png.
  3. Open Chrome and go to chrome://extensions.
  4. Toggle on Developer Mode (top right corner).
  5. Click Load Unpacked and select your folder.

Your extension icon appears in the toolbar. Click it on any web page and it counts words. That is a working, real extension.

The Three Script Contexts You Need to Understand

Chrome extensions can run JavaScript in three different contexts, and mixing them up is the most common source of confusion for new extension builders. Understanding where each script lives saves hours of debugging.

Context File type Can access DOM? Can use Chrome APIs?
Popup popup.js Popup’s own DOM only Yes, most APIs
Content Script content.js Yes, the web page’s DOM Limited subset
Service Worker background.js No Yes, most APIs

A content script is injected directly into a web page. It can read and change the page’s DOM, which is exactly what you need if you want to highlight text, insert a button, or scrape data. But content scripts cannot use most Chrome APIs directly. They pass messages to the service worker, which handles those privileged operations.

The service worker (called the background script in older Manifest V2 extensions) runs in the background, separate from any page. In Manifest V3, it wakes up to handle events and then sleeps again. This means you cannot store state in a service worker variable and expect it to persist. Use chrome.storage.local instead.

If you want to go deeper on how these pieces fit into a full project, our guide to Chrome extension development covers the full picture, including how to think about scope before you write a line of code.

A Slightly More Complex Example: Highlight Keywords

Once you have the word counter working, a good next step is a content script that modifies the page. This example highlights every instance of a word the user types, directly on the page.

Add the content script to manifest.json

"content_scripts": [
  {
    "matches": ["<all_urls>"],
    "js": ["content.js"]
  }
]

The content script

// content.js
chrome.runtime.onMessage.addListener((message) => {
  if (message.action === 'highlight') {
    highlightWord(message.word);
  }
});

function highlightWord(word) {
  if (!word) return;
  const body = document.body.innerHTML;
  const regex = new RegExp(`(${word})`, 'gi');
  document.body.innerHTML = body.replace(
    regex,
    '<mark>$1</mark>'
  );
}

Sending the message from popup.js

// In popup.js, after getting the active tab:
document.getElementById('go').addEventListener('click', async () => {
  const word = document.getElementById('wordInput').value;
  await chrome.tabs.sendMessage(tab.id, { action: 'highlight', word });
});

This pattern, a popup that sends a message, and a content script that receives and acts on it, is the backbone of most real extensions. Get comfortable with it and you can build almost anything.

A note on innerHTML replacement: it works for a demo but it is fragile on complex pages and can break event listeners. A production version would use the TreeWalker API to walk text nodes instead. Worth knowing before you ship.

What to Build First and Why Small Wins Matter

The best first extension is one that solves a problem you actually have. The word counter and keyword highlighter above are fine learning exercises, but the extensions people actually use and sometimes sell are the ones that scratch a real itch.

At Tinkrd, we build small apps for families, and the mindset transfers directly: start with the smallest thing that is genuinely useful, ship it, then see what people actually want. An extension that does one thing well is easier to maintain, easier to explain in the Chrome Web Store listing, and easier to get someone to install.

Some practical ideas that are small enough to finish in a weekend:

Each of these uses content scripts, storage, or the popup pattern you already know. None requires a backend. None requires a framework. You can build and publish a Chrome extension for any of these in a few days with the skills above.

The Chrome Web Store charges a one-time $5 developer registration fee. After that, publishing is free. The review process takes anywhere from a few days to a couple of weeks depending on the permissions your extension requests. Requesting fewer permissions speeds things up and builds user trust.

One thing worth knowing before you get too far: the official Chrome Extensions documentation is genuinely good and kept up to date. The API reference there is the source of truth when something behaves unexpectedly.

Common Mistakes in First Chrome Extension Examples

Most early errors in chrome extension examples come from a handful of predictable misunderstandings. Knowing them in advance saves a lot of time staring at a broken console.

Forgetting that service workers sleep

In Manifest V3, the background service worker does not run continuously. If you store a value in a JavaScript variable in your service worker and then the worker sleeps, that value is gone. Use chrome.storage.local.set() and chrome.storage.local.get() to persist anything you need across events.

Using Manifest V2 examples from old tutorials

A lot of tutorials still online show Manifest V2 syntax, including "manifest_version": 2, browser_action instead of action, and background.scripts arrays instead of a service worker. Chrome still runs V2 extensions locally but will not accept new V2 submissions to the Web Store. Start with V3 from the beginning.

Requesting too many permissions

New developers often add permissions like tabs, history, or webRequest because a tutorial mentioned them, even when the extension does not need them. Each extra permission triggers a warning to the user during install, and broader permissions mean slower Web Store review. Request only what you actually use.

Trying to use ES modules in content scripts

Content scripts do not support ES module syntax (import / export) out of the box. If you want to share utility functions, either bundle your code with a tool like esbuild, or use the world: 'MAIN' injection option with care. For a first extension, just keep the code in one file and skip the bundler complexity until you need it.

Frequently asked questions

What is the simplest chrome extension example I can build in one hour?

A popup that reads and displays the current page’s title or word count is achievable in under an hour. You need three files: manifest.json, popup.html, and popup.js. Load it unpacked from chrome://extensions with Developer Mode on and you have a working extension with no build step required.

Do I need to know React or any framework to build a Chrome extension?

No. Plain HTML, CSS, and JavaScript are enough for most extensions. Frameworks like React are useful if your popup UI becomes complex, but they add a build step and bundle size. Start without one and add it only when the complexity earns it.

Can I use my chrome extension example locally without publishing it?

Yes. Any extension loaded via Load Unpacked on the chrome://extensions page runs fully on your local machine. You can develop and test indefinitely without publishing. The only limitation is that locally loaded extensions do not auto-update and are tied to one Chrome profile.

How do I pass data between my popup and the web page?

The two main options are chrome.scripting.executeScript, which runs a function directly on the page and returns a value, and message passing using chrome.tabs.sendMessage and chrome.runtime.onMessage. For simple one-time reads, executeScript is easier. For ongoing communication, message passing is more flexible.

Is it hard to get a chrome extension approved by the Web Store?

It is straightforward if you keep permissions minimal, write an accurate description, and do not violate Google’s policies around data collection. Extensions that request sensitive permissions like webRequest or access to all URLs face more scrutiny and longer review times. A simple extension with narrow permissions typically gets reviewed within a few days.

Working through a real chrome extension example, even a tiny word counter, teaches you the manifest structure, script contexts, and message passing patterns that power every extension on the store. Start small, load it unpacked, break it, fix it. That loop is where the actual learning happens.

We’re building in the open

Tinkrd is a small studio making thoughtful apps for families. Our first app is launching soon.

See what we’re building

T
The Tinkrd Team

We are a small independent app studio in California. We write about what we are learning as we build and ship products, from first commit to first customer.