Make it bookmarkable

This commit is contained in:
Saugat Acharya 2017-06-15 23:29:03 +05:45
parent e4575d098c
commit 714c5c92df
2 changed files with 27 additions and 2 deletions

View File

@ -28,7 +28,7 @@
<div class="form-group">
<form id="form" role="form">
<div class="input-group">
<input id="repo" class="form-control" type="text" placeholder="techgaun/github-dorks">
<input id="q" name="q" class="form-control" type="text" placeholder="techgaun/github-dorks">
<span class="input-group-btn">
<button onClick="fetchData()" type="button" class="btn btn-primary">Find</button>
</span>

View File

@ -1,12 +1,22 @@
window.addEventListener('load', (e) => {
const repo = getQueryParams().q;
if (repo) {
document.getElementById('q').value = repo;
fetchData()
}
});
document.getElementById('form').addEventListener('submit', (e) => {
e.preventDefault()
fetchData()
})
function fetchData() {
const repo = document.getElementById('repo').value
const repo = document.getElementById('q').value
const re = /[-_\w]+\/[-_.\w]+/
window.history.pushState('', '', `?q=${repo}`);
if (re.test(repo)) {
fetchAndShow(repo)
} else {
@ -71,6 +81,21 @@ function showData(data) {
`
}
function getQueryParams() {
let query = location.search;
if (!query) {
return { };
}
return (/^[?#]/.test(query) ? query.slice(1) : query)
.split('&')
.reduce((params, param) => {
let [ key, value ] = param.split('=');
params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : '';
return params;
}, { });
};
function timeSince(date_str) {
const date = new Date(date_str)
const seconds = Math.floor((new Date() - date) / 1000);