Browse Source

Merge pull request #2 from isolenta/tabs

improve click action: open site_url in empty/existing/new tab
Andrew Dolgov 5 years ago
parent
commit
0a61d4407e
2 changed files with 38 additions and 2 deletions
  1. 37 2
      js/background.js
  2. 1 0
      manifest.json

+ 37 - 2
js/background.js

@@ -158,13 +158,48 @@ function timeout() {
   prefs_last_updated = prefs_updated;
 }
 
+function is_newtab(url) {
+	return url.indexOf('chrome://newtab/') == 0;
+}
+
+function is_site_url(url) {
+    var site_url = localStorage['site_url'];
+	return url.indexOf(site_url) == 0;
+}
+
 function init() {
   chrome.browserAction.onClicked.addListener(function() {
     var site_url = localStorage['site_url'];
 
     if (site_url) {
-      chrome.tabs.create({url: site_url});
-    }
+      // try to find already opened tab
+      chrome.tabs.query({currentWindow: true}, function(tabs) {
+
+        var found_existing = false;
+
+        for (var i = 0, tab; tab = tabs[i]; i++) {
+          if (tab.url && is_site_url(tab.url)) {
+            chrome.tabs.update(tab.id, {highlighted: true});
+            update();
+            found_existing = true;
+            return;
+          }
+        }
+
+        // check if current tab is newtab (only if not updated yet)
+        if (!found_existing) {
+          chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
+            if (tabs[0].url && is_newtab(tabs[0].url)) {
+              chrome.tabs.update(tabs[0].id, {url: site_url});
+            } else {
+              chrome.tabs.create({url: site_url});
+            }
+          });
+        } // (if (!found_existing))
+
+      });
+
+    } // if (site_url)
   });
 
   // TODO: Create smarter algorithm that sets `periodInMinutes` to

+ 1 - 0
manifest.json

@@ -14,6 +14,7 @@
     "default_title": "You have no unread articles."
   },
   "permissions": [
+    "tabs",
     "alarms",
     "http://*/*",
     "https://*/*"]