-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_page.js
68 lines (56 loc) · 1.83 KB
/
options_page.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
const onSubmit = e => {
e.preventDefault();
const user = document.getElementById('lastfm-user').value;
lastFm.getUserInfo(user, setUser, userValidationFailed);
};
const userValidationFailed = error => {
chrome.notifications.create("failed_validation", {
iconUrl: 'res/last-fm.png',
type: 'basic',
message: `${error.message} (code ${error.error})`,
title: 'Failed to set user'
});
};
const setUser = user => {
chrome.storage.sync.set({lastFmUser: user.name}, function() {
fillUserData(user);
chrome.notifications.create("user_set", {
iconUrl: 'res/last-fm.png',
type: 'basic',
message: `Last.fm user set to ${user.name}`,
title: 'User set!'
});
});
};
const loadCurrentUser = () => {
chrome.storage.sync.get(['lastFmUser'], function(result) {
if (!result.lastFmUser) {
userDataError('No Last.fm user set');
return;
}
lastFm.getUserInfo(result.lastFmUser, fillUserData, () => userDataError("Could not get user data. Maybe last.fm is down."));
});
};
const fillUserData = userData => {
const container = document.getElementById('userData');
console.log(userData);
container.innerHTML = '';
container.classList.remove('error');
const a = document.createElement('a');
a.setAttribute('href', userData.url);
a.setAttribute('target', '_blank');
const img = document.createElement('img');
img.setAttribute('src', userData.image[1]['#text']);
a.appendChild(img);
a.appendChild(document.createTextNode(userData.name));
container.appendChild(a);
};
const userDataError = message => {
document.getElementById('userData').innerHTML = message;
document.getElementById('userData').classList.add('error');
};
window.onload = () => {
loadCurrentUser();
const form = document.getElementById('settings-form');
form.addEventListener("submit", onSubmit);
};