Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Practice: Extension Challenges and Code Review

Test your Chrome extension skills with hands-on challenges on messaging, storage, permissions and security, review a flawed extension for bugs, and plan three project ideas to build next.

You have learned how parts of an extension talk, store data, stay secure, get tested and get published. The best way to make that stick is practice. This lesson gives you challenges with runnable stand-ins, a code review exercise and project ideas. Try each one before reading the hint.

Challenge 1: Fix the lost reply

This worker should answer { type: "count" } with the number of saved items, but the sender receives undefined. Fix it so the reply arrives. The stand-in below follows the real rule: the channel closes unless the listener returns true.

Challenge 1: make the reply arrive

Hint: the answer comes from One-time messages. After the fix the output should be reply: {"count":3}.

Challenge 2: Merge settings safely

Write mergeSettings(saved, defaults) that returns defaults overridden by saved values, but ignores unknown keys and values of the wrong type. This is what a settings loader should do when old or hostile data sits in storage.

Challenge 2: a defensive settings loader

Extend it: reject a fontSize outside 8 to 48, and allow theme only to be "light" or "dark". This combines Storage patterns with Secure messaging.

Challenge 3: Review a flawed extension

Read this extension and list every problem before you check the answers.

JSON
{
  "manifest_version": 3,
  "name": "SuperTool - Best Free Tab Manager Cheap Coupons Dark Mode",
  "version": "1.0-beta",
  "permissions": ["tabs", "history", "cookies", "storage", "downloads"],
  "host_permissions": ["<all_urls>"],
  "content_security_policy": {
    "extension_pages": "script-src 'self' 'unsafe-eval' https://cdn.example.com"
  }
}
JavaScript
// content.js
chrome.runtime.onMessage.addListener((msg) => {
  document.body.innerHTML += msg.html;
});
window.addEventListener("message", (e) => {
  chrome.runtime.sendMessage({ type: "open", url: e.data.url });
});
JavaScript
// service-worker.js
let cache = {};
chrome.runtime.onMessage.addListener((msg) => {
  if (msg.type === "open") chrome.tabs.create({ url: msg.url });
  if (msg.type === "save") cache[msg.key] = msg.value;
});

Problems in the manifest

Keyword-stuffed name Several unrelated purposes
Invalid version 1.0-beta has a letter
Too many permissions history, cookies, downloads, all_urls
Illegal CSP unsafe-eval and a CDN script
1 / 2

How many did you find? The fixes are covered in Permissions model, CSP, Secure messaging and Updates and versioning.

Challenge 4: Pick the storage area

For each case choose local, sync, session or in-memory only, and explain why: (a) the user's theme preference, (b) a cached list of 5,000 downloaded rows, (c) an access token that must vanish when the browser closes, (d) the number of clicks in the current popup session.

Suggested answers: (a) sync: small and useful on every device; (b) local: large; (c) session: kept in memory, never written to disk (and ask whether you should hold the token at all); (d) in-memory in the popup: it disappears with it, and no storage is needed.

Challenge 5: Write the tests

Take the increment function from Unit testing with mocks and write five tests: a new host, a repeat visit, a URL with a port, an invalid URL (decide whether it should throw or return the input unchanged) and an about:blank page. Then add one end-to-end test idea from the E2E lesson in plain words.

Project ideas to build next

  1. Reading time badge: a content script that counts words on the page and the worker shows "4m" in the toolbar badge (chrome.action.setBadgeText).
  2. Quick bookmarks panel: uses the side panel API and storage.sync for a link list per user, with import and export.
  3. Focus mode: blocks a list of distracting sites with declarativeNetRequest dynamic rules and a timer in chrome.alarms.

For each, write first: the single purpose sentence, the minimum permissions, which context owns which data, and how you would test it. That plan is half of the work and it matches what reviewers will ask.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is the best first step when designing a new extension?

    A narrow purpose guides permissions, review, privacy and testing.
  2. 2. A cache of 5,000 rows is best stored in:

    Sync has tiny quotas; local holds large data on one device.
  3. 3. In the flawed extension, which is the most dangerous content script bug?

    Both let page-controlled data steer the extension and inject markup.
  4. 4. Why do tests pass now into time-based functions?

    Injected time lets tests simulate any moment instantly.

Summary

  • Practice the core skills: async replies, defensive settings, storage choice, code review and testing.
  • Reviewing flawed code trains you to spot permission, CSP, XSS and trust problems early.
  • Start every project from the single purpose, then the minimum permissions.
  • You now know the full path from an idea to a published, maintained extension.

Next: Back to the Chrome Extensions track