0Pricing
Browser Extensions Development (Chrome & Edge) · บทเรียน

รูปแบบการส่งข้อความทางเดียว

ลงมือสร้างการสื่อสารทางเดียวแบบง่ายระหว่างสคริปต์เนื้อหากับผู้ปฏิบัติงานบริการเบื้องหลัง และในทางกลับกัน

รูปแบบการส่งข้อความทางเดียว เป็นบทเรียน Browser Extensions Development (Chrome & Edge) ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Browser Extensions Development (Chrome & Edge) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

One-Way Messaging Intro

Welcome! In this lesson, we'll explore one-way communication in browser extensions. This is how different parts of your extension can talk to each other, like a content script sending data to your background service worker, or vice versa.

Think of it like sending a message without expecting an immediate reply.

Isolated Extension Worlds

Browser extensions are designed with security in mind. This means different components of your extension (like content scripts and background service workers) live in their own isolated JavaScript environments.

  • Content Scripts: Run directly on web pages.
  • Background Service Worker: Runs in the background, handling events and persistent logic.

They can't directly access each other's variables or functions. Messaging is the bridge!

CS to BG: Sending Messages

To send a one-way message from a content script to your background service worker, you use the chrome.runtime.sendMessage() method.

You pass an object (your message) as the first argument. This object can contain any data you want to send.

Content Script Sender Example

Here's a simple content script that sends a message to the background service worker when the page is clicked. Imagine tracking user interactions!

/* content.js */

document.addEventListener('click', () => {
  chrome.runtime.sendMessage({
    action: "userClicked",
    data: "The document was clicked!"
  });
  console.log("Message sent from content script.");
});

BG to CS: Receiving Messages

In your background service worker, you need to set up a listener to receive messages from content scripts (or other parts of your extension). You use chrome.runtime.onMessage.addListener() for this.

The listener function receives the message itself, sender info, and a sendResponse function (which we won't use for one-way).

Background Listener Example

This background script listens for messages. When it receives a message with action: "userClicked", it logs the data.

/* background.js */

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action === "userClicked") {
    console.log("Received from content script:", message.data);
    // No sendResponse() for one-way communication
  }
});

BG to CS: Sending Messages

Now, let's look at sending a message from your background service worker to a content script. This is slightly different because you need to specify *which* tab's content script should receive the message.

You use chrome.tabs.sendMessage(tabId, message), where tabId identifies the target tab.

Background Sender Example

This example shows a background script sending a message to the active tab's content script when the extension's icon is clicked. The message tells the content script to change the page's background.

/* background.js (continued) */

chrome.action.onClicked.addListener((tab) => {
  // Send a message to the content script in the clicked tab
  chrome.tabs.sendMessage(tab.id, {
    action: "changeBackground",
    color: "#E0FFFF" // Light Cyan
  });
  console.log("Message sent to content script in tab:", tab.id);
});

CS to BG: Receiving Messages

Just like background scripts, content scripts also use chrome.runtime.onMessage.addListener() to listen for incoming messages.

When a background script sends a message using chrome.tabs.sendMessage() to a specific tab, only the content scripts injected into that tab will receive it.

Content Script Receiver Example

This content script snippet listens for the "changeBackground" action and updates the body's background color. This allows your background script to control aspects of the webpage.

/* content.js (continued) */

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action === "changeBackground") {
    document.body.style.backgroundColor = message.color;
    console.log("Page background changed to:", message.color);
  }
});

Check Your Understanding

Which of the following are key components or methods used for one-way message passing between a content script and a background service worker?

Recap: One-Way Messaging

Great job! You've learned the fundamentals of one-way messaging in browser extensions.

  • Isolated Environments: Extension components operate independently.
  • chrome.runtime.sendMessage(): Used to send messages from content scripts to background or other parts.
  • chrome.tabs.sendMessage(): Used by background scripts to send messages to specific content scripts.
  • chrome.runtime.onMessage.addListener(): Used by both content scripts and background scripts to listen for incoming messages.

This is a crucial skill for building interactive and robust extensions!

คำถามที่พบบ่อย

บทเรียน “รูปแบบการส่งข้อความทางเดียว” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “รูปแบบการส่งข้อความทางเดียว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “รูปแบบการส่งข้อความทางเดียว” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Browser Extensions Development (Chrome & Edge) นี้ได้ไหม

ได้ บทเรียน Browser Extensions Development (Chrome & Edge) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รูปแบบการส่งข้อความทางเดียว
  2. การส่งข้อความสองทางระหว่างส่วนประกอบ
  3. การใช้ Chrome Storage API
  4. พื้นที่จัดเก็บแบบซิงค์เทียบกับแบบเฉพาะเครื่องและโควตา
← กลับไปที่ Browser Extensions Development (Chrome & Edge)