-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
293 lines (260 loc) · 7.54 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Global variables
var ip; // store IP
localStorage.getItem('name') ? document.getElementById('fname').textContent=localStorage.getItem('name') : document.getElementById('fname').textContent="'name'";
//condition ? exprIfTrue : exprIfFalse
// Lat and Long
var lat;
var lon;
// Month array
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "Oct", "November", "December"];
// Counter for search
var count = 0;
// Search engine array
var commands = [
{
command: "",
label: "Google",
icon: "fa fa-google",
url: "https://www.google.com/search?q="
},
{
command: "/a",
label: "Amazon",
icon: "fa fa-amazon",
url: "https://www.amazon.co.uk/s?k="
},
{
command: "/d",
label: "DuckDuckGo",
icon: "fa fa-ban",
url: "https://duckduckgo.com/?q="
},
{
command: "/f",
label: "Facebook",
icon: "fa fa-facebook",
url: "https://www.facebook.com/search/top/?q="
},
{
command: "/t",
label: "Twitter",
icon: "fa fa-twitter",
url: "https://twitter.com/search?q="
},
{
command: "/r",
label: "Reddit",
icon: "fa fa-reddit-alien",
url: "https://www.reddit.com/search?sort=relevance&t=all&q="
},
{
command: "/w",
label: "Wikipedia",
icon: "fa fa-wikipedia-w",
url: "http://en.wikipedia.org/wiki/Special:Search/"
},
{
command: "/y",
label: "YouTube",
icon: "fa fa-youtube-play",
url: "https://www.youtube.com/results?search_query="
},
{
command: "/s",
label: "SciHub",
icon: "fa fa-key",
url: "https://scihub.to/"
}
];
// Load functions on window load
function start() {
dateTime();
// weather() called from initMap() so the user's lat and lon exist
}
window.onload = start;
$("#top").on("click", function() {
$("aside").toggleClass("hide");
});
$("footer").on("click", function() {
$("nav").toggleClass("hide");
});
// IP/VPN functions
$.getJSON("https://am.i.mullvad.net/json", function(data){
console.log(data);
if(data.mullvad_exit_ip) {
document.getElementById('vpn').textContent="connected";
}
else {
document.getElementById('vpn').textContent="disconnected";
}
});
// IP location
$.getJSON("https://ip-geolocation.whoisxmlapi.com/api/v1?apiKey=at_87UkRbwACHaX1jMHwhx6OKxUIYNNg&"+ip+"=8.8.8.8", function(data) {
console.log(data.location.postalCode);
document.getElementById('loc').textContent = data.location.city;
});
// Covid tracker test
$.getJSON("https://c19downloads.azureedge.net/downloads/msoa_data/MSOAs_latest.json", function(data) {
//console.log(data.data[1908].lad19_nm); // Coventry, Walsgrave
var corona = data.data[1908];
//document.getElementById('cc').textContent=corona.latest_7_days+" coronavirus cases in " + corona.msoa11_hclnm + ", " + corona.lad19_nm + " in the last 7 days";
document.getElementById('cc').textContent=corona.latest_7_days+" coronavirus cases in Walsgrave in the last 7 days";
//JSON.parse(data).data.find(element => element.msoa11_cd === 'E02006525'));
});
// Google maps
function initMap() {
$.getJSON("https://am.i.mullvad.net/json", function(data){ // redundant need to merge with other call
lat = data.latitude;
lon = data.longitude;
// The location of IP, needs doing dynamically using mullvad json object values
var loc = {lat: lat, lng: lon};
// The map, centered at loc
var map = new google.maps.Map(
document.getElementById('map'), {zoom: 9, center: loc, disableDefaultUI:true});
// The marker, positioned at loc, hidden with disableDefaultUI
var marker = new google.maps.Marker({position: loc, map: map});
weather();
})
};
// Show settings and aside by pressing 's' and 'a'
$(document).keydown(function(e) {
if ($(e.target).closest("input")[0]) { // if an input is being used do not trigger
return;
}
if (e.keyCode==83) $("nav").toggleClass("hide");
if (e.keyCode==65) $("aside").toggleClass("hide")
});
// Function to give correct 'st', 'nd', 'rd', 'th' for month dates
function ordinal_suffix_of(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}
// Date
function dateTime() {
var d=new Date();
ap="am";
//Hours, mins, secs
h=d.getHours();
m=d.getMinutes();
s=d.getSeconds();
// Format output wanted
if (h>11) { ap = "pm"; }
if (h>12) { h = h-12; }
if (h==0) { h = 12; }
if (m<10) { m = "0" + m; }
if (s<10) { s = "0" + s; }
//Day, month, year
t=d.getDate();
t=ordinal_suffix_of(t);
n=monthNames[d.getMonth()];
y=d.getFullYear();
document.getElementById('timest').textContent=h + ":" + m + ap;
document.getElementById('datest').textContent=t + " of " + n;
t=setTimeout(dateTime, 500);
}
// Load first search icon
window.onload($("#searchEngine").toggleClass(commands[count]["icon"]));
$("#searchEngine").on("click", function() {
// 'this' refers to the h1 being clicked, not ALL h1s
//$(this).css("color", "purple");
$("#searchEngine").removeClass(commands[count]["icon"])
count++;
if(count == commands.length) { // reset counter (loop)
count = 0;
}
$("#searchEngine").addClass(commands[count]["icon"])
console.log("Search button clicked!");
});
// Show/hide category links
$(".coll").on("click", function() {
$(this).find(".links").toggleClass("hide");
});
// Hide links on mobile
(function($) {
var $window = $(window),
$link = $('.links');
function resize() {
if ($window.width() < 641) {
return $link.addClass('hide');
}
$link.removeClass('hide');
}
$window
.resize(resize)
.trigger('resize');
})(jQuery);
// Search function
function search(e) {
if(e.keyCode == 13) { // if enter pressed
var val = document.getElementById("search-field").value;
if(commands[count]["command"] === "/s") {
val = val.replace(/^\D*/,''); // only works to truncate DOI links, will break normal strings
}
location.href = (commands[count]["url"]+val);
document.getElementById('search-field').value="";
//"AA1 2BB".split(/[0-9]/)[0];
}
}
function updateName(e) {
if(e.keyCode == 13) {
localStorage.setItem('name', document.getElementById("name-field").value); // need to set up an input to use this
document.getElementById('name-field').value="";
document.activeElement.blur(); // unfocus input after update
document.getElementById('fname').textContent=localStorage.getItem('name');
}
}
// Weather
function weather() {
var apiKey = "c632ff1ea87a9720027890a394abe1e4";
var url = "https://api.forecast.io/forecast/";
latitude = lat;
longitude = lon;
//location.innerHTML = "Latitude is " + latitude + ", Longitude is " + longitude;
$.getJSON(
url + apiKey + "/" + latitude + "," + longitude + "?callback=?",
function(data) {
temp = data.currently.temperature;
function fahrenheitCelsius(temp) {
var fsc = temp;
var cfc = ((fsc-32) * (5/9)).toFixed(2);
return cfc;
}
temp = fahrenheitCelsius(temp);
$("#temp").html(temp + "°C");
$("#minutely").html(data.minutely.summary);
//Skycons
var iconRequest = data.currently.icon;
var icons = new Skycons({'color' : '#268bd2'});
var iconList = [
"clear-day",
"clear-night",
"partly-cloudy-day",
"partly-cloudy-night",
"cloudy",
"rain",
"sleet",
"snow",
"wind",
"fog"
];
//console.log(icons);
for (i = 0; i < iconList.length; i++) {
if (iconRequest == iconList[i]) {
icons.set('icon', iconList[i]);
}
}
icons.play();
}
);
}