백그라운드 서비스 워커
백그라운드 서비스 워커가 확장 프로그램에서 이벤트, 지속적인 로직 및 수명 주기를 관리하는 방법을 배웁니다.
백그라운드 서비스 워커은(는) CoddyKit의 무료 Browser Extensions Development (Chrome & Edge) 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Browser Extensions Development (Chrome & Edge) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Browser Extensions Development (Chrome & Edge) 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
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!
자주 묻는 질문
“백그라운드 서비스 워커” 강의는 무료인가요?
네 — “백그라운드 서비스 워커” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Browser Extensions Development (Chrome & Edge) 강의 전체를 잠금 해제할 수 있습니다. Browser Extensions Development (Chrome & Edge) 강의에는 총 4개의 강의가 포함되어 있습니다.
“백그라운드 서비스 워커”에서 뭘 배우나요?
백그라운드 서비스 워커가 확장 프로그램에서 이벤트, 지속적인 로직 및 수명 주기를 관리하는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Browser Extensions Development (Chrome & Edge)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Browser Extensions Development (Chrome & Edge)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Browser Extensions Development (Chrome & Edge)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“백그라운드 서비스 워커” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Browser Extensions Development (Chrome & Edge) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Browser Extensions Development (Chrome & Edge) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Manifest V3 구조 이해
- 백그라운드 서비스 워커
- 권한 및 호스트 일치
- 작업, 아이콘과 도구 모음 버튼