From 04ab5ab0e493c6187e4191883d4a6f3391343e39 Mon Sep 17 00:00:00 2001 From: wbamberg Date: Fri, 12 Oct 2018 11:22:27 -0700 Subject: [PATCH] Added an example of the search API (#372) * Added an example of the search API * Fix manifest.json --- menu-search/README.md | 20 ++++++++++++++++++++ menu-search/background.js | 26 ++++++++++++++++++++++++++ menu-search/icons/LICENSE | 1 + menu-search/icons/page-16.png | Bin 0 -> 251 bytes menu-search/icons/page-32.png | Bin 0 -> 344 bytes menu-search/icons/page-48.png | Bin 0 -> 310 bytes menu-search/manifest.json | 28 ++++++++++++++++++++++++++++ 7 files changed, 75 insertions(+) create mode 100644 menu-search/README.md create mode 100644 menu-search/background.js create mode 100644 menu-search/icons/LICENSE create mode 100644 menu-search/icons/page-16.png create mode 100644 menu-search/icons/page-32.png create mode 100644 menu-search/icons/page-48.png create mode 100644 menu-search/manifest.json diff --git a/menu-search/README.md b/menu-search/README.md new file mode 100644 index 0000000..a3fb887 --- /dev/null +++ b/menu-search/README.md @@ -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. + diff --git a/menu-search/background.js b/menu-search/background.js new file mode 100644 index 0000000..29b59ed --- /dev/null +++ b/menu-search/background.js @@ -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 + }); +}); diff --git a/menu-search/icons/LICENSE b/menu-search/icons/LICENSE new file mode 100644 index 0000000..cb79109 --- /dev/null +++ b/menu-search/icons/LICENSE @@ -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/. \ No newline at end of file diff --git a/menu-search/icons/page-16.png b/menu-search/icons/page-16.png new file mode 100644 index 0000000000000000000000000000000000000000..a6fed5ce35db2eec07aa361536fb6ba9817b389a GIT binary patch literal 251 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`EX7WqAsj$Z!;#Vf4nJ za0`PlBg3pY5t|=cq7;*`i57AB^IBSk^BPThyw) zK``gYHNQo^O^PapYabjqV3a@eZ~TMBdP!HU9!KyTz9W;|k=|N9C^o z+Gk#IDQL#{UHa3;cTFPYbfBkW#EI&a;xEnBUOWy7ogg^nWhl=qfhVFSnm>g9*c{yP s%*w~`kQsN>&g0cxGipAvO4~f(O>s1qoy@j-5740up00i_>zopr001sm+yDRo literal 0 HcmV?d00001 diff --git a/menu-search/icons/page-32.png b/menu-search/icons/page-32.png new file mode 100644 index 0000000000000000000000000000000000000000..dae663dbb4989c607cb29de15c2d5300060c459d GIT binary patch literal 344 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdzmSQK*5Dp-y;YjHK@;M7UB8wRq zxP?KOkzv*x383IFPZ!6Kh}O5)?7f%+1&)984`Ja~^yE|2Fx%-6#VyUBIMq6$fry* z{)J8Fd3GP*J~orN!QCe1*}CZ82Uh4?Y-tpd$}%(TVU<-8+kGtZ$xq&UR(}?6>*N%R z?OZ7x(RII8_N44~Um5j%-1W&1s|yC9yDm!jKi9ar+^ zNc`vx(*!cU_)S_JTgN^rL~;dB2%8r3)W%Z|sSAt*ZXekB?EQ`wKU1GS7wQxq3b@a* kp0>Z(uE>6;pio#-$q%07HSn)78&qol`;+0OH(%rvLx| literal 0 HcmV?d00001 diff --git a/menu-search/icons/page-48.png b/menu-search/icons/page-48.png new file mode 100644 index 0000000000000000000000000000000000000000..ba042cdcd5f88b6666d52e2ff60c3c963bc5ac35 GIT binary patch literal 310 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA1|-9oezpTCmSQK*5Dp-y;YjHK@;M7UB8wRq zxP?KOkzv*x383H&PZ!6KjK;S!y}1rK2(Wq!+J}Fc($%%7d_v6O)^v&R%>f)IRW4U{ ztzR(xvhCEgduj>$=CgS?mNJ`NWD1;qzO;ARiRMMW)E+RcVNTn_+x2SpYo7^6LYO!$ z7+0@w46L<(?UCAP)_Cc7RMEeHU?;^14YnJ4L?lj6&g+crbqr*Re&G1?Sy!jf3Zrtn zgRV}IJr>c`+M5;I|5Y(J?~BN-t78@mcq|eGM9GY%eu^N3MIiVSo5+6-tCyP_)&7XR fz0