-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover.js
54 lines (42 loc) · 1.6 KB
/
cover.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
/**
* @author Philip Laine <philip.laine@gmail.com>
* @author Niklas Tegnander <niklas@youpic.com>
*/
(function (window, document, undefined) {
var updateTab = function (data) {
var bg = document.querySelector('.bg-photo');
bg.style.backgroundImage = 'url(' + data.image + ')';
var author_link = document.querySelector('.author a');
author_link.href = data.user_link;
author_link.innerHTML = data.display_name + '<span class="profile"></span>'
var profile = document.querySelector('.profile');
profile.style.backgroundImage = 'url(' + data.profile_image + ')';
};
// Fetch from cache
chrome.storage.sync.get('data', function (result) {
if (!result.data) return;
updateTab(result.data);
});
var url = 'https://api.youpic.com/web_image', xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var result = JSON.parse(xhr.responseText);
if (result && result.image_urls && result.user) {
var profile_image = (result.user && result.user.profile_image_urls
&& result.user.profile_image_urls.small)
|| 'https://youpic.com/images/avatar/avatar-32.png';
var data = {
user_link: 'https://youpic.com/' + result.user.username,
display_name: result.user.display_name,
image: result.image_urls.huge,
profile_image: profile_image
}
updateTab(data); // Display image
// Update cache
chrome.storage.sync.set({ 'data': data }); // Omit callback
}
}
};
xhr.open('GET', url, true);
xhr.send();
})(window, document);