aboutsummaryrefslogtreecommitdiff
path: root/src/background/presenters/tab.js
blob: be6955a641e3882847099e6e39d8ca7c1118c83a (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
export default class TabPresenter {
  open(url, tabId) {
    return browser.tabs.update(tabId, { url });
  }

  create(url, opts) {
    return browser.tabs.create({ url, ...opts });
  }

  async getCurrent() {
    let tabs = await browser.tabs.query({
      active: true, currentWindow: true
    });
    return tabs[0];
  }

  getAll() {
    return browser.tabs.query({ currentWindow: true });
  }

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

  select(tabId) {
    return browser.tabs.update(tabId, { active: true });
  }

  async selectAt(index) {
    let tabs = await browser.tabs.query({ currentWindow: true });
    if (tabs.length < 2) {
      return;
    }
    if (index < 0 || tabs.length <= index) {
      throw new RangeError(`tab ${index + 1} does not exist`);
    }
    let id = tabs[index].id;
    return browser.tabs.update(id, { active: true });
  }

  remove(ids) {
    return browser.tabs.remove(ids);
  }

  async createAdjacent(url, { openerTabId, active }) {
    let tabs = await browser.tabs.query({
      active: true, currentWindow: true
    });
    return browser.tabs.create({
      url,
      openerTabId,
      active,
      index: tabs[0].index + 1
    });
  }

  onSelected(listener) {
    browser.tabs.onActivated.addListener(listener);
  }
}