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 บทเรียน

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

What are Context Menus?

Browser extensions can add custom items to the context menu, which appears when you right-click on a web page.

These items let users perform quick actions related to what they clicked on, like saving an image, translating selected text, or triggering a custom search.

Enabling Context Menus

Before your extension can create context menu items, you need to declare the "contextMenus" permission in your manifest.json file.

This permission tells the browser your extension intends to interact with the context menu API.

{ "manifest_version": 3,
  "name": "My Menu Extension",
  "version": "1.0",
  "permissions": [
    "contextMenus"
  ],
  "background": {
    "service_worker": "background.js"
  }
}

The `chrome.contextMenus` API

The chrome.contextMenus API is how your background service worker interacts with context menus.

It provides methods to manage menu items:

  • create(): To add a new menu item.
  • update(): To change an existing item's properties.
  • remove(): To delete a specific item.
  • removeAll(): To clear all items added by your extension.

Creating Your First Item

To create a context menu item, use chrome.contextMenus.create(). You must provide a unique id and a title.

This example adds a simple item named "My First Item" to the general page context menu.

// background.js
chrome.runtime.onInstalled.addListener(() => {
  chrome.contextMenus.create({
    id: "myFirstItem",
    title: "My First Item",
    contexts: ["page"]
  });
});

Item Properties: ID & Title

Each context menu item needs a unique id. This ID helps you identify which item was clicked later.

The title is the text displayed in the menu. The contexts array specifies where the item should appear.

  • id: A unique string identifier.
  • title: The user-visible text for the menu item.
  • contexts: An array of strings defining where the item shows up.

Responding to Clicks

After creating an item, you'll want to run code when a user clicks it. Use chrome.contextMenus.onClicked.addListener() to listen for these clicks.

The listener receives an info object with details about the click, including the menuItemId, which helps you identify which item was activated.

// background.js
chrome.runtime.onInstalled.addListener(() => {
  chrome.contextMenus.create({
    id: "logClickItem",
    title: "Log This Click",
    contexts: ["page"]
  });
});

chrome.contextMenus.onClicked.addListener((info, tab) => {
  if (info.menuItemId === "logClickItem") {
    console.log("Item 'Log This Click' was clicked!");
    // You can perform other actions here
  }
});

Context Types for Specific Elements

The contexts property is powerful! It allows you to control exactly where your menu item appears.

Common context types include:

  • "page": Right-click on any part of the page.
  • "selection": Right-click on selected text.
  • "link": Right-click on a hyperlink.
  • "image": Right-click on an image.
  • "video", "audio": For media elements.

Example: Search Selected Text

Let's create an item that appears only when text is selected. When clicked, it will open a new tab to search that text on Google.

Notice the selectionText property in the info object when using the "selection" context.

// background.js
chrome.runtime.onInstalled.addListener(() => {
  chrome.contextMenus.create({
    id: "searchSelected",
    title: "Search '%s' on Google", // %s is placeholder for selected text
    contexts: ["selection"]
  });
});

chrome.contextMenus.onClicked.addListener((info, tab) => {
  if (info.menuItemId === "searchSelected" && info.selectionText) {
    const query = encodeURIComponent(info.selectionText);
    const url = `https://www.google.com/search?q=${query}`;
    chrome.tabs.create({ url: url });
  }
});

Updating and Removing Items

You can dynamically change or remove context menu items after they've been created. This is useful for adapting to user preferences or page content.

  • chrome.contextMenus.update(id, properties): Changes properties like title or contexts.
  • chrome.contextMenus.remove(id): Deletes a specific item.
  • chrome.contextMenus.removeAll(): Deletes all items added by your extension.

Context Menu Challenge

Consider the following manifest.json and background script for an extension.

Which statements about the context menu items created are true?

// manifest.json
{
  "manifest_version": 3,
  "name": "Test Menu",
  "version": "1.0",
  "permissions": ["contextMenus"],
  "background": {"service_worker": "background.js"}
}

// background.js
chrome.runtime.onInstalled.addListener(() => {
  chrome.contextMenus.create({
    id: "itemA",
    title: "Action A",
    contexts: ["page"]
  });
  chrome.contextMenus.create({
    id: "itemB",
    title: "Action B",
    contexts: ["selection"]
  });
  chrome.contextMenus.create({
    id: "itemC",
    title: "Action C",
    contexts: ["link"]
  });
});

Recap: Context Menus

You've learned how to add custom context menu items to your browser extension!

  • Declare "contextMenus" permission in manifest.json.
  • Use chrome.contextMenus.create() in your background script.
  • Specify id, title, and contexts for each item.
  • Listen for clicks using chrome.contextMenus.onClicked.addListener().
  • Contexts like "page", "selection", "link" allow precise targeting.

These skills enable you to add powerful, context-aware actions to your extension. Next, we'll explore integrating with the Omnibox!

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

บทเรียน “การเพิ่มรายการเมนูบริบท” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเพิ่มรายการเมนูบริบท” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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. การผสานรวมคำสำคัญกับ Omnibox
  3. การตอบสนองต่อข้อมูลป้อนเข้า Omnibox
  4. แป้นพิมพ์ลัดด้วย API Commands
← กลับไปที่ Browser Extensions Development (Chrome & Edge)