Skip to content

Instantly share code, notes, and snippets.

View adactio's full-sized avatar

Jeremy Keith adactio

View GitHub Profile
@adactio
adactio / updateDateTimes.js
Last active September 11, 2023 05:10
Periodically update the text of `datetime` elements with the relative time elapsed.
(function (win, doc) {
'use strict';
if (!doc.querySelectorAll || !win.Intl || !win.Intl.RelativeTimeFormat) {
// doesn't cut the mustard.
return;
}
var rtf = new Intl.RelativeTimeFormat('en', {
localeMatcher: 'best fit',
numeric: 'always',
style: 'long'
@adactio
adactio / sectioningcontenttest.html
Created November 12, 2011 12:19
Illustration of sectioning content and the outline algorithm in HTML5.
<!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 / getBandcampArtist.php
Created October 21, 2021 12:06
Scraping Bandcamp for artist info.
<?php
function curlURL($url) {
$return = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
@adactio
adactio / autoLinkMastodonUsernames.php
Created October 30, 2023 13:11
A regular expression to convert @@ usernames into hyperlinks
<?php
/* Pass a string of text into the function to convert @@ usernames. */
function autoLinkMastodonUsernames($string) {
return preg_replace(
'/@?\b([A-Za-zŽžÀ-ÿ0-9._%+-]+)@([A-Za-zŽžÀ-ÿ0-9.-]+\.[A-Za-zŽžÀ-ÿ]{2,})\b/i',
'<a href="https://$2/@$1">$0</a>',
$string
@adactio
adactio / redirectLinks.js
Last active November 20, 2023 18:11
Intercept clicks on external links and pass them to a redirector.
@adactio
adactio / saveTextarea.js
Last active December 2, 2023 06:52
Put the contents of a textarea into localStorage if the user leaves the page before submitting the form.
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// http://creativecommons.org/publicdomain/zero/1.0/
(function (win, doc) {
// Cut the mustard.
if (!win.localStorage) return;
// You should probably use a more specific selector than this.
var textarea = doc.querySelector('textarea');
// The key for the key/value pair in localStorage is the current URL.
var key = win.location.href;
@adactio
adactio / redirect.php
Last active January 2, 2024 23:24
A proxy that tries to redirect bad links to the internet archive.
<?php
// Check that the request is coming from my site.
if (!isset($_SERVER['HTTP_REFERER']) || !stristr(strtolower($_SERVER['HTTP_REFERER']), strtolower($_SERVER['SERVER_NAME']))) {
http_response_code(403);
exit;
}
// There has to be a URL provided in the query string.
if (!isset($_GET['url'])) {
@adactio
adactio / Twig-critical-CSS-test.twig
Last active January 30, 2024 07:13
Twig template for inlining critical CSS on first visits.
{% 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 / aria-controls.js
Last active March 8, 2024 07:07
Show and hide content with "aria-controls" buttons.
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// http://creativecommons.org/publicdomain/zero/1.0/
(function (win, doc) {
'use strict';
if (!doc.querySelectorAll || !win.addEventListener) {
// doesn't cut the mustard.
return;
}
var toggles = doc.querySelectorAll('[aria-controls]');
var togglecount = toggles.length;
@adactio
adactio / webmention.php
Last active March 16, 2024 22:15
Minimum viable webmention in 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;
}