-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchrome.js
34 lines (29 loc) · 935 Bytes
/
chrome.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
/* global chrome */
import * as spotifyLink from './spotify-link.js'
const urls = {
urls: spotifyLink.domains.map(d => `*://${d}/*`)
}
chrome.webRequest.onBeforeRequest.addListener(function (details) {
if (spotifyLink.leaveAlone(details.url)) return
const spotifyUri = spotifyLink.createDesktop(details.url)
if (!spotifyUri) return
console.log('++++')
console.log(details)
console.log('++++')
// If the current tab url is the same as the one initiating this
// request it means this is a newly opened tab, so it is safe to
// close the tab after Spotify has been opened.
if (details.tabId >= 0) {
chrome.tabs.get(details.tabId, function (tab) {
console.log('going in!')
if (tab.url === details.url) {
setTimeout(function () {
chrome.tabs.remove(details.tabId)
}, 100)
}
})
}
return {
redirectUrl: `spotify:${spotifyUri}`
}
}, urls, ['blocking'])