0Pricing
Browser Extensions Development (Chrome & Edge) · 강의

UI 구성 요소 간 통신

팝업, 옵션 페이지 및 백그라운드 스크립트를 위한 메시지 채널을 구성하여 원활한 데이터 흐름을 보장합니다.

UI 구성 요소 간 통신은(는) CoddyKit의 무료 Browser Extensions Development (Chrome & Edge) 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Browser Extensions Development (Chrome & Edge) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Browser Extensions Development (Chrome & Edge) 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Bridge Your Extension Components

Browser extensions are made of several parts: popups, options pages, and background scripts. Each part often runs in its own isolated environment.

To build powerful extensions, these components need to talk to each other. This lesson shows you how to establish messaging channels for seamless data flow.

Isolated Environments

Think of your extension's popup, options page, and background script as separate mini-programs. They don't automatically share data or functions.

  • Popup: Appears when clicked, then closes.
  • Options Page: A dedicated settings page.
  • Background Script: Runs continuously, handles events.

Messaging allows them to send information and trigger actions across these boundaries.

UI Components Send Messages

To send a message from your popup or options page to the background script, you use the chrome.runtime.sendMessage() API.

This function lets you pass a JSON object (your message) to other parts of your extension. It's great for triggering actions or sending user input.

Popup to Background Example

Here's how a popup script might send a message to the background script, for instance, to toggle a feature. This JavaScript would be in your popup.js file, linked from popup.html.

// popup.js
document.getElementById('toggleButton').addEventListener('click', () => {
  chrome.runtime.sendMessage({ action: 'toggleFeature', value: true }, (response) => {
    console.log('Received response:', response);
  });
});

Background Listens for Messages

Your background script needs to listen for incoming messages. It does this using the chrome.runtime.onMessage.addListener() event.

This listener will fire whenever any part of your extension (like a popup or options page) sends a message via chrome.runtime.sendMessage().

Background Script Listener

This snippet shows a basic message listener in your background service worker (e.g., background.js). It checks the message's action and performs a task.

// background.js
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action === 'toggleFeature') {
    console.log('Feature toggled:', message.value);
    // Perform background task here
    sendResponse({ status: 'success', message: 'Feature updated!' });
  }
  // Important: Return true to indicate you want to send a response asynchronously
  return true; 
});

Sending a Response Back

Often, the component that sent the message (e.g., your popup) needs to know if the action was successful or receive data back.

The sendResponse function, provided as the third argument to your onMessage listener, allows the background script to send a one-time reply to the original sender.

Handling Background Responses

The callback function in chrome.runtime.sendMessage() is where your popup or options page receives the response from the background script. This completes the two-way communication.

// popup.js (modified)
document.getElementById('toggleButton').addEventListener('click', () => {
  chrome.runtime.sendMessage({ action: 'toggleFeature', value: true }, (response) => {
    if (chrome.runtime.lastError) {
      console.error('Error sending message:', chrome.runtime.lastError.message);
      return;
    }
    console.log('Background response:', response);
    // Update popup UI based on response
    document.getElementById('status').textContent = response.message;
  });
});

Options Pages Communicate Too

The same chrome.runtime.sendMessage() and chrome.runtime.onMessage.addListener() pattern applies to communication involving your options page.

An options page can send user-defined settings to the background script for persistence, and the background can reply with confirmation or fetched data.

Messaging Concepts

Consider an extension where a popup needs to ask the background script for the current count of unread notifications.

Which API should the popup use to initiate this request?

Recap: Seamless Data Flow

You've learned how to enable communication between your extension's UI components (popups, options pages) and its background script!

  • Use chrome.runtime.sendMessage() to send data.
  • Use chrome.runtime.onMessage.addListener() to receive data and respond.

This fundamental skill allows your extension's different parts to work together effectively.

자주 묻는 질문

“UI 구성 요소 간 통신” 강의는 무료인가요?

네 — “UI 구성 요소 간 통신” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Browser Extensions Development (Chrome & Edge) 강의 전체를 잠금 해제할 수 있습니다. Browser Extensions Development (Chrome & Edge) 강의에는 총 4개의 강의가 포함되어 있습니다.

“UI 구성 요소 간 통신”에서 뭘 배우나요?

팝업, 옵션 페이지 및 백그라운드 스크립트를 위한 메시지 채널을 구성하여 원활한 데이터 흐름을 보장합니다. 브라우저에서 직접 실행하는 실습 코드로 Browser Extensions Development (Chrome & Edge)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Browser Extensions Development (Chrome & Edge)을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Browser Extensions Development (Chrome & Edge)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“UI 구성 요소 간 통신” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Browser Extensions Development (Chrome & Edge) 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Browser Extensions Development (Chrome & Edge) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 대화형 팝업 UI 만들기
  2. 옵션 페이지 구축
  3. UI 구성 요소 간 통신
  4. CSS를 활용한 테마와 다크 모드 지원
← Browser Extensions Development (Chrome & Edge)(으)로 돌아가기