0Pricing
Browser Extensions Development (Chrome & Edge) · 课时

响应全能框输入

处理用户从全能框输入的内容,提供建议,并根据输入文本执行自定义逻辑

响应全能框输入 是 CoddyKit 上的免费 Browser Extensions Development (Chrome & Edge) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.

常见问题解答

「响应全能框输入」课时是免费的吗?

是的 — 「响应全能框输入」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Browser Extensions Development (Chrome & Edge) 课程的其余内容,请升级到 CoddyKit PRO。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。

「响应全能框输入」这节课中我会学到什么?

处理用户从全能框输入的内容,提供建议,并根据输入文本执行自定义逻辑 你通过在浏览器中直接运行的动手代码来练习 Browser Extensions Development (Chrome & Edge),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Browser Extensions Development (Chrome & Edge) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Browser Extensions Development (Chrome & Edge) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「响应全能框输入」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Browser Extensions Development (Chrome & Edge) 课中编写并运行代码吗?

能。每节 Browser Extensions Development (Chrome & Edge) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 添加上下文菜单项
  2. 全能框关键字集成
  3. 响应全能框输入
  4. 使用 Commands API 设置键盘快捷键
← 返回 Browser Extensions Development (Chrome & Edge)