확장 프로그램 구성 요소 디버깅
Chrome 및 Edge 개발자 도구를 사용하여 백그라운드 서비스 워커, 팝업 및 콘텐츠 스크립트를 효율적으로 디버깅합니다.
확장 프로그램 구성 요소 디버깅은(는) CoddyKit의 무료 Browser Extensions Development (Chrome & Edge) 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Browser Extensions Development (Chrome & Edge) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Browser Extensions Development (Chrome & Edge) 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Welcome to Extension Debugging
Debugging is the process of finding and fixing errors in your code. For browser extensions, it's crucial to understand how to debug different parts, like background scripts, popups, and content scripts.
Since extensions run in unique environments, the standard web debugging techniques need a slight adjustment. We'll explore how to use Chrome and Edge Developer Tools effectively.
Opening Extension DevTools
To debug your extension, you'll primarily use the browser's Developer Tools (DevTools). The way you open them depends on which part of your extension you want to inspect.
- Go to
chrome://extensions(oredge://extensions). - Ensure "Developer mode" is enabled.
- Find your extension and click "Inspect views" for background scripts or specific popups.
Inspecting Background Scripts
Background service workers are event-driven scripts that run in the background. They don't have a visible UI, so their DevTools instance is separate.
- On the
chrome://extensionspage, under your extension, look for the "Inspect views" link, specifically for "service worker" or "background page." - Clicking this link opens a dedicated DevTools window where you can see console logs, set breakpoints, and inspect variables for your background script.
Hands-on: Background Script
Let's see a simple background script in action and how to debug it. This script just logs a message when the extension starts up.
After loading this extension, open its background service worker DevTools from chrome://extensions. You'll see "Background script loaded!" and other messages in the console.
// background.js
console.log("Background script loaded!");
chrome.runtime.onInstalled.addListener(() => {
console.log("Extension installed or updated!");
});
chrome.action.onClicked.addListener((tab) => {
console.log("Extension icon clicked!");
// This would typically trigger a popup or other action
});Inspecting Popups & Options
Popups and options pages are essentially regular HTML pages with their own JavaScript and CSS. This means you debug them just like any other webpage.
- For a popup: Right-click anywhere on the popup when it's open and select "Inspect" or "Inspect Element."
- For an options page: Open the options page (usually from the extension management page or a direct link) and then right-click and "Inspect."
This opens a standard DevTools window for that specific UI component.
Hands-on: Popup UI
Here's the JavaScript for a basic popup. When you open the popup, this script runs and logs a message. You can inspect this popup's elements and console output directly.
To debug: Click your extension icon to open the popup, then right-click on the popup and choose "Inspect". Check the Console and Elements tabs.
// popup.js
document.addEventListener('DOMContentLoaded', () => {
console.log('Popup script loaded!');
const button = document.getElementById('myButton');
if (button) {
button.addEventListener('click', () => {
alert('Button clicked!');
console.log('Button was clicked!');
});
}
});Inspecting Content Scripts
Content scripts run in the context of a web page. This means they can interact with the page's DOM, but also that their DevTools are linked to the page itself.
- Open the web page where your content script is injected.
- Open the page's DevTools (usually F12 or right-click -> "Inspect").
- In the "Sources" tab, you'll often find your content script under a special
chrome-extension://ormoz-extension://URL.
Be aware that content scripts run in an isolated world, meaning they don't directly share global variables with the page's own JavaScript.
Hands-on: Content Script
This content script changes the background color of the body and logs a message when injected into a web page. Load this as part of an extension configured to run on a specific site (e.g., *://*/*).
To debug: Navigate to a web page where your script is injected, open the page's DevTools, and check the Console. You can also find the script in the Sources tab.
// content.js
console.log("Content script injected!");
// Example: Change background color
document.body.style.backgroundColor = "lightblue";
// Example: Add a button to the page
const newButton = document.createElement("button");
newButton.textContent = "Extension Button";
newButton.style.position = "fixed";
newButton.style.bottom = "10px";
newButton.style.right = "10px";
newButton.addEventListener("click", () => {
alert("Button from extension clicked!");
});
document.body.appendChild(newButton);Essential Debugging Tools
Beyond just seeing errors, DevTools offer powerful features:
- Breakpoints: Pause execution at specific lines of code to inspect variables and step through your program. Click on the line number in the "Sources" tab to set one.
- Console API: Use
console.log(),console.warn(),console.error()for quick output. Also,console.dir()for object inspection. - Network Tab: Monitor all network requests made by your extension or the page it interacts with.
- Elements Tab: Inspect and modify the HTML and CSS of your popup, options page, or the web page (for content scripts).
Advanced Debugging Tips
Here are a few more tips for effective debugging:
- Source Maps: If you're using a build tool (like Webpack or Rollup), ensure source maps are generated for easier debugging of original source code.
debugger;statement: Adddebugger;directly in your JavaScript code to programmatically trigger a breakpoint when DevTools are open.- Preserve Log: In the DevTools console settings, enable "Preserve log" to prevent the console from clearing on navigation or service worker restarts.
- Conditional Breakpoints: Right-click a breakpoint to add conditions, so it only pauses when a specific expression is true.
Debugging Component Quiz
You've learned how to access DevTools for different parts of your extension. Let's test your understanding.
Debugging Extension Recap
Great job! You've learned the essentials of debugging browser extensions.
- Background service workers are debugged via their dedicated "Inspect views" link on
chrome://extensions. - Popups and options pages are debugged like regular web pages: right-click and "Inspect."
- Content scripts are debugged by opening DevTools for the web page they are injected into.
- Utilize breakpoints, console logging, and other DevTools features to efficiently find and fix issues in your extension.
Mastering these techniques is key to building robust and error-free extensions!
자주 묻는 질문
“확장 프로그램 구성 요소 디버깅” 강의는 무료인가요?
네 — “확장 프로그램 구성 요소 디버깅” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Browser Extensions Development (Chrome & Edge) 강의 전체를 잠금 해제할 수 있습니다. Browser Extensions Development (Chrome & Edge) 강의에는 총 4개의 강의가 포함되어 있습니다.
“확장 프로그램 구성 요소 디버깅”에서 뭘 배우나요?
Chrome 및 Edge 개발자 도구를 사용하여 백그라운드 서비스 워커, 팝업 및 콘텐츠 스크립트를 효율적으로 디버깅합니다. 브라우저에서 직접 실행하는 실습 코드로 Browser Extensions Development (Chrome & Edge)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Browser Extensions Development (Chrome & Edge)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Browser Extensions Development (Chrome & Edge)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“확장 프로그램 구성 요소 디버깅” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Browser Extensions Development (Chrome & Edge) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Browser Extensions Development (Chrome & Edge) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 확장 프로그램 구성 요소 디버깅
- 확장 프로그램 단위 테스트 작성
- 성능 최적화 전략
- 로그 기록, 오류 보고와 진단