summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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://*/*"]