-
Notifications
You must be signed in to change notification settings - Fork 0
/
mua_tab.js
145 lines (120 loc) · 4.29 KB
/
mua_tab.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
(function (){
let background = document.getElementById("background");
let redditWallpaper = false;
function setBackground (backgroundImage) {
background.appendChild(backgroundImage);
}
function onBackgroundImageResponse (background) {
if(redditWallpaper){
let responseJson = JSON.parse(this.responseText).data.children;
let randomIndex = Math.random() * responseJson.length | 0;
let randomImage = responseJson[randomIndex].data;
let backgroundImage = createImage(randomImage.url)
setBackground(backgroundImage);
}else{
let responseJson = JSON.parse(this.responseText);
let randomIndex = Math.random() * responseJson.length | 0;
let randomImage = responseJson[randomIndex];
let backgroundImage = createImage(randomImage.url)
setBackground(backgroundImage);
}
}
function createImage (url) {
let img = document.createElement("img");
img.setAttribute("src", url);
img.setAttribute("alt", "chrome-mua-tab-background");
img.className = "bg-img" ;
return img;
}
var backgroundImageReq = new XMLHttpRequest();
backgroundImageReq.addEventListener("load", onBackgroundImageResponse);
chrome.storage.sync.get('reddit-wallpaper', function (value) {
redditWallpaper = value["reddit-wallpaper"];
if(redditWallpaper){
backgroundImageReq.open("GET", "https://www.reddit.com/r/wallpaper/top.json");
}else{
backgroundImageReq.open("GET", "https://raw.githubusercontent.com/maifeeulasad/chrome-mua-tab/data-source/data.json");
}
backgroundImageReq.send();
});
})();
(function () {
let timeElem = document.getElementById("time");
let dateElem = document.getElementById("date");
function getTime() {
let today = new Date();
let hour = today.getHours();
hour = hour.toString().length === 1 ? ("0" + hour) : hour;
let minutes = today.getMinutes();
minutes = minutes.toString().length === 1 ? ("0" + minutes) : minutes;
let seconds = today.getSeconds();
seconds = seconds.toString().length === 1 ? ("0" + seconds) : seconds;
let time = hour + " : " + minutes + " : " + seconds;
let day = today.getUTCDate();
let dayOfWeek = today.getUTCDay();
if(-today.getTimezoneOffset() > hour * 60 + minutes){
day += 1;
dayOfWeek += 1;
}
day = day.toString().length === 1 ? ("0" + day) : day;
let month = today.getUTCMonth() + 1;
month = month.toString().length === 1 ? ("0" + month) : month;
let daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
let date = daysOfWeek[dayOfWeek] + " , " + day + " / " + month;
timeElem.innerHTML = time;
dateElem.innerHTML = date;
setTimeout(function () {
getTime()
}, 1000*1);
}
getTime();
})();
(function (){
let batteryElem = document.getElementById("battery");
let batteryLabelElem = document.getElementById("battery-label");
function getBattery() {
navigator
.getBattery()
.then(battery => {
let batLevelInt = Math.round(battery.level * 100)
batteryElem.style.height = batLevelInt + "%";
batteryLabelElem.setAttribute("title",batLevelInt + "%");
if(batLevelInt<20){
batteryElem.classList.add("alert");
}else if(batLevelInt<40){
batteryElem.classList.add("warn");
}
})
setTimeout(function () {
getBattery()
}, 1000*60*2);
}
getBattery();
})();
(function (){
let quoteElem = document.getElementById("quote");
function setRandomQuote(quote){
quoteElem.innerHTML = quote;
}
function onQuotesResponse (quotesData) {
let responseJson = JSON.parse(this.responseText);
responseJson = responseJson.data.children;
let randomIndex = Math.random() * responseJson.length | 0;
let randomQuote = responseJson[randomIndex];
let quote = randomQuote.data.title;
setRandomQuote(quote);
}
var quotesReq = new XMLHttpRequest();
quotesReq.addEventListener("load", onQuotesResponse);
quotesReq.open("GET", "https://www.reddit.com/r/quotes/top.json");
quotesReq.send();
})();
(function (){
let checkRedditWallpaper = document.getElementById("input-check-reddit-wallpaper");
chrome.storage.sync.get('reddit-wallpaper', function (value) {
checkRedditWallpaper.checked = value["reddit-wallpaper"];
});
checkRedditWallpaper.onclick = () => {
chrome.storage.sync.set({'reddit-wallpaper': checkRedditWallpaper.checked})
}
})();