0Pricing
Browser Extensions Development (Chrome & Edge) · Lekcja

Background service workery

Dowiedz się, jak background service workery zarządzają zdarzeniami, trwałą logiką i cyklem życia rozszerzenia.

Background service workery to bezpłatna lekcja Browser Extensions Development (Chrome & Edge) na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Browser Extensions Development (Chrome & Edge), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Browser Extensions Development (Chrome & Edge) zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Extension's Brain: Service Workers

Welcome to the core of your Manifest V3 extension: Background Service Workers! Think of them as the silent brain of your extension.

They're responsible for listening to browser events, managing persistent logic, and ensuring your extension runs efficiently without constantly consuming resources.

MV3: Why Service Workers?

In Manifest V2, extensions often used 'persistent background pages' that ran constantly. Manifest V3 introduced service workers as a more modern, energy-efficient alternative.

  • Event-Driven: They only wake up when an event occurs.
  • Temporary: They go idle and terminate when not needed.
  • Energy-Saving: This 'sleep-wake' cycle saves battery and memory.

Declaring Your Service Worker

To use a service worker, you must declare it in your manifest.json file. This tells the browser which JavaScript file acts as your background script.

It's specified under the 'background' key, pointing to your service worker script.

{
  "manifest_version": 3,
  "name": "My Service Worker Extension",
  "version": "1.0",
  "background": {
    "service_worker": "service-worker.js"
  },
  "action": {
    "default_popup": "popup.html"
  }
}

Service Worker Lifecycle

Unlike a traditional always-on program, a service worker has a dynamic lifecycle:

  • It activates when an event it's listening for occurs.
  • It runs its associated tasks.
  • After a period of inactivity (usually a few seconds), it goes idle and eventually terminates.

It will reactivate the next time an event triggers it.

Listening for Events

Service workers primarily function by listening for browser events using the chrome API. When an event fires, your worker code springs into action!

Common events include:

  • chrome.runtime.onInstalled: When the extension is installed or updated.
  • chrome.action.onClicked: When the extension's toolbar icon is clicked.

Event: On Extension Installed

Let's write a simple service worker that logs a message when your extension is first installed or updated. This is a great place for one-time setup tasks.

Create a file named service-worker.js:

// service-worker.js
chrome.runtime.onInstalled.addListener(() => {
  console.log('CoddyKit Extension installed or updated!');
  // You could set default preferences here
  chrome.storage.sync.set({ 'theme': 'light' });
});

Event: On Extension Icon Click

Another common event is when the user clicks your extension's icon in the browser toolbar. You can make your service worker respond to this interaction, for example, by opening a new tab.

// service-worker.js
chrome.action.onClicked.addListener((tab) => {
  console.log('Extension icon clicked! Current tab ID:', tab.id);
  // Open a new tab to a specific URL
  chrome.tabs.create({ url: 'https://www.google.com/search?q=CoddyKit' });
});

Debugging Your Service Worker

To see your console.log messages and debug your service worker, you'll use the browser's Developer Tools.

In Chrome/Edge, go to chrome://extensions, enable 'Developer mode', find your extension, and click the 'service worker' link to open its dedicated DevTools window.

Quick Check: Service Worker Role

What is a key characteristic of Manifest V3 background service workers compared to Manifest V2 background pages?

Recap: Background Service Workers

Great job! You've learned about the fundamental role of Background Service Workers in Manifest V3 extensions:

  • They are the event-driven 'brains' of your extension.
  • They are declared in manifest.json.
  • They have a temporary lifecycle, waking for events and then terminating.
  • You use event listeners (e.g., chrome.runtime.onInstalled, chrome.action.onClicked) to make them react to browser events.

Next, we'll dive into how permissions control what your extension can do!

Często zadawane pytania

Czy lekcja „Background service workery” jest bezpłatna?

Tak — pełny tekst „Background service workery” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Browser Extensions Development (Chrome & Edge), przejdź na CoddyKit PRO. Kurs Browser Extensions Development (Chrome & Edge) zawiera 4 lekcji w sumie.

Co nauczysz się w „Background service workery”?

Dowiedz się, jak background service workery zarządzają zdarzeniami, trwałą logiką i cyklem życia rozszerzenia. Ćwiczysz Browser Extensions Development (Chrome & Edge) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Browser Extensions Development (Chrome & Edge)?

Nie wymagamy żadnego doświadczenia. Browser Extensions Development (Chrome & Edge) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Background service workery”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Browser Extensions Development (Chrome & Edge)?

Tak. Każda lekcja Browser Extensions Development (Chrome & Edge) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Poznawanie struktury Manifest V3
  2. Background service workery
  3. Uprawnienia i dopasowywanie hostów
  4. Action, ikony i przycisk na pasku narzędzi
← Powrót do Browser Extensions Development (Chrome & Edge)