From 92a1e170e15131e65be5144e101ea6e4057fc09b Mon Sep 17 00:00:00 2001
From: Daniel Campoverde <alx@sillybytes.net>
Date: Sun, 5 Nov 2017 18:45:38 -0500
Subject: Keep previous selected tab

---
 src/background/tabs.js | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

(limited to 'src/background')

diff --git a/src/background/tabs.js b/src/background/tabs.js
index ce48eda..06831f8 100644
--- a/src/background/tabs.js
+++ b/src/background/tabs.js
@@ -1,4 +1,3 @@
-// var prevSelTab = null;
 var prevSelTab = 0;
 
 const closeTab = (id) => {
@@ -61,6 +60,10 @@ const getCompletions = (keyword) => {
 };
 
 const selectPrevTab = (current, count) => {
+  browser.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
+    prevSelTab = tabs[0].id;
+  });
+
   return browser.tabs.query({ currentWindow: true }).then((tabs) => {
     if (tabs.length < 2) {
       return;
@@ -72,6 +75,10 @@ const selectPrevTab = (current, count) => {
 };
 
 const selectNextTab = (current, count) => {
+  browser.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
+    prevSelTab = tabs[0].id;
+  });
+
   return browser.tabs.query({ currentWindow: true }).then((tabs) => {
     if (tabs.length < 2) {
       return;
@@ -83,6 +90,10 @@ const selectNextTab = (current, count) => {
 };
 
 const selectFirstTab = () => {
+  browser.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
+    prevSelTab = tabs[0].id;
+  });
+
   return browser.tabs.query({ currentWindow: true }).then((tabs) => {
     let id = tabs[0].id;
     return browser.tabs.update(id, { active: true });
@@ -90,6 +101,10 @@ const selectFirstTab = () => {
 };
 
 const selectLastTab = () => {
+  browser.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
+    prevSelTab = tabs[0].id;
+  });
+
   return browser.tabs.query({ currentWindow: true }).then((tabs) => {
     let id = tabs[tabs.length - 1].id;
     return browser.tabs.update(id, { active: true });
@@ -97,14 +112,15 @@ const selectLastTab = () => {
 };
 
 const selectPrevSelTab = () => {
-  if (prevSelTab != null) {
-    return browser.tabs.query({ currentWindow: true }).then((tabs) => {
-      let id = tabs[prevSelTab].id;
-      return browser.tabs.update(id, { active: true });
-    });
-  } else {
-    // some error message
-  }
+  var tmpPrevSelTab = null;
+  browser.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
+    tmpPrevSelTab = tabs[0].id;
+  });
+
+  return browser.tabs.query({ currentWindow: true }).then((tabs) => {
+    browser.tabs.update(prevSelTab, { active: true });
+    prevSelTab = tmpPrevSelTab;
+  });
 };
 
 const reload = (current, cache) => {
-- 
cgit v1.2.3