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

การผสานรวมคำสำคัญกับ Omnibox

ผสานรวมส่วนขยายของคุณกับ Omnibox ของเบราว์เซอร์ เพื่อให้ผู้ใช้เรียกใช้การทำงานหรือค้นหาได้โดยตรงจากแถบที่อยู่

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

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

What is the Omnibox?

The omnibox is the URL bar at the top of your browser. It's not just for typing website addresses; it also handles searches and can be a powerful entry point for browser extensions!

Integrating with the omnibox allows users to quickly interact with your extension directly from the address bar, without needing to click an icon or open a popup.

Power Up Your Address Bar

Why integrate your extension with the omnibox?

  • Quick Access: Users can trigger extension actions with just a few keystrokes.
  • Custom Searches: Create shortcuts for specific searches within your extension's data.
  • Streamlined Workflow: Perform tasks without interrupting their browsing flow.

Setting Your Omnibox Keyword

To enable omnibox integration, you need to declare it in your extension's manifest.json file.

You'll specify a unique keyword. When a user types this keyword into the omnibox, followed by a space, your extension takes over the input.

Manifest Keyword Example

Here's how to declare an omnibox keyword, for example, 'myext':

{  "manifest_version": 3,
  "name": "My Omnibox Extension",
  "version": "1.0",
  "omnibox": {
    "keyword": "myext"
  },
  "background": {
    "service_worker": "background.js"
  }
}

Capturing User Input

Once your keyword is set, your background service worker needs to listen for user input. The main event for this is chrome.omnibox.onInputChanged.

This event fires every time the user types a character after your keyword, allowing you to provide real-time suggestions.

Displaying Live Suggestions

The onInputChanged listener provides a suggest callback function. You use this to send an array of suggestion objects back to the omnibox.

Each suggestion object needs a content (what gets inserted if selected) and a description (what the user sees in the dropdown).

Dynamic Suggestions in Action

This background.js snippet shows how to listen for input and offer basic suggestions. Try typing 'myext hello' in your browser's omnibox after loading this extension.

// background.js
chrome.omnibox.onInputChanged.addListener(
  function(text, suggest) {
    let suggestions = [];
    if (text.startsWith("hello")) {
      suggestions.push({content: "hello world", description: "Says Hello World"});
      suggestions.push({content: "hello there", description: "Greets you"});
    } else {
      suggestions.push({content: "type 'hello'", description: "Try typing 'hello'"});
    }
    suggest(suggestions);
  }
);

Acting on User Selection

After the user types their input and optionally selects a suggestion, they press Enter. This triggers the chrome.omnibox.onInputEntered event.

This is where your extension performs the final action based on the user's input, like opening a new tab or executing a script.

Executing Actions

Inside onInputEntered, the text parameter contains the final string the user entered or selected.

A common action is to open a new tab with a specific URL using chrome.tabs.create({url: '...' }), or perform a search based on the input.

Full Omnibox Integration

Here's a more complete example combining both input handling and action execution. This example allows users to search Google directly from the omnibox using 'myext go [query]'.

// background.js
chrome.omnibox.onInputChanged.addListener(
  function(text, suggest) {
    let suggestions = [];
    if (text.startsWith("go ")) {
      const query = text.substring(3);
      suggestions.push({
        content: `https://www.google.com/search?q=${query}`,
        description: `Search Google for "${query}"`
      });
    } else {
      suggestions.push({
        content: "go [search_term]",
        description: "Type 'go' to search Google"
      });
    }
    suggest(suggestions);
  }
);

chrome.omnibox.onInputEntered.addListener(
  function(text, disposition) {
    if (text.startsWith("http://") || text.startsWith("https://")) {
      chrome.tabs.create({url: text});
    } else if (text.startsWith("go ")) {
      const query = text.substring(3);
      chrome.tabs.create({url: `https://www.google.com/search?q=${query}`});
    } else {
      console.log("Omnibox entered:", text);
      chrome.tabs.create({url: `https://www.google.com/search?q=${text}`});
    }
  }
);

Omnibox Quick Check

You've learned how to integrate your extension with the browser's omnibox. Let's test your knowledge!

Omnibox Power-Up Recap

Great job! You've learned how to integrate your extension with the browser's omnibox.

  • We defined a keyword in manifest.json.
  • We used chrome.omnibox.onInputChanged to provide dynamic suggestions as the user types.
  • We handled the final action using chrome.omnibox.onInputEntered, for example, opening a new tab.

This powerful feature allows users to interact with your extension quickly and efficiently right from their address bar!

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

บทเรียน “การผสานรวมคำสำคัญกับ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 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)