summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2014-04-07 16:52:51 +0400
committerAndrew Dolgov <[email protected]>2014-04-07 16:52:51 +0400
commit0a61d4407ec17ccdceb80c1331de46152426ce34 (patch)
tree579cfe05cfe5dafa748757e897183b0677dd7c91
parentf501e1b363375e7715f19962f4800e447776fe6a (diff)
parent5a205dcc434207135d2e8e992c38c8453fa40b2c (diff)
Merge pull request #2 from isolenta/tabs
improve click action: open site_url in empty/existing/new tab
-rw-r--r--js/background.js39
-rw-r--r--manifest.json1
2 files changed, 38 insertions, 2 deletions
diff --git a/js/background.js b/js/background.js
index 754e178..dec4290 100644
--- a/js/background.js
+++ b/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
diff --git a/manifest.json b/manifest.json
index 30107fd..75b6f39 100644
--- a/manifest.json
+++ b/manifest.json
@@ -14,6 +14,7 @@
"default_title": "You have no unread articles."
},
"permissions": [
+ "tabs",
"alarms",
"http://*/*",
"https://*/*"]