-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
117 lines (104 loc) · 3.72 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
const RESPONSE_TYPE = encodeURIComponent('token');
const REDIRECT_URI = encodeURIComponent('https://' + EXTENSION_ID + '.chromiumapp.org/');
// The search API require the `user-read-private` scope, see https://developer.spotify.com/documentation/general/guides/scopes/
const SCOPE = encodeURIComponent('user-read-private');
const SHOW_DIALOG = encodeURIComponent('true');
let STATE = '';
let ACCESS_TOKEN = localStorage['spotify_access_token'];
let isUserSignedIn = function () {
return localStorage['spotify_signed_in'] == true;
};
var createSpotifyEndpoint = function() {
STATE = encodeURIComponent('meet' + Math.random().toString(36).substring(2, 15));
let oauth2_url =
`https://accounts.spotify.com/authorize
?client_id=${CLIENT_ID}
&response_type=${RESPONSE_TYPE}
&redirect_uri=${REDIRECT_URI}
&state=${STATE}
&scope=${SCOPE}
&show_dialog=${SHOW_DIALOG}
`;
return oauth2_url;
};
var queryAlbum = function(title, cb) {
$.ajax({url:qpath,
crossDomain:true,
headers: {"Authorization": "Bearer " + ACCESS_TOKEN},
data:{q: data},
success:function (ret) {
cb(ret.albums);
}
});
};
var saveLoginInfo = function(access_token) {
localStorage['spotify_signed_in'] = true;
localStorage['spotify_access_token'] = access_token;
};
var deleteLoginInfo = function () {
localStorage['spotify_signed_in'] = false;
localStorage['spotify_access_token'] = '';
};
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'login') {
if (isUserSignedIn()) {
console.log("User is already signed in.");
} else {
console.log('User signing in');
// sign the user in with Spotify
chrome.identity.launchWebAuthFlow({
url: createSpotifyEndpoint(),
interactive: true
}, function (redirect_url) {
if (chrome.runtime.lastError) {
console.log('Chrome runtime error', chrome.runtime.lastError.message);
sendResponse({ message: 'fail' });
} else {
if (redirect_url.includes('callback?error=access_denied')) {
sendResponse({ message: 'fail' });
} else {
ACCESS_TOKEN = redirect_url.substring(redirect_url.indexOf('access_token=') + 13);
ACCESS_TOKEN = ACCESS_TOKEN.substring(0, ACCESS_TOKEN.indexOf('&'));
let state = redirect_url.substring(redirect_url.indexOf('state=') + 6);
if (state === STATE) {
console.log("SUCCESS")
saveLoginInfo(ACCESS_TOKEN);
chrome.browserAction.setPopup({ popup: './popup.html' }, () => {
sendResponse({ message: 'success' });
});
} else {
sendResponse({ message: 'fail' });
}
}
}
});
}
return true;
}
});
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "getLocalStorage") {
sendResponse({data: localStorage[request.key]});
} else if (request.method == "queryAlbums") {
console.log('queryAlbums: ' + request.title);
var qpath = 'https://api.spotify.com/v1/search?type=album';
$.ajax({url:qpath,
crossDomain:true,
headers: {"Authorization": "Bearer " + ACCESS_TOKEN},
data:{q: request.title},
success: function (ret) {
sendResponse({
albums: ret.albums,
isOpenSpotifyDirect: localStorage['isOpenSpotifyDirect']});
},
error: function (ret) {
if (ret.status == 401) {
// Token not valid
deleteLoginInfo();
}
},
});
} else {
sendResponse({}); // snub them.
}
});