website/content/offline.md

89 lines
2 KiB
Markdown
Raw Normal View History

2022-11-22 22:24:30 -05:00
---
title: Offline
description: Your device is offline.
layout: offline
2023-03-10 12:40:38 -05:00
hidden: true
2022-11-22 22:24:30 -05:00
---
Your device is offline. You may have some pages cached for offline viewing,
2023-01-05 01:02:21 -05:00
otherwise please wait for the internet to reconnect to continue browsing.
2024-10-19 21:00:24 -04:00
{{< unsafe >}}
2024-10-19 21:38:51 -04:00
<ul id='offline-posts'>
<li><a href='/'>Homepage</a></li>
</ul>
2024-10-19 21:00:24 -04:00
<script>
function daysAgo(date) {
date.setHours(0, 0, 0, 0);
const time = date.getTime();
2024-10-19 21:38:51 -04:00
const today = new Date();
today.setHours(0, 0, 0, 0);
2024-10-19 21:00:24 -04:00
const now = today.getTime();
const delta = ((now - time) / 1000 / 60 / 60 / 24) | 0;
if (delta < 1) {
return 'today';
}
if (delta === 1) {
return 'yesterday';
}
return `${delta | 0} days ago`;
}
async function listPages() {
// Get a list of all of the caches for this origin
const result = [];
2024-10-19 21:38:51 -04:00
const cache = await caches.open('v1::website');
// Get a list of entries. Each item is a Request object
for (const request of await cache.keys()) {
const url = request.url;
if (url.includes('/blog')) {
console.log(url)
const post = await cache.match(request);
if (post.headers.get('content-type') === 'text/html') {
2024-10-19 21:00:24 -04:00
const body = await post.text();
2024-10-19 21:38:51 -04:00
console.log(body)
2024-10-19 21:00:24 -04:00
const title = body.match(/<title>(.*)<\/title>/)[1];
result.push({
url,
post,
title,
visited: new Date(post.headers.get('date')),
});
}
2024-10-19 21:38:51 -04:00
}
2024-10-19 21:00:24 -04:00
}
const el = document.querySelector('#offline-posts');
if (result.length) {
el.innerHTML = result
.map((res) => {
let html = `<li>
<a href="${res.url}">${res.title}</a>
<small><span title="${res.visited.toString()}">
(visited ${daysAgo(res.visited)})
</span></small>
</li>`;
return html;
})
.join('\n');
}
return result;
}
2024-10-19 21:06:12 -04:00
document.addEventListener("DOMContentLoaded", (event) => {
2024-10-19 21:11:48 -04:00
if (navigator && navigator.serviceWorker) {
listPages()
}
2024-10-19 21:06:12 -04:00
});
2024-10-19 21:00:24 -04:00
</script>
{{< /unsafe >}}