Répondre aux saisies dans l’omnibox
Traitez les saisies utilisateur provenant de l’omnibox, fournissez des suggestions et exécutez une logique personnalisée en fonction du texte saisi.
Répondre aux saisies dans l’omnibox est une leçon Browser Extensions Development (Chrome & Edge) gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Browser Extensions Development (Chrome & Edge), et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Browser Extensions Development (Chrome & Edge) comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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
textthe user has typed. - You get a
suggestcallback 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
textentered 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.onInputChangedto provide dynamic suggestions as the user types. - Suggestions are an array of objects with
contentand optionaldescriptionfields. chrome.omnibox.onInputEnteredallows your extension to execute a specific action when the user presses Enter.- You can also set a
setDefaultSuggestionfor 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.
Questions Fréquemment Posées
La leçon « Répondre aux saisies dans l’omnibox » est-elle gratuite ?
Oui — le texte complet de « Répondre aux saisies dans l’omnibox » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Browser Extensions Development (Chrome & Edge), passe à CoddyKit PRO. Le cours Browser Extensions Development (Chrome & Edge) comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Répondre aux saisies dans l’omnibox » ?
Traitez les saisies utilisateur provenant de l’omnibox, fournissez des suggestions et exécutez une logique personnalisée en fonction du texte saisi. Tu pratiques Browser Extensions Development (Chrome & Edge) avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Browser Extensions Development (Chrome & Edge) ?
Aucune expérience préalable n'est requise. Browser Extensions Development (Chrome & Edge) sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.
Combien de temps prend la leçon « Répondre aux saisies dans l’omnibox » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Browser Extensions Development (Chrome & Edge) ?
Oui. Chaque leçon Browser Extensions Development (Chrome & Edge) inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Ajouter des éléments au menu contextuel
- Intégrer des mots-clés à l’omnibox
- Répondre aux saisies dans l’omnibox
- Raccourcis clavier avec l’API Commands