mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-27 13:29:34 +07:00
* First draft * Rename folder * Tweak comments * Update index.html * remove extra closing div * Rename sw file * Apply first round of @sebastianbenz suggestions * Fix import * Update popover * Second tech review round * Removed long list of suggestions Only show past history * Move host permissions * Rename extension and folder * REmove await * Move icons into images folder
41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
console.log('sw-omnibox.js');
|
|
|
|
// Initialize default API suggestions
|
|
chrome.runtime.onInstalled.addListener(({ reason }) => {
|
|
if (reason === 'install') {
|
|
chrome.storage.local.set({
|
|
apiSuggestions: ['tabs', 'storage', 'scripting']
|
|
});
|
|
}
|
|
});
|
|
|
|
const URL_CHROME_EXTENSIONS_DOC =
|
|
'https://developer.chrome.com/docs/extensions/reference/';
|
|
const NUMBER_OF_PREVIOUS_SEARCHES = 4;
|
|
|
|
// Display the suggestions after user starts typing
|
|
chrome.omnibox.onInputChanged.addListener(async (input, suggest) => {
|
|
await chrome.omnibox.setDefaultSuggestion({
|
|
description: 'Enter a Chrome API or choose from past searches'
|
|
});
|
|
const { apiSuggestions } = await chrome.storage.local.get('apiSuggestions');
|
|
const suggestions = apiSuggestions.map((api) => {
|
|
return { content: api, description: `Open chrome.${api} API` };
|
|
});
|
|
suggest(suggestions);
|
|
});
|
|
|
|
// Open the reference page of the chosen API
|
|
chrome.omnibox.onInputEntered.addListener((input) => {
|
|
chrome.tabs.create({ url: URL_CHROME_EXTENSIONS_DOC + input });
|
|
// Save the latest keyword
|
|
updateHistory(input);
|
|
});
|
|
|
|
async function updateHistory(input) {
|
|
const { apiSuggestions } = await chrome.storage.local.get('apiSuggestions');
|
|
apiSuggestions.unshift(input);
|
|
apiSuggestions.splice(NUMBER_OF_PREVIOUS_SEARCHES);
|
|
return chrome.storage.local.set({ apiSuggestions });
|
|
}
|