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

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)
      .map(TabRepositoryImpl.toEntity);
  }

  async getAllTabs(excludePinned: boolean): Promise<Tab[]> {
    if (excludePinned) {
      return (
        await browser.tabs.query({ currentWindow: true, pinned: true })
      ).map(TabRepositoryImpl.toEntity);
    }
    return (await browser.tabs.query({ currentWindow: true })).map(
      TabRepositoryImpl.toEntity
    );
  }

  private static toEntity(tab: browser.tabs.Tab): Tab {
    return {
      id: tab.id!!,
      url: tab.url!!,
      active: tab.active,
      title: tab.title!!,
      faviconUrl: tab.favIconUrl,
      index: tab.index,
    };
  }
}