diff --git a/CHANGELOG.md b/CHANGELOG.md index b818ee1..b37b9e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ Check out past and upcoming releases here! ## [Unreleased] +### Fixed +- Bug with idle extension causing stumble page to open in new tab instead of existing. + ## [1.0.5] - 29 Feb 2020 ### Added - Additional info bubble on every stumble showing the site and source awesome-list diff --git a/extension/background.js b/extension/background.js index c49fed1..7f1c876 100644 --- a/extension/background.js +++ b/extension/background.js @@ -4,6 +4,12 @@ * Keep track of the StumbleUponAwesome tab */ var stumbleTabId = null; + +/** + * The focused window ID + */ +var windowId = null; + var totalUrls = 0; // to be counted on first run var os = 'mac'; // mac", "win", "android", "cros", "linux" @@ -65,8 +71,8 @@ function loadUrl() { } catch (exception) { chrome.tabs.update({ url: stumbleUrl.url, - }, function (tab) { - stumbleTabId = tab.id + }, async function (tab) { + await saveStumbleTabId(tab.id); }) } } @@ -74,8 +80,8 @@ function loadUrl() { else { chrome.tabs.create({ url: stumbleUrl.url, - }, function (tab) { - stumbleTabId = tab.id + }, async function (tab) { + await saveStumbleTabId(tab.id); }) } } @@ -217,8 +223,26 @@ async function animateIcon() { }); } -function resetIcon() { +const saveStumbleTabId = tabId => { + return new Promise((resolve, reject) => { + chrome.storage.local.set({ 'stumbleTabId': tabId }, function () { + console.log(`Saved stumble tabId: ${tabId}`); + stumbleTabId = tabId; + resolve(); + }); + }) +} +const getStumbleTabId = () => { + return new Promise((resolve, reject) => { + chrome.storage.local.get(['stumbleTabId'], function (result) { + if (result.stumbleTabId) { + resolve(result.stumbleTabId); + } else { + resolve(null); + } + }) + }); } function update() { @@ -272,7 +296,23 @@ function notifyTabWelcome() { // Load a page on click chrome.browserAction.onClicked.addListener( - function (tab) { + async function (tab) { + + const currentWindowId = await getFocusedWindowId(); + console.log(`oldWindow: ${windowId} newWindow: ${currentWindowId}`) + // Get stumble tab Id + const savedStumbleTabId = await getStumbleTabId(); + const tabs = await getBrowserTabs(); + const tabIds = tabs.map(t=>t.id); + + // Reset if necessary + if (windowId !== currentWindowId || !stumbleTabId || !tabIds.includes(savedStumbleTabId)) { + windowId = currentWindowId; + chrome.storage.local.remove(['stumbleTabId'], () => { + stumbleTabId = null; + }) + } + isPendingStumble = true; loadUrl(); animateIcon(); @@ -303,6 +343,30 @@ chrome.runtime.onInstalled.addListener(function () { }) }); +/** + * Return the list of Chrome tabs + * @returns {Promise>} + */ +const getBrowserTabs = async () => { + return new Promise((resolve, reject) => { + chrome.tabs.query({currentWindow: true}, function (tabs) { + resolve(tabs); + }); + }) +} + +/** + * Return the recently focused window id. + * @returns {Promise} + */ +const getFocusedWindowId = () => { + return new Promise((resolve, reject) => { + chrome.windows.getCurrent(window => { + resolve(window.id); + }); + }); +} + /* Context menu */