-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
247 lines (227 loc) · 6.69 KB
/
script.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const defaults = {
style: {
background:
"https://cdn.glitch.com/fbcc75ee-28e3-462b-9d78-8dd9e7264ccd%2Ffirewatch-nature-m1-1920x1080.jpeg",
foreground: "#FFFFFF",
circular: false,
css: "",
},
modules: {
time: {
show: true,
"24hour": true,
ampm: false,
},
weather: {
show: true,
units: "metric",
},
search: {
show: true,
engine: "https://www.google.com/search",
placeholder: false,
},
bookmarks: {
show: true,
items: [
"https://google.com",
"https://youtube.com",
"https://mail.google.com",
],
},
},
settings: {
timezone: false,
},
};
chrome.storage.sync.get(defaults, (config) => {
const body = document.querySelector("body");
const style = document.querySelector("style");
const time = document.getElementById("time");
const weather = document.getElementById("weather");
const icon = document.getElementById("icon");
const type = document.getElementById("type");
const temp = document.getElementById("temp");
const search = document.getElementById("search");
const input = search.elements.q;
const bookmarks = document.getElementById("bookmarks");
// Style
body.style.background = `url("${
config.style.background ===
"https://cdn.glitch.com/fbcc75ee-28e3-462b-9d78-8dd9e7264ccd%2Ffirewatch-nature-m1-1920x1080.jpeg"
? "/bg.jpeg"
: config.style.background
}") no-repeat center fixed`;
body.style.backgroundSize = `cover`;
body.style.backgroundPosition = `center`;
body.style.height = `100%`;
const booka = convertToRGB(config.style.foreground);
const bookb = convertToRGB(config.style.foreground);
booka.push(0.15);
bookb.push(0.2);
style.innerHTML =
`:root { --color: ${config.style.foreground}; --booka: rgba(${booka.join(
", "
)}); bookb: rgba(${bookb.join(", ")}); }` + config.style.css;
document
.getElementById("point")
.setAttribute("stroke", config.style.foreground);
if (config.style.circular === true) {
search.setAttribute("class", "circle");
bookmarks.setAttribute("class", "circle");
}
// Time
if (config.modules.time.show === true) {
time.style.display = "";
var options = config.modules.time["24hour"]
? { hour: "numeric", minute: "numeric", hour12: false }
: {
hour: "numeric",
minute: "numeric",
hour12: true,
};
if (config.settings.timezone !== false)
options.timeZone = config.settings.timezone;
function loadTime() {
var newTime = config.modules.time.ampm
? new Date().toLocaleString("en-GB", options)
: new Date()
.toLocaleString("en-GB", options)
.replace(/am|pm| /gi, "")
if (newTime.indexOf("0:") === 0) {
time.innerText = newTime.replace("0:", "12:");
} else {
time.innerText = newTime;
}
}
loadTime();
setInterval(loadTime, 1000);
}
// Weather
if (config.modules.weather.show === true) {
weather.style.display = "";
function loadWeather() {
navigator.geolocation.getCurrentPosition(
function (location) {
var units = config.modules.weather.units;
fetch(
`https://api.openweathermap.org/data/2.5/weather?lang=EN&lat=${location.coords.latitude}&lon=${location.coords.longitude}&units=${units}&appid=8e586fc94a1f3326672f6733aa38fd55`
)
.then((response) => response.json())
.then((weatherData) => {
icon.setAttribute(
"class",
"wi wi-owm-" + weatherData.weather[0].id
);
type.innerText = weatherData.weather[0].main;
temp.innerText = Math.round(weatherData.main.temp) + "°";
});
},
function (error) { console.log(error) }
);
}
loadWeather()
setInterval(loadWeather, 300000);
}
// Search
if (config.modules.search.show === true) {
search.style.display = "";
const placeholders = [
"What is my IP?",
"What is the time?",
"What is the weather?",
"How do I vote?",
"How do I tie a tie?",
"What song is this?",
"When is mother's day?",
"How to take a screenshot.",
"How do I make money?",
"How do I spell?",
"How do I make French Toast?",
"When is Labor Day?",
"Why is the sky blue?",
"What does 'lol' mean?",
"What is a verb?",
"Why am I bored?",
"How do I boil an egg?",
"What should I watch?",
"What is global warming?",
"What is a meme?",
"Who unfollowed me?",
"How do I learn English?",
"How do I code?",
"Who invented the internet?",
"How do I make guacamole?",
"How do I French Braid?",
"How do I make cookies?",
"How old is Selena Gomez?",
"How much water should I drink?",
"How do I clear cache?",
"How do I draw?",
"How to write a book?",
"How to cook bacon?",
"Why do we yawn?",
"How do I sell on eBay?",
"How do I make Mac 'n' Cheese?",
"Who invented the light bulb?",
"What does 🙌 mean?",
"How do I tie a scarf?",
"When are the Oscars?",
"What is a computer?",
"How to play UNO?",
"What is a CV?",
"How do I install Windows?",
"Can you see me?",
"Hint: hover on the bottom right",
];
const placeholder =
placeholders[Math.floor(Math.random() * placeholders.length)];
search.setAttribute("action", config.modules.search.engine);
if (config.modules.search.placeholder !== false) {
input.setAttribute("placeholder", config.modules.search.placeholder);
} else {
input.setAttribute("placeholder", placeholder);
}
}
// Bookmarks
if (
config.modules.bookmarks.show === true &&
config.modules.bookmarks.items != 0
) {
bookmarks.style.display = "";
config.modules.bookmarks.items.forEach((bookmark) => {
bookmarks.innerHTML += `
<a href="${bookmark}">
<img
src="https://favicon.splitbee.io/?url=${bookmark}"
width="22px"
height="22px"
/>
</a>
`;
});
}
});
// Convert to RGB
function convertToRGB(hex) {
hex = hex.replace(/#/g, "");
if (hex.length === 3) {
hex = hex
.split("")
.map(function (hex) {
return hex + hex;
})
.join("");
}
// validate hex format
var result = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})[\da-z]{0,0}$/i.exec(hex);
if (result) {
var red = parseInt(result[1], 16);
var green = parseInt(result[2], 16);
var blue = parseInt(result[3], 16);
return [red, green, blue];
} else {
// invalid color
return null;
}
}