Created
October 3, 2016 13:32
-
-
Save e-minguez/783095e6b63b7e1546d4ece549ffb6e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<meta name="robots" content="noindex"> | |
<title>~</title> | |
<script>WebFontConfig={google:{families:['Lato:300:latin']}};</script> | |
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js" async></script> | |
<style type="text/css"> | |
body { | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
margin: 0; | |
padding: 0; | |
color: #222; | |
font-family: 'Lato', sans-serif; | |
font-weight: 300; | |
} | |
main { | |
position: absolute; | |
top: 50%; | |
right: 0; | |
left: 0; | |
width: 90%; | |
max-width: 310px; | |
margin: 0 auto; | |
transform: translateY(-100px); | |
text-align: center; | |
} | |
time { | |
display: block; | |
margin-bottom: 20px; | |
font-size: 5rem; | |
letter-spacing: 6px; | |
} | |
input, | |
input:focus { | |
margin: 0; | |
border: 0; | |
outline: 0; | |
-webkit-appearance: none; | |
-moz-appearance: none; | |
} | |
input { | |
box-sizing: border-box; | |
width: 100%; | |
padding: 12px; | |
transition: 0.5s; | |
border-radius: 2px; | |
background: #222; | |
color: #fff; | |
font-family: 'Lato', sans-serif; | |
font-size: 1.1rem; | |
} | |
ul, | |
li { | |
margin: 0; | |
padding: 0; | |
list-style: none; | |
} | |
.overlay { | |
position: fixed; | |
display: flex; | |
justify-content: center; | |
box-sizing: border-box; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
transition: 200ms; | |
background: #222; | |
visibility: hidden; | |
opacity: 0; | |
z-index: 1; | |
} | |
.overlay[data-toggled='true'] { | |
visibility: visible; | |
opacity: 1; | |
} | |
.lists { | |
display: flex; | |
justify-content: center; | |
flex-wrap: wrap; | |
padding-bottom: 20px; | |
overflow: auto; | |
} | |
.category { | |
width: 160px; | |
margin: 20px 10px 0; | |
} | |
.category-name { | |
margin: 0; | |
padding: 14px; | |
border-radius: 2px 2px 0 0; | |
background: #30a388; | |
color: #165b4b; | |
font-size: .7rem; | |
text-transform: uppercase; | |
text-align: center; | |
} | |
.command { | |
transition: background 200ms; | |
background: #333; | |
} | |
.command:hover { | |
background: #2f2f2f; | |
} | |
.command:nth-child(even) { | |
background: #3a3a3a; | |
} | |
.command:nth-child(even):hover { | |
background: #393939; | |
} | |
.command:last-of-type { | |
border-radius: 0 0 2px 2px; | |
} | |
.command a { | |
display: block; | |
padding: 14px 12px; | |
color: #eee; | |
font-size: .8rem; | |
line-height: 1.3rem; | |
text-decoration: none; | |
} | |
.command-key { | |
display: block; | |
width: 32px; | |
margin-right: 10px; | |
float: left; | |
border-radius: 2px; | |
background: #292929; | |
font-family: 'Courier New', monospace; | |
text-align: center; | |
} | |
@media (min-width: 740px) { | |
.overlay { | |
align-items: center; | |
} | |
} | |
</style> | |
<main> | |
<time id="js-clock"></time> | |
<form id="js-search-form" autocomplete="off"> | |
<input id="js-search-input" type="text" autofocus> | |
</form> | |
</main> | |
<aside id="js-overlay" class="overlay"> | |
<ul id="js-lists" class="lists"></ul> | |
</aside> | |
<script> | |
'use strict'; | |
var config = { | |
categories: [ | |
{ name: "Tools", commands: [ | |
{ key: 'd', name: 'Drive', url: 'https://drive.google.com/drive', search: '/search?q=' }, | |
{ key: 'g', name: 'GitHub', url: 'https://github.com', search: '/search?q=' }, | |
{ key: 'k', name: 'Keep', url: 'https://keep.google.com', search: '/#search/text=' }, | |
] }, | |
{ name: "News", commands: [ | |
{ key: 'h', name: 'Hacker News', url: 'https://hn.algolia.com', search: '/?query=' }, | |
{ key: 'r', name: 'Reddit', url: 'https://www.reddit.com', search: '/search?q=' }, | |
{ key: 'm', name: 'Meneame', url: 'https://www.meneame.net/m/emnm/', search: '/search?q=' }, | |
{ key: 'l', name: 'Feedly', url: 'http://feedly.com/i/latest', search: '/search/' }, | |
] }, | |
{ name: "Social", commands: [ | |
{ key: 'i', name: 'Inbox', url: 'https://inbox.google.com', search: '/search/' }, | |
{ key: 'f', name: 'Facebook', url: 'https://www.facebook.com', search: '/search/top/?q=' }, | |
{ key: 't', name: 'Tweetdeck', url: 'https://tweetdeck.twitter.com/' }, | |
{ key: 'w', name: 'WhatsApp', url: 'https://web.whatsapp.com/' }, | |
{ key: 'c', name: 'Forocoches', url: 'https://www.forocoches.com/foro/', search: '/search.php?q=' }, | |
{ key: 'e', name: 'EOL', url: 'https://www.elotrolado.net', search: '/search.php?type=h&keywords=' }, | |
] }, | |
{ name: "Media", commands: [ | |
{ key: 's', name: 'Spotify', url: 'https://play.spotify.com', search: '/search/' }, | |
{ key: 'y', name: 'YouTube', url: 'https://www.youtube.com', search: '/weather?search_query=' }, | |
] }, | |
{ name: "Shopping", commands: [ | |
{ key: 'a', name: 'Amazon', url: 'https://www.amazon.com', search: '/s/?field-keywords=' }, | |
] }, | |
], | |
// if none of the keys are matched, this is used for searching | |
defaultSearch: 'https://www.google.com/search?q=', | |
// the delimiter between the key and your search query | |
// e.g. to search GitHub for potatoes you'd type "g:potatoes" | |
searchDelimiter: ':' | |
}; | |
function $(s) { | |
return document.querySelector(s); | |
}; | |
var Clock = (function() { | |
var clock = $('#js-clock'); | |
function pad(num) { | |
return ('0' + num.toString()).slice(-2); | |
} | |
function setTime() { | |
var date = new Date(); | |
clock.innerHTML = pad(date.getHours()) + ' ' + pad(date.getMinutes()); | |
} | |
setTime(); | |
setInterval(setTime, 1000); | |
})(); | |
var Help = (function(config) { | |
var charRegex = /[a-zA-Z0-9-_ ]/; | |
var head = $('head'); | |
var overlay = $('#js-overlay'); | |
var lists = $('#js-lists'); | |
var categoryItems = ''; | |
config.categories.forEach(function(category) { | |
var commandItems = ''; | |
category.commands.forEach(function(command) { | |
var prerenderLink = '<link rel="prerender" href="' + command.url + '">'; | |
head.insertAdjacentHTML('beforeend', prerenderLink); | |
commandItems += ( | |
'<li class="command">' + | |
'<a href="' + command.url + '">' + | |
'<span class="command-key">' + command.key + '</span>' + | |
'<span class="command-name">' + command.name + '</span>' + | |
'</a>' + | |
'</li>' | |
); | |
}); | |
lists.insertAdjacentHTML( | |
'beforeend', | |
'<li class="category">' + | |
'<h2 class="category-name">' + category.name + '</h2>' + | |
'<ul>' + commandItems + '</ul>' + | |
'</li>' | |
); | |
}); | |
document.addEventListener('keydown', function(event) { | |
var isEscape = event.keyCode === 27; | |
var isAlphaNum = charRegex.test(String.fromCharCode(event.keyCode)); | |
if (isEscape || isAlphaNum) { | |
overlay.removeAttribute('data-toggled'); | |
Form.focus(); | |
} | |
}); | |
return { | |
toggle: function() { | |
var toggle = overlay.getAttribute('data-toggled') !== 'true'; | |
overlay.setAttribute('data-toggled', toggle); | |
} | |
}; | |
})(config); | |
var Form = (function(config) { | |
var urlRegex = /(\b(https?|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i; | |
var searchForm = $('#js-search-form'); | |
var searchInput = $('#js-search-input'); | |
searchForm.addEventListener('submit', function(event) { | |
event.preventDefault(); | |
var q = searchInput.value.trim(); | |
if (q === '' || q === '?') { | |
Help.toggle(); | |
searchInput.value = ''; | |
} else { | |
var qSplit = q.split(config.searchDelimiter); | |
var qIsUrl = q.match(new RegExp(urlRegex)); | |
var redirect = ''; | |
if (qIsUrl) redirect = q; | |
else redirect = config.defaultSearch + encodeURIComponent(q); | |
config.categories.forEach(function(category) { | |
category.commands.forEach(function(command) { | |
if (qSplit[0] === command.key) { | |
if (qSplit[1] && command.search) { | |
qSplit.shift(); | |
var search = encodeURIComponent(qSplit[0].trim()); | |
redirect = command.url + command.search + search; | |
} else { | |
redirect = command.url; | |
} | |
} | |
}); | |
}); | |
window.location.href = redirect; | |
} | |
}, false); | |
return { | |
focus: function() { | |
searchInput.focus(); | |
} | |
}; | |
})(config); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment