aboutsummaryrefslogtreecommitdiff
path: root/src/background/shared/tabs.js
blob: 33a4fac6808ef1b9da93e180939d8996b60cc721 (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
147
148
149
150
151
152
153
154
155
156
157
158
import * as tabCompletions from './completions/tabs';

const closeTab = async(id) => {
  let tab = await browser.tabs.get(id);
  if (!tab.pinned) {
    return browser.tabs.remove(id);
  }
};

const closeTabForce = (id) => {
  return browser.tabs.remove(id);
};

const queryByKeyword = async(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);
  });
};

const closeTabByKeywords = async(keyword) => {
  let tabs = await queryByKeyword(keyword, false);
  if (tabs.length === 0) {
    throw new Error('No matching buffer for ' + keyword);
  } else if (tabs.length > 1) {
    throw new Error('More than one match for ' + keyword);
  }
  return browser.tabs.remove(tabs[0].id);
};

const closeTabByKeywordsForce = async(keyword) => {
  let tabs = await queryByKeyword(keyword, true);
  if (tabs.length === 0) {
    throw new Error('No matching buffer for ' + keyword);
  } else if (tabs.length > 1) {
    throw new Error('More than one match for ' + keyword);
  }
  return browser.tabs.remove(tabs[0].id);
};

const closeTabsByKeywords = async(keyword) => {
  let tabs = await tabCompletions.getCompletions(keyword);
  tabs = tabs.filter(tab => !tab.pinned);
  return browser.tabs.remove(tabs.map(tab => tab.id));
};

const closeTabsByKeywordsForce = async(keyword) => {
  let tabs = await tabCompletions.getCompletions(keyword);
  return browser.tabs.remove(tabs.map(tab => tab.id));
};

const reopenTab = async() => {
  let window = await browser.windows.getCurrent();
  let sessions = await browser.sessions.getRecentlyClosed();
  let session = sessions.find((s) => {
    return s.tab && s.tab.windowId === window.id;
  });
  if (!session) {
    return;
  }
  if (session.tab) {
    return browser.sessions.restore(session.tab.sessionId);
  }
  return browser.sessions.restore(session.window.sessionId);
};

const selectAt = async(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 });
};

const selectByKeyword = async(current, keyword) => {
  let tabs = await queryByKeyword(keyword);
  if (tabs.length === 0) {
    throw new RangeError('No matching buffer for ' + keyword);
  }
  for (let tab of tabs) {
    if (tab.index > current.index) {
      return browser.tabs.update(tab.id, { active: true });
    }
  }
  return browser.tabs.update(tabs[0].id, { active: true });
};

const selectPrevTab = async(current, count) => {
  let tabs = await browser.tabs.query({ currentWindow: true });
  if (tabs.length < 2) {
    return;
  }
  let select = (current - count + tabs.length) % tabs.length;
  let id = tabs[select].id;
  return browser.tabs.update(id, { active: true });
};

const selectNextTab = async(current, count) => {
  let tabs = await browser.tabs.query({ currentWindow: true });
  if (tabs.length < 2) {
    return;
  }
  let select = (current + count) % tabs.length;
  let id = tabs[select].id;
  return browser.tabs.update(id, { active: true });
};

const selectFirstTab = async() => {
  let tabs = await browser.tabs.query({ currentWindow: true });
  let id = tabs[0].id;
  return browser.tabs.update(id, { active: true });
};

const selectLastTab = async() => {
  let tabs = await browser.tabs.query({ currentWindow: true });
  let id = tabs[tabs.length - 1].id;
  return browser.tabs.update(id, { active: true });
};

const selectTab = (id) => {
  return browser.tabs.update(id, { active: true });
};

const reload = (current, cache) => {
  return browser.tabs.reload(
    current.id,
    { bypassCache: cache }
  );
};

const updateTabPinned = (current, pinned) => {
  return browser.tabs.update(current.id, { pinned });
};

const toggleTabPinned = (current) => {
  return updateTabPinned(current, !current.pinned);
};

const duplicate = (id) => {
  return browser.tabs.duplicate(id);
};

export {
  closeTab, closeTabForce,
  queryByKeyword, closeTabByKeywords, closeTabByKeywordsForce,
  closeTabsByKeywords, closeTabsByKeywordsForce,
  reopenTab, selectAt, selectByKeyword,
  selectPrevTab, selectNextTab, selectFirstTab,
  selectLastTab, selectTab, reload, updateTabPinned,
  toggleTabPinned, duplicate
};