-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
48 lines (47 loc) · 3.13 KB
/
popup.js
File metadata and controls
48 lines (47 loc) · 3.13 KB
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
function startSpotifyOAuth() {
var client_id = '33cef6191c9941c9b256df2c986192c8';
var client_secret = '556031aed9c741a887bdeaf02a95b357';
var redirectUri = chrome.identity.getRedirectURL("spotify") ;
chrome.identity.launchWebAuthFlow({
"url": "https://accounts.spotify.com/authorize?client_id="+client_id+
"&redirect_uri="+ encodeURIComponent(redirectUri) +
"&response_type=code&scope=app-remote-control%20playlist-read-private%20playlist-modify-private%20playlist-modify-public%20user-library-read%20playlist-modify%20user-modify-playback-state",
'interactive': true,
},
function(redirect_url)
{
console.log(redirect_url);
var parsedParameters = {}
var queryString = redirect_url.split( "?" )[1];
var queryParameters = queryString.split("&");
for (var parameterNumber in queryParameters) {
var parameter = queryParameters[parameterNumber];
var name = parameter.split( "=" )[0];
var value = decodeURIComponent( parameter.split( "=" )[1] );
parsedParameters[name] = value;
}
var code = parsedParameters['code'];
var req = new XMLHttpRequest();
req.open("POST", "https://accounts.spotify.com/api/token", true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.setRequestHeader("Authorization", "Basic " + btoa( client_id + ":" + client_secret ));
req.onreadystatechange = function() {//Call a function when the state changes.
if(this.readyState == XMLHttpRequest.DONE && this.status == 200) {
var value = JSON.parse(this.response);
value['start_time'] = new Date().getTime() / 1000;
chrome.storage.local.set( {'spotify_auth':value}, function() {
console.log( 'spotify_auth set to ' );
console.log( value );
});
}
}
req.send("grant_type=authorization_code&code="+ encodeURIComponent(code) + "&redirect_uri=" + encodeURIComponent(redirectUri));
});
}
if( document.getElementById("clickme") ) {
document.getElementById("clickme").addEventListener("click", function(){
console.log( 'launching' );
startSpotifyOAuth();
});
console.log( "adding listener" );
}