-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
150 lines (125 loc) · 3.98 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var tracklist
var tracklist_name
var tracklist_date
var get_url
var client_id
var client_secret
// Button clicked
function runScript() {
// Scan for spotify links
chrome.tabs.executeScript({
file: "/bbc2spotify.js"
})
// Base URL
get_url = "https://accounts.spotify.com/authorize?"
get_url += `client_id=${client_id}&`
get_url += "response_type=code&"
get_url += `redirect_uri=${encodeURIComponent(chrome.identity.getRedirectURL())}&`
get_url += "scope=playlist-modify-public"
}
chrome.runtime.onMessage.addListener(
// On playlist scan finish
function(request, sender, sendResponse) {
if (request.type == "tracklist"){
sendResponse({farewell: "recieved tracklist"});
tracklist = request.track_list
tracklist_name = request.name
tracklist_date = request.date
// Authenticate and make playlist
chrome.identity.launchWebAuthFlow(
{
url: get_url,
interactive: true,
}, authenticateSpotify)
}
// Recieve API details
if (request.type == "api_keys"){
sendResponse({farewell: "recieved api_keys"});
client_id = request.client_id
client_secret = request.client_secret
// Start request
runScript();
}
}
);
// Recieve api details
chrome.runtime.onMessage.add
function authenticateSpotify(response){
// Get parameters from response
var urlParams = new URLSearchParams(response.replace(chrome.identity.getRedirectURL(), ''))
// POST request for access token
var authRequest = new Request("https://accounts.spotify.com/api/token",
{
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: new URLSearchParams(
{
'grant_type': 'authorization_code',
'code': urlParams.get("code"),
'redirect_uri': chrome.identity.getRedirectURL(),
'client_id': `${client_id}`,
'client_secret': `${client_secret}`
})
})
// Create playlist
fetch(authRequest).then(function(response){
response.json().then(function(json){
createPlaylist(json.access_token)
})
})
}
async function createPlaylist(access_token){
id = await getSpotifyID(access_token)
var trackURIS = []
// Add create Spotify URIs from track links
for (var track of Object.values(tracklist)){
trackURIS.push(`"${track.replace("https://open.spotify.com/track/", "spotify:track:")}"`)
}
// Create new playlist
var request = new Request(`https://api.spotify.com/v1/users/${id}/playlists`,
{
method: "POST",
headers: {
"Authorization": `Bearer ${access_token}`,
"Content-Type": "application/json"
},
body:
`{
"name": "${tracklist_name} | ${tracklist_date}",
"description": "Made using Sounds2Spotify"
}`
})
var response = await fetch(request)
const responseJSON = await response.json()
const playlistID = await responseJSON.id
// Add tracklist songs to newly created playlist
var request = new Request(`https://api.spotify.com/v1/playlists/${playlistID}/tracks`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${access_token}`
},
body:
`{
"uris": [${trackURIS}]
}`
})
var response = await fetch(request)
// Open tab to new playlist
chrome.tabs.create({ url: `https://open.spotify.com/playlist/${playlistID}`})
}
async function getSpotifyID(access_token){
var userRequest = new Request("https://api.spotify.com/v1/me",
{
method: "GET",
headers: {
Authorization: `Bearer ${access_token}`
}
})
const response = await fetch(userRequest)
const json = await response.json()
return json.id
}