Skip to content

Instantly share code, notes, and snippets.

@DrI-T
Last active April 22, 2021 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrI-T/d8115fd6f32c1c56d17f7b869221bd10 to your computer and use it in GitHub Desktop.
Save DrI-T/d8115fd6f32c1c56d17f7b869221bd10 to your computer and use it in GitHub Desktop.
Your location is ...
var cached;
get_loc()
.then( json => {
el = document.getElementsByTagName('div')[0];
el.innerHTML = el.innerHTML.replace(':location',`${json.city}, ${json.region}`)
el.innerHTML = el.innerHTML.replace(':ip',json.ip)
})
function get_loc() {
let url = 'https://ipinfo.io/json'
return cached_fetch(url ,{ mode:'cors'}).catch(console.warn)
}
function cached_fetch(url,options) {
let tics = Date.now();
cached = JSON.parse(localStorage.getItem('cached-fetch'));
if (cached == null || typeof(cached) == 'undefined') { cached = {}; }
if (typeof(cached[url]) == 'undefined' || (cached[url][0] - tics) < 0) {
let ttl = (typeof(options.ttl) != 'undefined') ? options.ttl : 300000;
exp = tics + ttl;
return fetch(url,options).then( resp => resp.json() )
.then( json => {
cached[url] = [exp, json ];
localStorage.setItem('cached-fetch',JSON.stringify(cached));
return json
}).catch(console.warn);
} else {
return Promise.resolve(cached[url][1]);
}
}
true;
#
pandoc --template=layout.htm index.md -o index.html
<!DOCTYPE html><meta charset="utf8">
<link rel="stylesheet" href=style.css>
<title>location.js</title>
<div>
<p>Your location is :location (:ip)</p>
</div>
<script src=app.js></script>
title
location.js

Your location is :location (:ip)

<!DOCTYPE html><meta charset="utf8">
<link rel="stylesheet" href=style.css>
<title>$title$</title>
<div>
$body$
</div>
<script src=app.js></script>
body {
background-color: #300133;
margin: 0;
padding: 0;
height: 100vh;
}
body > div {
padding: 1.2rem;
max-width: 480px;
height: 10%;
margin: auto;
margin-top: calc(50vh - 5%);
background-color: white;
opacity: 0.94;
box-shadow: 5px 5px 15px 2px rgba(4,3,5,0.9);
border-radius: 12px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment