Skip to content

Commit

Permalink
Fix: better handling of multi-window and idle background script
Browse files Browse the repository at this point in the history
- Previously when the background script became idle, the stumbleTabId was lost, causing a new tab to open.
- When extension is ran in another window, tab opens in the new window
  • Loading branch information
basharovV committed Mar 23, 2020
1 parent eeda145 commit 7d81768
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 70 additions & 6 deletions extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -65,17 +71,17 @@ function loadUrl() {
} catch (exception) {
chrome.tabs.update({
url: stumbleUrl.url,
}, function (tab) {
stumbleTabId = tab.id
}, async function (tab) {
await saveStumbleTabId(tab.id);
})
}
}
// or Open New tab
else {
chrome.tabs.create({
url: stumbleUrl.url,
}, function (tab) {
stumbleTabId = tab.id
}, async function (tab) {
await saveStumbleTabId(tab.id);
})
}
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -303,6 +343,30 @@ chrome.runtime.onInstalled.addListener(function () {
})
});

/**
* Return the list of Chrome tabs
* @returns {Promise<Array<chrome.tabs.Tab>>}
*/
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<number>}
*/
const getFocusedWindowId = () => {
return new Promise((resolve, reject) => {
chrome.windows.getCurrent(window => {
resolve(window.id);
});
});
}

/*
Context menu
*/
Expand Down

0 comments on commit 7d81768

Please sign in to comment.