-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
35 lines (31 loc) · 1.14 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const STRAVA_URL_PATTERN = '*://*.strava.com/*';
const STRAVA_DASHBOARD_URL = 'https://www.strava.com/dashboard';
chrome.browserAction.onClicked.addListener(() => {
chrome.tabs.query({
windowId: chrome.windows.WINDOW_ID_CURRENT,
url: STRAVA_URL_PATTERN
}, tabs => {
if (tabs && tabs.length) {
// if the active tab is on strava, reload the page instead of switching tabs
const activeTab = getActiveTab(tabs);
if (activeTab) {
return chrome.tabs.update(activeTab.id, { url: STRAVA_DASHBOARD_URL });
}
// try to find a dashboard tab first, falling back to the first strava tab
const dashboards = tabs.filter(tab => tab.url.indexOf('dashboard') >= 0);
const targetTab = dashboards.length ? dashboards[0] : tabs[0];
// switch to strava tab
chrome.tabs.highlight({
windowId: targetTab.windowId,
tabs: targetTab.index
});
} else {
// open a new strava tab
window.open(STRAVA_DASHBOARD_URL);
}
});
});
function getActiveTab(tabs) {
const activeTabs = tabs.filter(tab => tab.active);
return activeTabs && activeTabs.length && activeTabs[0];
}