-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
91 lines (74 loc) · 2.52 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
var memory;
chrome.omnibox.onInputChanged.addListener(function(text, suggest){
var baseUrl = "http://ws.spotify.com/search/1/artist.json?q="+text;
var finalResult = [];
var store = {};
$.ajax({
url : baseUrl,
dataType : "json",
type: 'GET',
async: true,
success: function(result){
// console.log(result);
var artists = result['artists'];
for (var i = 0; i < artists.length && i < 5; i += 1){
var artist = artists[i];
if(artist.href){
var href_split = artist.href.split(":"),
href = "http://play.spotify.com/artist/" + href_split[2],
obj = {},
artist = artist.name.replace("&", "and");
obj.artist = artist;
obj.url = href;
obj.description = "<match>" +
artist +
"</match>" +
"<dim> - </dim>" +
" <url>" +
href +
"</url>";
finalResult.push({
content : obj.artist,
description : obj.description
});
store[obj.artist.replace(/ /g, "-")] = obj;
// temp store results in memory
}
}
memory = store;
suggest(finalResult);
}
});
});
// This event is fired with the user accepts the input in the omnibox.
chrome.omnibox.onInputEntered.addListener(function(text) {
var obj = memory[text.replace(/ /g, "-")];
$.ajax({url:'http://ws.spotify.com/search/1/track?q='+obj.artist,
dataType:'json',
type:'GET',
async:true,
success:function(result){
var finalResult = [],
tracks = result.tracks;
tracks.sort(function(a, b){
if (a.popularity > b.popularity){
return -1;
} else {
return 1;
}
return 1;
});
$.each(tracks, function(i, k){
var popularity = k.popularity;
var href = k.href.split(':');
finalResult.push({popularity:popularity, url:href[2]});
});
var first_url = 'https://play.spotify.com/track/'+finalResult[0].url;
console.log(first_url);
// chrome.omnibox.onInputEntered.addListener(function(text, suggest){
// suggest()
// });
chrome.tabs.update({ url: first_url });
}});
//loop through results find selected then push to local storage
});