Skip to content

Instantly share code, notes, and snippets.

Jeremy Keith adactio

Block or report user

Report or block adactio

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@adactio
adactio / minimal-serviceworker.js
Last active Jan 13, 2020
An attempt at a minimal viable service worker.
View minimal-serviceworker.js
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// http://creativecommons.org/publicdomain/zero/1.0/
// HTML files: try the network first, then the cache.
// Other files: try the cache first, then the network.
// Both: cache a fresh version if possible.
// (beware: the cache will grow and grow; there's no cleanup)
const cacheName = 'files';
@adactio
adactio / ampify.php
Last active Dec 18, 2019
Make a chunk of markup AMP-ready
View ampify.php
<?php
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# http://creativecommons.org/publicdomain/zero/1.0/
function ampify($html='') {
# Replace img, audio, and video elements with amp custom elements
$html = str_ireplace(
['<img','<video','/video>','<audio','/audio>'],
@adactio
adactio / micropub.php
Last active Dec 16, 2019
Minimal micropub endpoint.
View micropub.php
<?php
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# http://creativecommons.org/publicdomain/zero/1.0/
$mysite = 'https://adactio.com/'; // Change this to your website.
$token_endpoint = 'https://tokens.indieauth.com/token';
$_HEADERS = array();
foreach(getallheaders() as $name => $value) {
@adactio
adactio / indyMap.js
Created Oct 21, 2019
Scrape the current page for h-geo data and plot the result as a polyline on a Stamen watercolour map.
View indyMap.js
(function (win, doc) {
win.addEventListener('load', function() {
var latlons = [];
doc.querySelectorAll('.h-geo').forEach( function(geo) {
var lat = geo.querySelector('data.p-latitude').getAttribute('value');
var lon = geo.querySelector('data.p-longitude').getAttribute('value');
if (lat && lon) {
latlons.push([lat, lon]);
}
});
@adactio
adactio / basicServiceWorker.js
Last active Sep 20, 2019
A basic Service Worker, for use on, say, a blog.
View basicServiceWorker.js
'use strict';
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// http://creativecommons.org/publicdomain/zero/1.0/
(function() {
// Update 'version' if you need to refresh the cache
var staticCacheName = 'static';
var version = 'v1::';
@adactio
adactio / webmention.php
Last active Aug 10, 2019
Minimum viable webmention in PHP.
View webmention.php
<?php
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# http://creativecommons.org/publicdomain/zero/1.0/
if (!isset($_POST['source']) || !isset($_POST['target'])) {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
exit;
}
@adactio
adactio / timestampComparison.js
Last active Jul 30, 2019
Compare server and client timestamps
View timestampComparison.js
// Generate a timestamp (in seconds) on the server. This won't change if the page is served from a cache.
var serverTimestamp = <?php echo time(); ?>;
// Create a new Date object from the local date and time on the client.
var localDate = new Date();
// Convert the local date and time to Universal Time (same as the server).
var localUTCString = localDate.toUTCString();
// Create a new Date object from the UTC date and time on the client.
var UTCDate = new Date(localUTCString);
// Generate a timestamp (in seconds) from the UTC date and time on the client.
var clientTimestamp = UTCDate.getTime() / 1000;
@adactio
adactio / sectioningcontenttest.html
Created Nov 12, 2011
Illustration of sectioning content and the outline algorithm in HTML5.
View sectioningcontenttest.html
<!DOCTYPE html>
<html lang="en">
<title>Sectioning Content test</title>
<h1>This is an h1</h1>
<p>That h1 is the heading for the body (a sectioning root).</p>
<div>
<h1>This is another h1</h1>
<p>That h1 is inside a div so it is no different than the first h1.</p>
</div>
<section>
@adactio
adactio / Twig-critical-CSS-test.twig
Last active Jul 9, 2019
Twig template for inlining critical CSS on first visits.
View Twig-critical-CSS-test.twig
{% set cssupdate = '20150309' %}
{% if _cookie.csscached == cssupdate %}
<link rel="stylesheet" href="/css/global-min.{{ cssupdate }}.css">
{% else %}
<style>
{% include 'global/critical.css' %}
</style>
<script>
(function (win, doc) {
'use strict';
@adactio
adactio / trimCache.js
Last active Jun 27, 2019
A recursive function to limit the number of items in a specified cache.
View trimCache.js
// Limit the number of items in a specified cache.
function trimCache(cacheName, maxItems) {
caches.open(cacheName)
.then( cache => {
cache.keys()
.then(keys => {
if (keys.length > maxItems) {
cache.delete(keys[0])
.then( () => {
trimCache(cacheName, maxItems)
You can’t perform that action at this time.