การสื่อสารระหว่างองค์ประกอบส่วนติดต่อผู้ใช้
สร้างช่องทางการส่งข้อความสำหรับป๊อปอัป หน้าตัวเลือก และสคริปต์เบื้องหลัง เพื่อให้การไหลของข้อมูลเป็นไปอย่างราบรื่น
การสื่อสารระหว่างองค์ประกอบส่วนติดต่อผู้ใช้ เป็นบทเรียน Browser Extensions Development (Chrome & Edge) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
คำถามที่พบบ่อย
บทเรียน “การสื่อสารระหว่างองค์ประกอบส่วนติดต่อผู้ใช้” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสื่อสารระหว่างองค์ประกอบส่วนติดต่อผู้ใช้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Browser Extensions Development (Chrome & Edge) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสื่อสารระหว่างองค์ประกอบส่วนติดต่อผู้ใช้”
สร้างช่องทางการส่งข้อความสำหรับป๊อปอัป หน้าตัวเลือก และสคริปต์เบื้องหลัง เพื่อให้การไหลของข้อมูลเป็นไปอย่างราบรื่น คุณปฏิบัติ Browser Extensions Development (Chrome & Edge) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Browser Extensions Development (Chrome & Edge) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Browser Extensions Development (Chrome & Edge) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การสื่อสารระหว่างองค์ประกอบส่วนติดต่อผู้ใช้” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Browser Extensions Development (Chrome & Edge) นี้ได้ไหม
ได้ บทเรียน Browser Extensions Development (Chrome & Edge) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างส่วนติดต่อผู้ใช้ป๊อปอัปแบบโต้ตอบ
- การสร้างหน้าตัวเลือก
- การสื่อสารระหว่างองค์ประกอบส่วนติดต่อผู้ใช้
- การกำหนดธีมด้วย CSS และการรองรับโหมดมืด