-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
49 lines (47 loc) · 1.62 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
chrome.omnibox.onInputChanged.addListener(function (query, suggest) {
var clientId = '70dbe4d49232b596d30fb6c341646830';
var baseUrl = 'https://api.soundcloud.com/';
var url;
if (query.charAt(0) === '/') {
url = baseUrl + 'users.json?client_id=' + clientId + '&q=' + encodeURIComponent(query.substr(1));
fetch(url).then(function (response) {
response.json().then(function (data) {
suggest(data.map(function (user) {
var fullname;
if (user.full_name === '') { fullname = ''; }
else { fullname = '(' + user.full_name + ')'; }
return {
content: user.permalink_url,
description: user.username + ' ' + fullname
};
}));
});
});
} else {
url = baseUrl + 'tracks.json?client_id=' + clientId + '&q=' + encodeURIComponent(query);
fetch(url).then(function (response) {
response.json().then(function (data) {
suggest(data.map(function (track) {
return {
content: track.permalink_url,
description: track.title + ' by ' + track.user.username
};
}));
});
});
}
});
chrome.omnibox.onInputEntered.addListener(function (text) {
chrome.tabs.getSelected(null, function (tab) {
var url;
if ((text.substr(0, 8) === 'https://') || (text.substr(0, 7) === 'http://')) {
url = text;
} else if (text.charAt(0) === '/') {
text = text.substr(1);
url = 'https://soundcloud.com/search?q=' + text;
} else {
url = 'https://soundcloud.com/search?q=' + text;
}
chrome.tabs.update(tab.id, {url: url});
});
});