diff options
Diffstat (limited to 'src/background/repositories/CompletionsRepository.ts')
-rw-r--r-- | src/background/repositories/CompletionsRepository.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/background/repositories/CompletionsRepository.ts b/src/background/repositories/CompletionsRepository.ts new file mode 100644 index 0000000..18af587 --- /dev/null +++ b/src/background/repositories/CompletionsRepository.ts @@ -0,0 +1,37 @@ +type Tab = browser.tabs.Tab; +type BookmarkTreeNode = browser.bookmarks.BookmarkTreeNode; + +export default class CompletionsRepository { + async queryBookmarks(keywords: string): Promise<BookmarkTreeNode[]> { + let items = await browser.bookmarks.search({ query: keywords }); + return items.filter((item) => { + if (!item.url) { + return false; + } + let url = undefined; + try { + url = new URL(item.url); + } catch (e) { + return false; + } + return item.type === 'bookmark' && url.protocol !== 'place:'; + }); + } + + queryHistories(keywords: string): Promise<browser.history.HistoryItem[]> { + return browser.history.search({ + text: keywords, + startTime: 0, + }); + } + + async queryTabs(keywords: string, excludePinned: boolean): Promise<Tab[]> { + let tabs = await browser.tabs.query({ currentWindow: true }); + return tabs.filter((t) => { + return t.url && t.url.toLowerCase().includes(keywords.toLowerCase()) || + t.title && t.title.toLowerCase().includes(keywords.toLowerCase()); + }).filter((t) => { + return !(excludePinned && t.pinned); + }); + } +} |