From 582cc9a25a604766aa8c2705f0736e71cb640895 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 7 May 2018 20:53:00 +0900 Subject: [PATCH] Open URLs from bookmarks --- manifest.json | 3 ++- .../shared/completions/bookmarks.js | 15 +++++++++++++ src/background/shared/completions/index.js | 22 ++++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/background/shared/completions/bookmarks.js diff --git a/manifest.json b/manifest.json index 39d14f4..f4b0e75 100644 --- a/manifest.json +++ b/manifest.json @@ -32,7 +32,8 @@ "storage", "tabs", "clipboardRead", - "notifications" + "notifications", + "bookmarks" ], "web_accessible_resources": [ "build/console.html", diff --git a/src/background/shared/completions/bookmarks.js b/src/background/shared/completions/bookmarks.js new file mode 100644 index 0000000..aedaccf --- /dev/null +++ b/src/background/shared/completions/bookmarks.js @@ -0,0 +1,15 @@ +const getCompletions = (keywords) => { + return browser.bookmarks.search({ query: keywords }).then((items) => { + return items.filter((item) => { + let url = undefined; + try { + url = new URL(item.url); + } catch (e) { + return false; + } + return item.type === 'bookmark' && url.protocol !== 'place:'; + }); + }); +}; + +export { getCompletions }; diff --git a/src/background/shared/completions/index.js b/src/background/shared/completions/index.js index 5bfdbfb..8ecdcfc 100644 --- a/src/background/shared/completions/index.js +++ b/src/background/shared/completions/index.js @@ -1,5 +1,6 @@ import * as tabs from './tabs'; import * as histories from './histories'; +import * as bookmarks from './bookmarks'; const getSearchCompletions = (command, keywords, searchConfig) => { let engineNames = Object.keys(searchConfig.engines); @@ -23,11 +24,24 @@ const getHistoryCompletions = (command, keywords) => { }); }; +const getBookmarksCompletions = (command, keywords) => { + return bookmarks.getCompletions(keywords).then((items) => { + return items.map((item) => { + return { + caption: item.title, + content: command + ' ' + item.url, + url: item.url, + }; + }); + }); +}; + const getOpenCompletions = (command, keywords, searchConfig) => { return Promise.all([ getSearchCompletions(command, keywords, searchConfig), getHistoryCompletions(command, keywords), - ]).then(([engineItems, historyItems]) => { + getBookmarksCompletions(command, keywords), + ]).then(([engineItems, historyItems, bookmarkItems]) => { let completions = []; if (engineItems.length > 0) { completions.push({ @@ -41,6 +55,12 @@ const getOpenCompletions = (command, keywords, searchConfig) => { items: historyItems }); } + if (bookmarkItems.length > 0) { + completions.push({ + name: 'Bookmarks', + items: bookmarkItems + }); + } return completions; }); };