aboutsummaryrefslogtreecommitdiff
path: root/src/background/repositories/CompletionsRepository.ts
blob: dfecff0ea57d64b131e32b3f5fecce4ff4a1a15f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { injectable } from 'tsyringe';

type Tab = browser.tabs.Tab;
type BookmarkTreeNode = browser.bookmarks.BookmarkTreeNode;

@injectable()
export default class CompletionsRepository {
  async queryBookmarks(keywords: string): Promise<BookmarkTreeNode[]> {
    const 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[]> {
    const 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);
    });
  }
}