From 5a205dcc434207135d2e8e992c38c8453fa40b2c Mon Sep 17 00:00:00 2001 From: Anton Shmigirilov Date: Sun, 6 Apr 2014 13:21:58 +0400 Subject: improve click action: open site_url in empty/existing/new tab --- js/background.js | 39 +++++++++++++++++++++++++++++++++++++-- manifest.json | 1 + 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://*/*"] -- cgit v1.2.3