aboutsummaryrefslogtreecommitdiff
path: root/src/background/presenters/TabPresenter.ts
blob: 09cfa23cc9c29f05cceff6b877f835a5a83734db (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import MemoryStorage from "../infrastructures/MemoryStorage";

const CURRENT_SELECTED_KEY = "tabs.current.selected";
const LAST_SELECTED_KEY = "tabs.last.selected";

type Tab = browser.tabs.Tab;

export default interface TabPresenter {
  open(url: string, tabId?: number): Promise<Tab>;

  create(url: string, opts?: object): Promise<Tab>;

  getCurrent(): Promise<Tab>;

  getAll(): Promise<Tab[]>;

  getLastSelectedId(): Promise<number | undefined>;

  getByKeyword(keyword: string, excludePinned: boolean): Promise<Tab[]>;

  select(tabId: number): Promise<void>;

  remove(ids: number[]): Promise<void>;

  reopen(): Promise<void>;

  reload(tabId: number, cache: boolean): Promise<void>;

  setPinned(tabId: number, pinned: boolean): Promise<void>;

  duplicate(id: number): Promise<Tab>;

  getZoom(tabId: number): Promise<number>;

  setZoom(tabId: number, factor: number): Promise<void>;

  onSelected(
    listener: (arg: { tabId: number; windowId: number }) => void
  ): void;
}

export class TabPresenterImpl implements TabPresenter {
  open(url: string, tabId?: number): Promise<Tab> {
    return browser.tabs.update(tabId, { url });
  }

  create(url: string, opts?: object): Promise<Tab> {
    return browser.tabs.create({ url, ...opts });
  }

  async getCurrent(): Promise<Tab> {
    const tabs = await browser.tabs.query({
      active: true,
      currentWindow: true,
    });
    return tabs[0];
  }

  getAll(): Promise<Tab[]> {
    return browser.tabs.query({ currentWindow: true });
  }

  async getLastSelectedId(): Promise<number | undefined> {
    const cache = new MemoryStorage();
    const tabId = await cache.get(LAST_SELECTED_KEY);
    if (tabId === null || typeof tabId === "undefined") {
      return;
    }
    return tabId;
  }

  async getByKeyword(keyword: string, excludePinned = false): Promise<Tab[]> {
    const tabs = await browser.tabs.query({ currentWindow: true });
    return tabs
      .filter((t) => {
        return (
          (t.url && t.url.toLowerCase().includes(keyword.toLowerCase())) ||
          (t.title && t.title.toLowerCase().includes(keyword.toLowerCase()))
        );
      })
      .filter((t) => {
        return !(excludePinned && t.pinned);
      });
  }

  async select(tabId: number): Promise<void> {
    await browser.tabs.update(tabId, { active: true });
  }

  async remove(ids: number[]): Promise<void> {
    await browser.tabs.remove(ids);
  }

  async reopen(): Promise<void> {
    const window = await browser.windows.getCurrent();
    const sessions = await browser.sessions.getRecentlyClosed();
    const session = sessions.find((s) => {
      return s.tab && s.tab.windowId === window.id;
    });
    if (!session) {
      return;
    }
    if (session.tab && session.tab.sessionId) {
      await browser.sessions.restore(session.tab.sessionId);
    } else if (session.window && session.window.sessionId) {
      await browser.sessions.restore(session.window.sessionId);
    }
  }

  async reload(tabId: number, cache: boolean): Promise<void> {
    await browser.tabs.reload(tabId, { bypassCache: cache });
  }

  async setPinned(tabId: number, pinned: boolean): Promise<void> {
    await browser.tabs.update(tabId, { pinned });
  }

  duplicate(id: number): Promise<Tab> {
    return browser.tabs.duplicate(id);
  }

  getZoom(tabId: number): Promise<number> {
    return browser.tabs.getZoom(tabId);
  }

  setZoom(tabId: number, factor: number): Promise<void> {
    return browser.tabs.setZoom(tabId, factor);
  }

  onSelected(
    listener: (arg: { tabId: number; windowId: number }) => void
  ): void {
    browser.tabs.onActivated.addListener(listener);
  }
}

const tabPresenter = new TabPresenterImpl();
tabPresenter.onSelected((tab: any) => {
  const cache = new MemoryStorage();

  const lastId = cache.get(CURRENT_SELECTED_KEY);
  if (lastId) {
    cache.set(LAST_SELECTED_KEY, lastId);
  }
  cache.set(CURRENT_SELECTED_KEY, tab.tabId);
});