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

การตอบสนองต่อข้อมูลป้อนเข้า Omnibox

ประมวลผลข้อมูลป้อนเข้าของผู้ใช้จาก Omnibox แสดงคำแนะนำ และเรียกใช้ตรรกะแบบกำหนดเองตามข้อความที่ป้อน

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

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

Responding to Omnibox Input

Welcome to the lesson on making your extension smart! We've learned how to set up an omnibox keyword. Now, let's make it respond to user input.

The omnibox (address bar) is a powerful place. Your extension can listen for what users type after your keyword and react in two main ways:

  • Provide suggestions: Guide the user as they type.
  • Execute actions: Perform tasks when they press Enter.

The Omnibox API

To interact with the omnibox, we use the chrome.omnibox API. This API lives in your extension's background script (service worker).

Remember to declare the "omnibox" permission in your manifest.json file for your extension to use this API.

Listening for Input Changes

As a user types after your omnibox keyword, your extension can provide dynamic suggestions. This is handled by the chrome.omnibox.onInputChanged event.

  • It fires every time the user's input changes.
  • You receive the current text the user has typed.
  • You get a suggest callback function to send suggestions back to the omnibox.

Structuring Your Suggestions

The suggest callback expects an array of suggestion objects. Each object needs an "content" field, which is the actual text that will be inserted if the user selects the suggestion.

You can also add a "description" field for richer suggestions:

  • content: The text to use if the suggestion is selected.
  • description: (Optional) Rich HTML text displayed next to the suggestion.

Code: Dynamic Suggestions

Let's create a background script that suggests a prefix based on the user's input. Make sure your manifest.json has "omnibox": { "keyword": "go" } and the "omnibox" permission.

chrome.omnibox.onInputChanged.addListener(
  (text, suggest) => {
    const suggestions = [
      { content: text + " search", description: "Search for: " + text },
      { content: text + " docs", description: "Docs for: " + text }
    ];
    suggest(suggestions);
  }
);

Executing Actions on Enter

Once the user types their input and presses the Enter key, the chrome.omnibox.onInputEntered event is triggered. This is where you perform the main action of your extension.

  • It provides the final text entered by the user.
  • It also gives a disposition, indicating how the user wants the action to be handled (e.g., new tab, current tab).

Code: Opening a New Tab

Here's how to open a new tab with a search query based on the user's input. This code would typically be in the same background script as onInputChanged.

chrome.omnibox.onInputEntered.addListener(
  (text, disposition) => {
    const searchUrl = `https://www.google.com/search?q=${encodeURIComponent(text)}`;

    if (disposition === 'newForegroundTab') {
      chrome.tabs.create({ url: searchUrl });
    } else if (disposition === 'newBackgroundTab') {
      chrome.tabs.create({ url: searchUrl, active: false });
    } else {
      // currentTab
      chrome.tabs.update({ url: searchUrl });
    }
  }
);

Setting a Default Suggestion

You can provide a default suggestion that appears even before the user types anything. This is useful for guiding users or showing a common action.

Use chrome.omnibox.setDefaultSuggestion() with a description to set this initial hint.

chrome.omnibox.setDefaultSuggestion({
  description: 'Type a query to search Google'
});

Rich Suggestions with Descriptions

For even more helpful suggestions, you can use HTML within the description field. This allows you to highlight parts of the text, use different colors, or make it more readable.

  • Use <match> tags to highlight matching text.
  • Use <dim> tags for less important text.
  • Remember to escape special characters like < and > if they are part of your literal text.

Quick Check: Omnibox Events

Consider an extension that uses the omnibox keyword 'wiki'. Which event listener is primarily responsible for updating suggestions as the user types 'wiki cats'?

Recap: Responding to Omnibox Input

You've learned how to make your extension truly interactive with the omnibox!

  • We use chrome.omnibox.onInputChanged to provide dynamic suggestions as the user types.
  • Suggestions are an array of objects with content and optional description fields.
  • chrome.omnibox.onInputEntered allows your extension to execute a specific action when the user presses Enter.
  • You can also set a setDefaultSuggestion for initial guidance.

This powerful API lets users interact with your extension directly from the browser's address bar, making common tasks quicker and more efficient.

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

บทเรียน “การตอบสนองต่อข้อมูลป้อนเข้า Omnibox” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การตอบสนองต่อข้อมูลป้อนเข้า Omnibox”

ประมวลผลข้อมูลป้อนเข้าของผู้ใช้จาก Omnibox แสดงคำแนะนำ และเรียกใช้ตรรกะแบบกำหนดเองตามข้อความที่ป้อน คุณปฏิบัติ Browser Extensions Development (Chrome & Edge) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Browser Extensions Development (Chrome & Edge) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Browser Extensions Development (Chrome & Edge) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

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

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