mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
Added an example of the search API (#372)
* Added an example of the search API * Fix manifest.json
This commit is contained in:
20
menu-search/README.md
Normal file
20
menu-search/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# menu-search
|
||||
|
||||
A demo of the [search API](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/search/).
|
||||
|
||||
## What it does
|
||||
|
||||
This add-on retrieves the list of installed search engines, and adds a context menu item for each one, displayed in the "selection" context.
|
||||
|
||||
Each context menu item searches for the selected text using the corresponding search engine.
|
||||
|
||||
This enables a user to:
|
||||
* select some text to search for
|
||||
* activate the context menu
|
||||
* choose which search engine to use for the search.
|
||||
|
||||
## What it shows
|
||||
|
||||
* How to retrieve the set of search engines.
|
||||
* How to search using a specific search engine.
|
||||
|
||||
26
menu-search/background.js
Normal file
26
menu-search/background.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
Create a menu item for each installed search engine.
|
||||
The ID and title are both set to the search engine's name.
|
||||
*/
|
||||
function createMenuItem(engines) {
|
||||
for (let engine of engines) {
|
||||
browser.menus.create({
|
||||
id: engine.name,
|
||||
title: engine.name,
|
||||
contexts: ["selection"]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
browser.search.get().then(createMenuItem);
|
||||
|
||||
/*
|
||||
Search using the search engine whose name matches the
|
||||
menu item's ID.
|
||||
*/
|
||||
browser.menus.onClicked.addListener((info, tab) => {
|
||||
browser.search.search({
|
||||
query: info.selectionText,
|
||||
engine: info.menuItemId
|
||||
});
|
||||
});
|
||||
1
menu-search/icons/LICENSE
Normal file
1
menu-search/icons/LICENSE
Normal file
@@ -0,0 +1 @@
|
||||
The "page-32.png" and "page-48.png" icons are taken from the miu iconset created by Linh Pham Thi Dieu, and are used under the terms of its license: http://linhpham.me/miu/.
|
||||
BIN
menu-search/icons/page-16.png
Normal file
BIN
menu-search/icons/page-16.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 251 B |
BIN
menu-search/icons/page-32.png
Normal file
BIN
menu-search/icons/page-32.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 344 B |
BIN
menu-search/icons/page-48.png
Normal file
BIN
menu-search/icons/page-48.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 310 B |
28
menu-search/manifest.json
Normal file
28
menu-search/manifest.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
|
||||
"manifest_version": 2,
|
||||
"name": "Search using...",
|
||||
"description": "Search ",
|
||||
"version": "1.0",
|
||||
"applications": {
|
||||
"gecko": {
|
||||
"strict_min_version": "63.0b14"
|
||||
}
|
||||
},
|
||||
|
||||
"background": {
|
||||
"scripts": ["background.js"]
|
||||
},
|
||||
|
||||
"permissions": [
|
||||
"menus",
|
||||
"search"
|
||||
],
|
||||
|
||||
"icons": {
|
||||
"16": "icons/page-16.png",
|
||||
"32": "icons/page-32.png",
|
||||
"48": "icons/page-48.png"
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user