aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion/impl/TabRepositoryImpl.ts
blob: 6692b273bb7a2368961b92012ee8cb21d2c07d4a (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
import TabRepository, { Tab } from "../TabRepository";

const COMPLETION_ITEM_LIMIT = 10;

export default class TabRepositoryImpl implements TabRepository {
  async queryTabs(query: string, excludePinned: boolean): Promise<Tab[]> {
    const tabs = await browser.tabs.query({ currentWindow: true });
    return tabs
      .filter((t) => {
        return t.url && t.url.toLowerCase().includes(query.toLowerCase()) ||
          t.title && t.title.toLowerCase().includes(query.toLowerCase());
      })
      .filter((t) => {
        return !(excludePinned && t.pinned);
      })
      .filter(item => item.id && item.title && item.url)
      .slice(0, COMPLETION_ITEM_LIMIT)
      .map(item => ({
        id: item.id!!,
        url: item.url!!,
        active: item.active,
        title: item.title!!,
        faviconUrl: item.favIconUrl,
        index: item.index,
      }))
  }
}