Skip to content

Instantly share code, notes, and snippets.

View adactio's full-sized avatar

Jeremy Keith adactio

View GitHub Profile
@adactio
adactio / monthmap.php
Last active December 27, 2018 07:21
Display a heat calendar for a month of data.
<?php
// Create an array of timestamps for posts
// and put them into an array called $timestamps.
foreach ($timestamps as $timestamp) {
$day = date("j", $timestamp);
$hour = date("G", $timestamp);
if (isset($heatcalendar['posts'][$day][$hour])) {
$heatcalendar['posts'][$day][$hour]++;
@adactio
adactio / sendWebmention.php
Last active December 27, 2018 07:24
Search a web page for a webmention endpoint and, if one exists, send a webmention to it.
<?php
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# http://creativecommons.org/publicdomain/zero/1.0/
function sendWebmention($source, $target) {
$endpoint = false;
$options = array(
@adactio
adactio / timestampComparison.js
Last active July 30, 2019 14:51
Compare server and client timestamps
// 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 / indyMap.js
Created October 21, 2019 14:12
Scrape the current page for h-geo data and plot the result as a polyline on a Stamen watercolour map.
(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]);
}
});
<?php
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# http://creativecommons.org/publicdomain/zero/1.0/
function postToMedium($data=array()) {
$user_id = "XXXX";
$accessToken = "XXXX";
@adactio
adactio / getEmbedCode.php
Created March 24, 2020 11:29
A PHP function to return an oEmbed result from a URL.
<?php
function getEmbedCode($url="") {
$return = '';
$providers = array(
'flickr.com' => 'https://www.flickr.com/services/oembed/',
'huffduffer.com' => 'https://huffduffer.com/oembed',
'instagram.com' => 'https://api.instagram.com/publicapi/oembed',
@adactio
adactio / trimCache.js
Last active August 17, 2020 13:28
A recursive function to limit the number of items in a specified cache.
// 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)
@adactio
adactio / _polyfills.js
Created November 9, 2020 17:19
JavaScript polyfills for matches,closest, and forEach.
// https://github.com/jonathantneal/closest/blob/master/src/index.js
var ElementPrototype = window.Element.prototype;
if (typeof ElementPrototype.matches !== 'function') {
ElementPrototype.matches = ElementPrototype.msMatchesSelector || ElementPrototype.mozMatchesSelector || ElementPrototype.webkitMatchesSelector || function matches(selector) {
var element = this;
var elements = (element.document || element.ownerDocument).querySelectorAll(selector);
var index = 0;
while (elements[index] && elements[index] !== element) {
++index;
}
@adactio
adactio / giveFeedback.js
Last active August 27, 2022 11:22
An unobtrusive animation effect for providing feedback to the user.
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// http://creativecommons.org/publicdomain/zero/1.0/
/*
This function takes two arguments:
element: a reference to a DOM node in the document e.g. a button.
feedbackContent: a string of text or HTML.
An example of usage would be:
document.querySelector('button').addEventListener('click', function() {
@adactio
adactio / checkbox-slider.html
Created October 1, 2014 18:26
Display a checkbox as a slider.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Checkbox</title>
<style>
body {
background-color: #fff;
color: #000;