diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-05-06 21:42:43 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-06 21:42:43 +0900 |
commit | 104a9666ff627b11ce7cf3a5bf096b1bc3437bc1 (patch) | |
tree | 4d995736429753d8c9d2892ef6195e6517777fc9 /src/background/shared/tabs.js | |
parent | 828ac8cd84ddae9565be67f56e452a416b305c7a (diff) | |
parent | cf30ef0cd503b3b9dee882202d64ba9db9922ee3 (diff) |
Merge pull request #386 from ueokande/addon-enabled-indicator
Addon enabled indicator
Diffstat (limited to 'src/background/shared/tabs.js')
-rw-r--r-- | src/background/shared/tabs.js | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/background/shared/tabs.js b/src/background/shared/tabs.js new file mode 100644 index 0000000..f1dcc73 --- /dev/null +++ b/src/background/shared/tabs.js @@ -0,0 +1,126 @@ +const closeTab = (id) => { + return browser.tabs.get(id).then((tab) => { + if (!tab.pinned) { + return browser.tabs.remove(id); + } + }); +}; + +const closeTabForce = (id) => { + return browser.tabs.remove(id); +}; + +const reopenTab = () => { + return browser.sessions.getRecentlyClosed({ + maxResults: 1 + }).then((sessions) => { + if (sessions.length === 0) { + return; + } + let session = sessions[0]; + if (session.tab) { + return browser.sessions.restore(session.tab.sessionId); + } + return browser.sessions.restore(session.window.sessionId); + }); +}; + +const selectAt = (index) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + 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 = (current, keyword) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + let matched = tabs.filter((t) => { + return t.url.includes(keyword) || t.title.includes(keyword); + }); + + if (matched.length === 0) { + throw new RangeError('No matching buffer for ' + keyword); + } + for (let tab of matched) { + if (tab.index > current.index) { + return browser.tabs.update(tab.id, { active: true }); + } + } + return browser.tabs.update(matched[0].id, { active: true }); + }); +}; + +const selectPrevTab = (current, count) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + 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 = (current, count) => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + 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 = () => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + let id = tabs[0].id; + return browser.tabs.update(id, { active: true }); + }); +}; + +const selectLastTab = () => { + return browser.tabs.query({ currentWindow: true }).then((tabs) => { + 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.query({ currentWindow: true, active: true }) + .then(() => { + return browser.tabs.update(current.id, { pinned: pinned }); + }); +}; + +const toggleTabPinned = (current) => { + updateTabPinned(current, !current.pinned); +}; + +const duplicate = (id) => { + return browser.tabs.duplicate(id); +}; + +export { + closeTab, closeTabForce, reopenTab, selectAt, selectByKeyword, + selectPrevTab, selectNextTab, selectFirstTab, + selectLastTab, selectTab, reload, updateTabPinned, + toggleTabPinned, duplicate +}; |