Files
startpage-onedark/js/main.js
deepjyoti30 f37e24e770 Added icon.
2020-05-12 22:24:45 +05:30

69 lines
2.1 KiB
JavaScript

window.onload = function() {
this.initBody()
}
searchBarId = "search-bar-input"
messageId = "message-text"
userName = "Deepjyoti"
function initBody() {
/**
* Function called when the body is loaded.
*
* Do everything like adding an event listener to
* other things.
*/
// Clear the search bar on load, just in case
document.getElementById(searchBarId).value = ""
document.getElementById(searchBarId).addEventListener("keypress", (event) => {
if (event.key != 'Enter') return
// Open google with the search results.
googleSearchUrl = "https://www.google.com/search?q="
query = document.getElementById(searchBarId).value.replace(/\ /g, "+")
document.location = googleSearchUrl + query
})
// Build a message for the user
builtMsg = buildMsg()
builtMsg == "" ?
builtMsg = `Hello ${userName}` : builtMsg = `Hey ${userName}, ${builtMsg}!`
document.getElementById(messageId).textContent = builtMsg
}
function buildMsg() {
/**
* Build a nice message for the user.
*
* Following is how the message would be decided.
* 0 - 5:59 : It's too late, take some sleep
* 6 - 8:59 : You're up early
* 9 - 11:59 : Have a good day ahead
* 12 - 16:59 : Good Afternoon
* 17 - 19:59 : Good Evening
* 20 - 23:59 : It's time to wrap up for the day
*/
date = new Date()
currentHour = date.getHours()
currentMinute = date.getMinutes()
currentTime = currentHour + (0.01 * currentMinute)
if (inRange(currentTime, 0, 5.59))
return "It's too late, take some sleep"
if (inRange(currentTime, 6, 8.59))
return "You're up early"
if (inRange(currentTime, 9, 11.59))
return "Have a good day ahead"
if (inRange(currentTime, 12, 16.59))
return "Good Afternoon"
if (inRange(currentTime, 17, 19.59))
return "Good Evening"
if (inRange(currentTime, 20, 24))
return "It's time to wrap up for the day"
else
return ""
}
function inRange(number, min, max) {
return (number >= min && number <= max)
}