aboutsummaryrefslogtreecommitdiff
path: root/src/background/tabs.js
blob: 899284ddf82eb7a33dcf60925878d8507e1f48dd (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
const closeTab = (id) => {
  browser.tabs.remove(id);
};

const reopenTab = () => {
  browser.sessions.getRecentlyClosed({
    maxResults: 1
  }).then((sessions) => {
    if (sessions.length === 0) {
      return;
    }
    let session = sessions[0];
    if (session.tab) {
      browser.sessions.restore(session.tab.sessionId);
    } else {
      browser.sessions.restore(session.window.sessionId);
    }
  });
};

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

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

export { closeTab, reopenTab, selectNextTab, selectPrevTab };